Public Group

# Timing a function!

This topic is 3980 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

This shows me the current time before and after the function giving me zero for an answer. time(NULL); Any other suggestions got get the time before the function is called and after, and finding the difference.
#include <time.h>

time_t start1, end1,  total1, ;

float  s1, e1, t1;

s1 = start1 = time(NULL);

for(int i = 0; i < 1000; i++)
DrawTriangle1(memDC, BLUE);

e1 = end1 = time(NULL);
t1 = e1 - s1;

char str[100];
sprintf(str,"Flood Fill Scan = %.2f", s1);
TextOut(memDC, 10, 20, str, strlen(str));



##### Share on other sites
That would be because you are actually only printing the start time :)

Also, note that time() is not an accurate timing function on most systems, try gettimeofday() on *nix/mac or QueryPerformanceCounter on windows to get better timer resolution.

##### Share on other sites
You'll want to use ctime instead of time.h.

Here's what I use:
unsigned long StartTime = timeGetTime();// do code hereunsigned long EndTime = timeGetTime();float Delta = (EndTime - StartTime) * 0.001f;

Delta now holds how long it took, in seconds. So, for half a second Delta would be 0.5f and for a second and a half it'd be 1.5f.

HTH!

##### Share on other sites
gettimeofday();
Its not part of time.h, whats the #include?
Im not getting any usable respondes from google?

http://www.cplusplus.com/reference/clibrary/ctime/

"That would be because you are actually only printing the start time :)"

I should look over my post better, it was t1 before which gives me 0.
I printed start and end both give me the same number.

OOPS DIDNT NOTICE Programmer16 responds ill try that out

[Edited by - pascalosti on July 26, 2007 11:07:43 PM]

##### Share on other sites
Quote:
 Original post by pascalostigettimeofday(); Its not part of time.h, whats the #include? Im not getting any usable respondes from google?

man gettimeofday would be more helpful than Google, I think. FWIW, on GNU/Linux systems, you need to include sys/time.h and time.h.

##### Share on other sites
Maybe im loosing my mind. Neither worked for me!

unsigned long StartTime1 = timeGetTime();
// thats with #include<ctime>
// and using namespace std;

gettimeofday();
// I added every #include that i could find with "time" in it
// I could not find "FWIW" or anything that looks similar.

##### Share on other sites
From the first hit on Google:
#include <sys/time.h>

You are running Linux/Unix, not Windows, right?

Edit: According to the "Who's online" section, you are running Windows XP. As was previously mentioned, on Windows, you want to use QueryPerformanceCounter.

##### Share on other sites

I was very intimidated by this site
http://support.microsoft.com/kb/172338

this one made it easier to swallow
http://www.decompile.com/cpp/faq/windows_timer_api.htm

I think im making progress!

Thanks all for the help

[Edited by - pascalosti on July 27, 2007 12:51:50 AM]

##### Share on other sites

Here is what the final looks like!!
Looks really messy
if(GetTime){				// get the high resolution counter's accuracy				QueryPerformanceFrequency(&ticksPerSecond);				// what time is it?				QueryPerformanceCounter(&tick);				// since the system was started...				timeX.QuadPart = tick.QuadPart/ticksPerSecond.QuadPart;				//get the number of hours				Delta1 = (timeX.QuadPart * 100);				GetTime = false;			}			DrawTriangle1(memDC, BLUE);			//DrawTriangle(memDC, GREEN);								if(GetTime1)			{				// get the high resolution counter's accuracy				QueryPerformanceFrequency(&ticksPerSecond1);				// what time is it?				QueryPerformanceCounter(&tick1);				//get the number of hours				// since the system was started...				timeX1.QuadPart =  tick1.QuadPart/ticksPerSecond1.QuadPart;				Delta2 = (timeX1.QuadPart * 100);				GetTime1 = false;			}			total = Delta2 - Delta1;

##### Share on other sites
Edit: call QueryPerformanceFrequency() only once, and handle the timer accuracy in a better, more readable way

Quote:
 Original post by pascalostiHere is what the final looks like!! Looks really messy*** Source Snippet Removed ***

And here's how it should look like, providing that you are using C++ and not plain C.

class timer{  LARGE_INTEGER  frequency;  LARGE_INTEGER  tick_start;  LARGE_INTEGER  accumulated_time;  unsigned long  timer_accuracy;public:  static const unsigned long seconds = 1;  static const unsigned long milliseconds = 1000;  static const unsigned long microseconds = 1000000;public:  timer(unsigned long accuracy = milliseconds)     : accumulated_time()    , timer_accuracy(accuracy)  {    QueryPerformanceFrequency(&frequency);    restart();  }  void restart()  {    QueryPerformanceCounter(&tick_start);  }  void reset()  {    accumulated_time = LARGE_INTEGER();  }  // stop the timer, and update the accumulated time  void stop()  {    LARGE_INTEGER tick_current;    QueryPerformanceCounter(&tick_current);    accumulated_time.QuadPart += tick_current.QuadPart - tick_start.QuadPart;  }    // retrieve accumulated time  unsigned __int64 get_accumulated_time(unsigned long accuracy) const  {    return (accumulated_time.QuadPart * accuracy) / frequency.QuadPart;  }  unsigned __int64 get_accumulated_time() const  {    return get_accumulated_time(timer_accuracy);  }  // compute elapsed time since last restart, but do not   // accumulate the time  unsigned __int64 get_elapsed_while_running(unsigned long accuracy) const  {    LARGE_INTEGER tick_current;    QueryPerformanceCounter(&tick_current);    LARGE_INTEGER elapsed;     elapsed.QuadPart = tick_current.QuadPart - tick_start.QuadPart;    return (elapsed.QuadPart * accuracy) / frequency.QuadPart;  }  unsigned __int64 get_elapsed_while_running() const  {    return get_elapsed_while_running(timer_accuracy);  }};

And it's used like this:
  // time with a milliseconds accuracy  timer  my_timer(timer::milliseconds);    for (int i=0; i<1000; ++i)  {    std::cout << "index = " << i << std::endl;  }  my_timer.stop();  std::cout << "accumulated elasped time: "             << my_timer.get_accumulated_time()             << " ms"             << std::endl;  my_timer.restart();  for (int i=0; i<1000; ++i)  {    std::cout << "elapsed time since last restart = "               << my_timer.get_elapsed_while_running(timer::microseconds);              << " µs" << std::endl;  }  my_timer.stop();  std::cout << "accumulated elasped time: "             << my_timer.get_accumulated_time()             << " ms"             << std::endl;

Which is a bit easier than your code [smile].

[Edited by - Emmanuel Deloget on July 27, 2007 9:33:03 AM]

• 18
• 11
• 16
• 9
• 51
• ### Forum Statistics

• Total Topics
631396
• Total Posts
2999786
×