• 0

Time function in C++


Question

i'm try to make a comparison between the speed of sorting methods, and i've been using this one

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

int main ()
{
  time_t seconds;
  time_t seconds1;
  time_t realTime;

  seconds = time (NULL);

Sorting method

  seconds1 = time (NULL);

  realTime = seconds1 - seconds;

  cout << "This method took "<< realTime << " sec";

  return 0;

}

Problem is that i need the measurments to be very accurate in milliseconds or even microseconds and this will only measure the time in seconds. So the question is, do u guys know a c++ that measures time in milliseconds or microseconds and how do i use it?

i'm using visual studio 2008 btw

Link to comment
Share on other sites

12 answers to this question

Recommended Posts

  • 0

give this a shot. I found it at the following QueryPerformanceCounter

QueryPerformanceCounter is defined in windows.h

ulong lFreq;
ulong lTime1;
ulong lTime2;
long diffTicks;
float diffSecs;

QueryPerformanceCounter(&lFreq);
QueryPerformanceFrequency(&lTime1);
//function call here
QueryPerformanceFrequency.(&lTime2);
diffTicks=lTime1.QuadPart-lTime2.QuadPart;//ticks passed
diifSecs=(diffTicks/lFreq.QuadPart);//secs passed.

let me know if you have questions on how this works

Wesley

Link to comment
Share on other sites

  • 0

i must have done something wrong, cause the results keep changing on the same method, not to mention that i got some negative results, lol

here's something i tried, could u plz run it and tell me whats wrong

#include <iostream>
#include <stdio.h>
#include <time.h>
#include <windows.h>
using namespace std;
int main ()
{


LARGE_INTEGER lFreq;
 LARGE_INTEGER lTime1;
LARGE_INTEGER lTime2;
long diffTicks;
float diffSecs;

QueryPerformanceFrequency(&lFreq);
QueryPerformanceCounter(&lTime1);




  time_t seconds;
  time_t seconds1;
  time_t realTime;
  int  i=0;

  seconds = time (NULL);

  while(i < 200000000)  
  {
	  i++;
	  i--;
	  i++;
	  i--;
	  i++;
  }

  seconds1 = time (NULL);

  realTime = seconds1 - seconds;
QueryPerformanceFrequency(&lTime2);
diffTicks=lTime2.QuadPart-lTime1.QuadPart;//ticks passed
diffSecs=(diffTicks/lFreq.QuadPart);//secs passed.

  cout << diffSecs;

  return 0;

}

Link to comment
Share on other sites

  • 0

Just looking at it quickly, this line may need to change:

QueryPerformanceFrequency(&lTime2);

diffTicks=lTime2.QuadPart-lTime1.QuadPart;//ticks passed

=>

QueryPerformanceCounter(&lTime2);

diffTicks=lTime2.QuadPart-lTime1.QuadPart;//ticks passed

QueryPerformanceFrequency gets the frequency, in ticks, of the high-precision counter of the processor.

QueryPerformanceCounter gets the current tick count, which can be used to measure time.

Hope that helps!

Edited by someone64
Link to comment
Share on other sites

  • 0

Alternatively, you can use GetTickCount, which will give you something with a resolution of around 15ms and is much simpler to use.

DWORD dwTickCount = GetTickCount();
// Do some stuff
printf("%u ms elapsed\n", GetTickCount() - dwTickCount);

Link to comment
Share on other sites

  • 0
Alternatively, you can use GetTickCount, which will give you something with a resolution of around 15ms and is much simpler to use.

DWORD dwTickCount = GetTickCount();
// Do some stuff
printf("%u ms elapsed\n", GetTickCount() - dwTickCount);

Great suggestion, as that's much simpler. In the same way, you could use mmsystem.h (already included by windows.h)/winmm.lib's timeGetTime() in place of GetTickCount() in the example kliu0x52 provided for ~1-5ms precision. It's important to note that overhead increases as precision increases, so kliu0x52's suggestion of GetTickCount is the fastest, followed by timeGetTime, and then QueryPerformanceCounter. It can vary, though, depending on the system.

On some systems, to get the best precision from timeGetTime, you may need to add this:

int main ()
{
	timeBeginPeriod( 1 );

	PROGRAM HERE

	timeEndPeriod( 1 );
}

This code will set the precision for timeGetTime to 1ms for the duration of your program's runtime. timeBeginPeriod(x) sets it, timeEndPeriod(x) restores the default precision. GetTickCount may also benefit from timeBeginPeriod/timeEndPeriod, as both timeGetTime/GetTickCount rely on the scheduler; I've just never tried it lol

Anyway, try each of them and see what works for you :)

Edited by someone64
Link to comment
Share on other sites

  • 0

ok, thanks guys i got it working. but now i have another problem and this one is a doozy. See, this whole project si for a "Data Structure" course i'm taking and now i'm supposed to show the results i got using a graph and i dont even know anything about graphics in c++ if thats what it actually needs. So basically i have lets say 3 sorting methods tested with 3 different size arrays and each has a a result, how can i turn that into a graph or is there like some kind of a plugin i can use that will be called to create that graph. hope that makes sense

thanks

Link to comment
Share on other sites

  • 0

that didnt work with visual studio, at first it asked for graphics.h library and after i installed that one, it asked for gtk/gtk.h. Any other suggestions

Link to comment
Share on other sites

  • 0

Are you sure they meant "write a program to graph the results" instead of "create a graph with your results [using any graphing program of your choice]"? Because graphics is extremely platform-specific and API-specific. I find it very hard to believe that any self-respecting data structures instructor would have anyone do something that is so off-topic and totally irrelevant to the point of a data structures course (esp. if it ends up taking more effort than the actual benchmarking and coding of algorithm, which it probably will be, due to the tedious nature of such things).

Link to comment
Share on other sites

  • 0

well, i have the results stored, i wanna output them into a chart. And you're right, the instructor assumed that we all took teh graphics course, witch all the class except for me did. And this is not the whole assignment, sorting algorithms is the smallest part, the main program is about stack and queue but i finished that. doing this graph is the last thing

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.