Solved

Strange time consumption in for loop

Posted on 2012-03-21
6
276 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
Comment Utility
Well you loop is doing a total of 500 * 500 * 100 iterations. Thats 25000000 times through a LOOP!!
0
 

Author Comment

by:DBTechnique
Comment Utility
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 166 total points
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 53

Accepted Solution

by:
Infinity08 earned 167 total points
Comment Utility
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 32

Assisted Solution

by:sarabande
sarabande earned 167 total points
Comment Utility
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
Comment Utility
To Infinity08 :
Thanks ! Those are really useful information.
To Sara :
I am too curious now to not try it ^^
Thank you.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

771 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now