Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

change clock's precision

Status
Not open for further replies.

davyzhu

Advanced Member level 1
Joined
May 23, 2004
Messages
494
Helped
5
Reputation
10
Reaction score
2
Trophy points
1,298
Location
oriental
Activity points
4,436
Hello all,

I use clock() to measure the time cost in my program.
But the t_clock always be the multiply of 10, for example 10, 20, 30, 40,...
And I want to get the precision of 1, such as 11, 12, 13, 14...
How can I change the t_clock's precision?

Code list below:
//-----------------
#include <cstdio>
#include <fstream.h>
#include <ctime>

void main() {
int x;
clock_t t_click,t_start;

t_start = clock();
for (x=0;x<60;x++) {
printf("%d\n",x);
}

t_click = clock() - t_start;
printf("Time Elapsed %d\n",t_click);

}
//-------------------
BTW, I use VC6.0. The environment is P3 600MHz and WinXP.

Any suggestions will be appreciated!

Best regards,
Davy
 

Your example runs almost instantly on my system, and gives the value 0.

You are seeing the resolution of your system multitasking timer, which defaults to somewhere around 5 or 10 or 15 milliseconds. You can increase its resolution to 1ms by calling the multimedia functions timeBeginPeriod() and timeEndPeriod().

Instead of using clock(), try QueryPerformanceCounter() and QueryPerformanceFrequency(). It has much higher resolution.
Code:
#include <windows.h>
#include <stdio.h>

int main(void)
{
  int x;
  LARGE_INTEGER t_start, t_stop, t_freq;
  double t_click;

  QueryPerformanceFrequency(&t_freq);
  QueryPerformanceCounter(&t_start);
  for (x=0; x<60; x++)
    printf("%d\n", x);
  QueryPerformanceCounter(&t_stop);

  t_click = (t_stop.QuadPart - t_start.QuadPart) / (double)t_freq.QuadPart;
  printf("Time Elapsed %f\n", t_click);
  return 0;
}
That prints:
...
56
57
58
59
Time Elapsed 0.003213
 

    davyzhu

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top