Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > change clock's precision

Reply
Thread Tools

change clock's precision

 
 
Robert
Guest
Posts: n/a
 
      10-05-2005
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

 
Reply With Quote
 
 
 
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      10-05-2005
Robert wrote:
>
> 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.

--
Karl Heinz Buchegger
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      10-05-2005
In article <(E-Mail Removed). com>,
Robert <(E-Mail Removed)> wrote:
>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...
--
Programming is what happens while you're busy making other plans.
 
Reply With Quote
 
=?iso-8859-1?Q?Ali_=C7ehreli?=
Guest
Posts: n/a
 
      10-05-2005
"Robert" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...

> 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>


<fstream.h> is pre-standard; use <fstream> instead. (Though you don't use it
in the program.)

> #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

 
Reply With Quote
 
Mark B
Guest
Posts: n/a
 
      10-05-2005
"Robert" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> 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



 
Reply With Quote
 
RamOn
Guest
Posts: n/a
 
      10-05-2005
Hello,

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

Hope this helps

RamOn


In article <(E-Mail Removed). com>,
(E-Mail Removed) says...
> 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
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A Paradise DNS address change? What change? There was no change. Tony Neville NZ Computing 7 09-22-2006 01:02 PM
iostreams: change default floating point precision? Joseph Turian C++ 1 04-17-2006 10:28 AM
change clock's precision Robert C++ 5 10-05-2005 06:50 PM
Need > 15 digits precision without big performance hit Jasper Perl 1 06-27-2004 08:25 AM
Precision Change - Float to String Bryan R. Meyer C++ 5 05-22-2004 01:09 AM



Advertisments