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
Solved

Strange time consumption in for loop

Posted on 2012-03-21
6
280 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 166 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 53

Accepted Solution

by:
Infinity08 earned 167 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 33

Assisted Solution

by:sarabande
sarabande earned 167 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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 tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

791 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