Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Strange time consumption in for loop

Posted on 2012-03-21
6
Medium Priority
?
286 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
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

886 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