[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

timing how long  functions/routine

Posted on 2007-08-02
8
Medium Priority
?
195 Views
Last Modified: 2010-04-01
Hi folks, I want to time particular function calls in my routine(s). For example, I remove color in one program version by an iterating routine. In another version I do it by calling a function that does a matrix transform with a LUT table. But the bottom line is, I need to evaluate which is faster.I also don't mind if there are program breaks along the way that return the time. Thats is unimportant as I will eventually take it back out.  Is there a quick a dirty way to do this?

WLE
0
Comment
Question by:Wanderinglazyeye
8 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 19619313
What's your OS?
0
 
LVL 30

Expert Comment

by:Axter
ID: 19619346
>>Is there a quick a dirty way to do this?

For Win32, you can use GetTickCount()
0
 
LVL 86

Expert Comment

by:jkr
ID: 19619358
A portable way to do that is using 'clock()', e.g.

/* CLOCK.C: This example prompts for how long
 * the program is to run and then continuously
 * displays the elapsed time for that period.
 */

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void sleep( clock_t wait );

void main( void )
{
   long    i = 600000L;
   clock_t start, finish;
   double  duration;

   /* Delay for a specified time. */
   printf( "Delay for three seconds\n" );
   sleep( (clock_t)3 * CLOCKS_PER_SEC );
   printf( "Done!\n" );

   /* Measure the duration of an event. */
   printf( "Time to do %ld empty loops is ", i );
   start = clock();
   while( i-- )
      ;
   finish = clock();
   duration = (double)(finish - start) / CLOCKS_PER_SEC;
   printf( "%2.1f seconds\n", duration );
}

/* Pauses for a specified number of milliseconds. */
void sleep( clock_t wait )
{
   clock_t goal;
   goal = wait + clock();
   while( goal > clock() )
      ;
}
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 30

Accepted Solution

by:
Axter earned 800 total points
ID: 19619373

Example:

DWORD StartTime = GetTickCount();

//Do some work here


LenTime = GetTickCount() - StartTime;

printf("Time duration is %li\n", LenTime);
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 800 total points
ID: 19619385
BTW, if you are on Windows 'GetTickCount()' (http://msdn2.microsoft.com/en-us/library/ms724408.aspx) is a little more convenient, i.e.

DWORD dwStart, dwEnd, dwDuration;

dwStart = GetTickCount();

for (int i = 0; i < 200000;++i) {

  // some calculation
}

dwEnd = GetTickCount():

dwDuration = dwEnd - dwStart;
0
 
LVL 86

Expert Comment

by:jkr
ID: 19619712
Oh, and here's a nice and simple utility class for that task: http://www.codeproject.com/cpp/duration.asp ("Precise duration measurement -
A simple class that provides high precision timing")
0
 
LVL 9

Assisted Solution

by:jasonclarke
jasonclarke earned 400 total points
ID: 19623604
There are tools that do this for you if you want something a little more sophisticated.   One that works fairly well for C++ is Rational's PurifyPlus:  http://www-306.ibm.com/software/awdtools/purifyplus/ (it produces tables/charts and annotated source code that show you exactly where the time is being used in the execution of code).

The tool is fairly expensive to buy - but you can download it and use it for free for 15 days.
0
 

Author Comment

by:Wanderinglazyeye
ID: 19624407
Awesome everyone, thankyou. FYI I used axter/jkr's tick marks calls and produced some interesting results. I also may look at jasonclarke's suggestion as well.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 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.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

872 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