Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I write the following programe.The output confused me much.Someone please kindly explain why this happen.
#include
#include
#includemain()
{
double x;
int i;
x = clock()/CLOCKS_PER_SEC;
printf("%f\n%f\n",CLOCKS_PER_SEC,x);
for(i=0; i<300000000; i++)
;
x = clock()/CLOCKS_PER_SEC - x;
printf("%f\n",x);
}When compiled with gcc in linux the output is:
0.000000
20041826304.000000
0.000000But when compiled with devcpp in win98,the output is:
1000.000000
0.000000
0.160000
which I think is a better result.Thank you very much!

Try modifing the 'int x' range. Replace it with 'unsigned long int x'. It may be gcc uses a different 'int' width range than devcpp. Let me know if it works.
----------------
giuseppe__NOSPAM__branca@altavista.it
remove __NOSPAM__ to get the correct address.

Yes,I think you are right!Thank you for your advice,Now I modified it like this:
main()
{
long int x;
int i,j;
x = clock();
printf("%ld\n",x);
for(i=0; i<3000; i++)
for(j=0;j<3000;j++)
;
x = clock();
printf("%ld\n",x);
printf("%d\n",CLOCKS_PER_SEC);
}and it did come out with a reasonable result:
0
20000
1000000But now another question rise:
It seems that the return value of clock() will always be a multiple of 10000! I could not get any value between 0~1000.May it be an unreasonable design or just that I made some mistake?

The program you wrote is correct. See clock() reference for more information...
-----------------
remove __NOSPAM__ to get the correct address
giuseppebranca@altavista.it
http://pizzacode.too.it

There probably is no error at all. Many general functions that seem to return high-resolution time (e.g., milliseconds), are not actually capable of such precision. For example, it is not uncommon for clock() to return the elapsed time in seconds, and then return it multiplied by CLOCKS_PER_SEC. Thus, the smallest increment would be CLOCKS_PER_SEC. This all depends on the machine, OS, actual function code, etc. These functions commonly just look at the often-coarse system counter (the system normally does not need such precision).
The OS cannot update its internal counter too often, because other code has to run with as few interrupts as possible. For older, slower machines, this is why often only a one second resolution was considered realistic for things like file times. Millisecond resolutions are a reflection of look-ahead planning, for as computers get faster, it becomes more practical to poll the CPU more frequently.
Millisecond precision can be obtained by using custom routines. Multimedia apps in Linux use these, and on Windows there are ones in the Win32 API and MultiMedia API. They are typically started and stopped for as short a time as possible, because they eat up CPU time more (they poll the CPU more frequently while running). Setting the coarsest resolution acceptible, is a good way to reduce overhead.
Cheers

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |