Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Strange time consumption in for loop

Posted on 2012-03-21
6
Medium Priority
?
285 Views
Last Modified: 2012-03-21
Hello Experts,
I have encountered a strange situation.
I have added the corresponding code and the TRACE results.
So basically, I have two / three (three in the example but it is the same behavior with only two for loops), and it is making a huge time consumption on certain index.
Usually, it takes 0 milliseconds, but sometimes, it will take 16 ms.
I am working with VC2010 SP1 with Windows 7 and under Debug mode.
Thanks in advance !

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	int nRetCode = 0;

	HMODULE hModule = ::GetModuleHandle(NULL);

	if (hModule != NULL)
	{
		// initialize MFC and print and error on failure
		if (!AfxWinInit(hModule, NULL, ::GetCommandLine(), 0))
		{
			// TODO: change error code to suit your needs
			_tprintf(_T("Fatal Error: MFC initialization failed\n"));
			nRetCode = 1;
		}
		else
		{
			for (unsigned int i = 0 ; i < 100 ; i++)
			{
				DWORD dwT1 = GetTickCount();

				for (unsigned int j = 0 ; j < 500 ; j++)
				{
					for (unsigned int k = 0 ; k < 500 ; k++)
					{
						int Position = 0;
						Position++;
					}
				}

				DWORD dwT2 = GetTickCount();
				TRACE(_T("Delai (%d) = %d\r\n"), i, dwT2 - dwT1);
			}
		}
	}
	else
	{
		// TODO: change error code to suit your needs
		_tprintf(_T("Fatal Error: GetModuleHandle failed\n"));
		nRetCode = 1;
	}

	return nRetCode;
}

Open in new window


Delai (0) = 0
...
Delai (5) = 0
Delai (6) = 15
Delai (7) = 0
...
Delai (14) = 0
Delai (15) = 16
Delai (16) = 0
...
Delai (28) = 0
Delai (29) = 15
Delai (30) = 0
...
Delai (63) = 0
Delai (64) = 16
Delai (65) = 0
...
Delai (98) = 0
Delai (99) = 16
0
Comment
Question by:DBTechnique
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 37

Expert Comment

by:Neil Russell
ID: 37746166
Well you loop is doing a total of 500 * 500 * 100 iterations. Thats 25000000 times through a LOOP!!
0
 

Author Comment

by:DBTechnique
ID: 37746182
But why doesn't it take the same time for all the iterations of the first loop ?
0
 
LVL 37

Assisted Solution

by:Neil Russell
Neil Russell earned 664 total points
ID: 37746190
Almost certainly because your computer is doing other things. Also your innermost loop will almost certainly be optimised out by the compiler when compiled to a runtime. It does nothing, changes nothing and non of its variables are used except in the loop itself. A prime candidate for optimization.

A loop for timing is of no use at all as you can NOT predict what else windows is doing.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 53

Accepted Solution

by:
Infinity08 earned 668 total points
ID: 37746221
The work you're doing is too short to measure accurately. It would be even worse if you enabled optimization (I hope you didn't).

The granularity of GetTickCount is around 15ms on many systems. And apparently also on yours.
And it's that granularity that you're seeing. Consecutive calls to GetTickCount will return values like 0, 0, ..., 0, 15, 15, ..., 15, 30, etc., and not 0, 1, 2, 3, etc.

To get a more accurate timing, run the code you're measuring a few thousand times (or more, such that it takes at least several seconds to complete), measure the total time, and then divide by the amount of times you ran the code.
0
 
LVL 35

Assisted Solution

by:sarabande
sarabande earned 668 total points
ID: 37746492
to add to above comment:

optimization is off when compiling a visual studio project in debug mode.  but it is not recommended to make time measurement in debug mode cause the debugger necessarily distorts the results.

the GetTickCount returns number of milliseconds gone since the (windows) system was started. the return type is DWORD what is defined as unsigned int. on a system with 32-bit integers (as far as i know all windows systems since win32s) it wraps to zero after 49.7 days.

the resolution can be retrieved by GetSystemTimeAdjustment and could be set by SetSystemTimeAdjustment, but i never did it myself and never heard that someone made adjustments.

Sara
0
 

Author Closing Comment

by:DBTechnique
ID: 37746557
To Infinity08 :
Thanks ! Those are really useful information.
To Sara :
I am too curious now to not try it ^^
Thank you.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question