CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    May 2002
    Location
    Somewhere in Asia
    Posts
    96

    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

  2. #2
    Join Date
    Nov 2002
    Location
    Devon, UK
    Posts
    212
    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.

  3. #3
    Join Date
    Aug 2000
    Posts
    116
    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;
    }
    }

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637
    Or you could use profiling.

  5. #5
    Join Date
    May 2002
    Location
    Somewhere in Asia
    Posts
    96

    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.

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    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.

  7. #7
    Join Date
    Apr 2004
    Posts
    17
    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

  8. #8
    Join Date
    Dec 2003
    Posts
    63
    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 !

  9. #9
    Join Date
    Aug 2000
    Posts
    116
    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;
    }

  10. #10
    Join Date
    Apr 2004
    Location
    Belgium
    Posts
    7
    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.

  11. #11
    Join Date
    Aug 2000
    Posts
    116
    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
  •  





Click Here to Expand Forum to Full Width

Featured