|
-
April 29th, 2004, 05:45 AM
#1
How to measure the time taken to run a certain process?
Hi there...
Would like to check with experts of Visual C++ out there..how am I able to get the time taken to run a centain portion of my program.
my current method is
StartClock = clock();
for (i = 0; i<n; i++)
{
x = x + 1; // just an example
}
EndClock = clock();
durationClock = EndClock - StartClock ;
however, it seems that the StartClock and EndClock are the same... probably the function is too fast.
Is there another way to capture the timing?
Thanks.
Joseph
-
April 29th, 2004, 05:51 AM
#2
You can get the current clock tick by calling GetTickCount. If you need more accuracy, you need to think about using performance counters - see QueryPerformanceCounter and his friends.
Some cause happiness wherever they go; others, whenever they go.
-
April 29th, 2004, 06:49 AM
#3
Try this code. To start the timing process call TimeTaken( 1 ) and to end it call TimeTaken( 0 ). Returned value after second TimeTaken call is the timed value. Hope this helps.
unsigned int TimeTaken( BOOL Start )
{
static LARGE_INTEGER Frequency;
static LARGE_INTEGER StartTime;
static LARGE_INTEGER StopTime;
double dTimeTaken;
LARGE_INTEGER Temp;
if( Start )
{
QueryPerformanceFrequency( &Frequency );
QueryPerformanceCounter( &StartTime );
return 0;
}
else
{
QueryPerformanceCounter( &StopTime );
if( StopTime.QuadPart < StartTime.QuadPart ) // Timer wrapped around
{
dTimeTaken = (double)MAX_64BIT_SIGNED_NO - StartTime.QuadPart;
Temp.QuadPart = MIN_64BIT_SIGNED_NO - StopTime.QuadPart;
Temp = Abs64( Temp );
dTimeTaken = (double)Temp.QuadPart;
}
else
{
dTimeTaken = (double)(StopTime.QuadPart - StartTime.QuadPart);
}
dTimeTaken = (dTimeTaken / (double)Frequency.QuadPart) * 1000.0; // 1000 = milisec 1000000.0 = microsec
return (unsigned int)dTimeTaken;
}
}
-
April 29th, 2004, 07:10 AM
#4
Or you could use profiling.
-
April 29th, 2004, 10:33 PM
#5
Calculating Energy comsumption using the time taken?
Hi All,
Thanks a lot... it was indeed helpful!
err... GCDEF, what do you mean by profiling?
I'm trying to calculate the Energy consumption for my program on the desktop PC (win2000) running MS VC++ 6.0... based on the time taken...
MY understanding is, since Energy = Power x time, so I first calculate the time taken by my program, multiply by the power consumption per clock by my CPU...
will that give me the Energy comsumed by my program? has anyone tried this before?
Thanks.
Regards,
Joseph
Last edited by tcmjoe; April 29th, 2004 at 10:35 PM.
-
April 30th, 2004, 07:11 AM
#6
Re: Calculating Energy comsumption using the time taken?
Originally posted by tcmjoe
Hi All,
Thanks a lot... it was indeed helpful!
err... GCDEF, what do you mean by profiling?
I'm trying to calculate the Energy consumption for my program on the desktop PC (win2000) running MS VC++ 6.0... based on the time taken...
MY understanding is, since Energy = Power x time, so I first calculate the time taken by my program, multiply by the power consumption per clock by my CPU...
will that give me the Energy comsumed by my program? has anyone tried this before?
Thanks.
Regards,
Joseph
MSDN should provide all the information you need about profileing. It's a tool that tells you how much time your program spends in each function, how often your function is called, etc.
I doubt you can get anything meaningful regarding energy consumption. The CPU is cycling, the hard drive is spinning, the monitor displaying whether your program is running or not. Moving the drive heads may consume a tiny insignificant amount of power, other than that Nothing you could realistically measure I would think. I don't see why consumption would change just because you run a program.
-
April 30th, 2004, 09:14 AM
#7
hi,
you could also see in MSDN (i think it is simplier than Profiling)
GetProcessTimes and see the calculation formula on this site:
http://www.alexfedotov.com/samples/wmitop.asp
hmm ... i've just saw you want to calculate your prog
power/energy comsumption ... :-) this i don't know how to make ...
wish you luck,
typecast
-
May 4th, 2004, 12:30 AM
#8
Dear all,
I used the code segments GCDEF provided and build code. But I encountered the error message :
MAX_64BIT_SIGNED_NO,
MIN_64BIT_SIGNED_NO, and
Abs64
are "undeclared identifier"......
Do I forget to include which file ?
Thanks !
-
May 4th, 2004, 01:53 AM
#9
Missing code for above sample, sorry.
#define MAX_64BIT_SIGNED_NO 9223372036854775807
#define MIN_64BIT_SIGNED_NO -9223372036854775808
LARGE_INTEGER Abs64( LARGE_INTEGER Value )
{
if( Value.QuadPart < 0 )
{
if( Value.QuadPart == MIN_64BIT_SIGNED_NO ) Value.QuadPart++;
Value.QuadPart *= -1;
}
return Value;
}
-
May 9th, 2004, 02:05 AM
#10
Nice code, Matthew! But I have a remark (or two). This is your program:
Code:
#define MAX_64BIT_SIGNED_NO 9223372036854775807
#define MIN_64BIT_SIGNED_NO -9223372036854775808
LARGE_INTEGER Abs64( LARGE_INTEGER Value )
{
if( Value.QuadPart < 0 )
{
if( Value.QuadPart == MIN_64BIT_SIGNED_NO ) Value.QuadPart++;
Value.QuadPart *= -1;
}
return Value;
}
unsigned int TimeTaken( BOOL Start )
{
static LARGE_INTEGER Frequency;
static LARGE_INTEGER StartTime;
static LARGE_INTEGER StopTime;
double dTimeTaken;
LARGE_INTEGER Temp;
if( Start )
{
QueryPerformanceFrequency( &Frequency );
QueryPerformanceCounter( &StartTime );
return 0;
}
else
{
QueryPerformanceCounter( &StopTime );
if( StopTime.QuadPart < StartTime.QuadPart ) // Timer wrapped around
{
*-> dTimeTaken = (double)MAX_64BIT_SIGNED_NO - StartTime.QuadPart;
Temp.QuadPart = MIN_64BIT_SIGNED_NO - StopTime.QuadPart;
Temp = Abs64( Temp );
*-> dTimeTaken = (double)Temp.QuadPart;
}
else
{
dTimeTaken = (double)(StopTime.QuadPart - StartTime.QuadPart);
}
dTimeTaken = (dTimeTaken / (double)Frequency.QuadPart) * 1000.0;
return (unsigned int)dTimeTaken;
}
}
In the first marked line, dTimeTaken gets a value, but in the next marked line dTimeTaken gets a new value without using the first. So, is the first marked line really necessary?
I've read also an article from Microsoft about this function: Read this before using this function.
But, as I've said: I like your code and I'm already using it. Thx.
-
May 10th, 2004, 01:40 AM
#11
Yes Newix, the second dTimeTaken line is needed but it should read
dTimeTaken += (double)Temp.QuadPart;
This part of the code is only used if the timer value has exceeded the maximum 64bit signed value and has wrapped back to the minimum value. Shouldn't hit this code very often.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|