change clock's precision

R

Robert

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
 
K

Karl Heinz Buchegger

Robert said:
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?

You can't.
The 'precision' is given to you and there is no way to change
that. That may eg. be because the hardware doesn't support a
finer granularity.
 
W

Walter Roberson

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?

There is no way to do that in standard C. Keep in mind that
clock_t is a count of CLOCKS_PER_SEC and not an absolute time,
so a value of 10 might mean 10 femtoseconds with the extra digit
reserved for the next board rev with the built-in 1 femtosecond
counter.

Code list below:
//-----------------

// comments are not allowed in C89.
#include <cstdio>
#include <fstream.h>
#include <ctime>

Those are not standard C headers. If you expect us to be able to
understand your program in comp.lang.c, you will have to provide the
expansion of the headers.

void main() {

In standards-compliant C code, main() must return an int.

Also, if you are not declaring parameters for main, it is
better practice to prototype it as main(void)
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);

You did not divide by CLOCKS_PER_SEC so printing out the number
of ticks gives us no idea how long the program took.

In C89, if you do not return a value from main, the exit status
of the program is undefined.
//-------------------
BTW, I use VC6.0. The environment is P3 600MHz and WinXP.
Any suggestions will be appreciated!

Any higher precision timing would be implementation specific,
and is thus not a suitable topic for comp.lang.c .
This topic is, by the way, disposed of in the comp.lang.c FAQ...
 
?

=?iso-8859-1?Q?Ali_=C7ehreli?=

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?

You cannot change the precision; you can only measure. Windows allocates
each thread a certain amount of time slice. Apparently the granularity of
that slice on your system is 10/CLOCKS_PER_SEC seconds.

Use a different timing function.

[It is completely off-topic on this group, and my Windows programming skills
are very rusty, but you may start reading on GetTickCount and
QueryPerformanceCounter functions.]
Code list below:
//-----------------
#include <cstdio>
#include <fstream.h>

#include <ctime>

void main() {

'void main' is not standard; should be 'int 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);

This is one example that demonstrates the superiority of C++'s operator<<
overloading over printf. Since we don't know the actual type of clock_t, we
cannot know what format identifier to use with printf (%d didn't match
clock_t on my system).

This works:

cout << "Time Elapsed: " << t_click << '\n';
}
//-------------------
BTW, I use VC6.0.

In that case, in addition to using 'int main', you will have to explicitly
add a return statement at the end of your main function:

return 0;

That statement is not necessary and most other compilers get that right; but
VC++60 needs you to type it. In other words, even though the following is a
valid C++ program, VC++60 will need you to add a return statement.

int main()
{}

Ali
 
M

Mark B

Robert said:
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?

divide it by 10 ;)
 
R

RamOn

Hello,

Under Windows, maybe you can get advantage of using timeGetTime() /
timeBeginPeriod() / timeEndPeriod() instead of clock()

Hope this helps

RamOn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top