Computing.Net > Forums > Programming > Trouble in A little C programe

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Trouble in A little C programe

Reply to Message Icon

Name: hydra
Date: October 2, 2002 at 05:00:44 Pacific
OS: rh linux
CPU/Ram: P4/128ddr
Comment:

I write the following programe.The output confused me much.Someone please kindly explain why this happen.

#include
#include
#include

main()
{
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.000000

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



Sponsored Link
Ads by Google

Response Number 1
Name: Giuseppe
Date: October 2, 2002 at 09:41:12 Pacific
Reply:

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.


0

Response Number 2
Name: hydra
Date: October 2, 2002 at 13:02:12 Pacific
Reply:

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
1000000

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



0

Response Number 3
Name: Giuseppe
Date: October 3, 2002 at 08:42:48 Pacific
Reply:

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


0

Response Number 4
Name: Jeff J
Date: October 3, 2002 at 15:35:44 Pacific
Reply:

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


0

Response Number 5
Name: hydra
Date: October 4, 2002 at 08:55:03 Pacific
Reply:

Thank you!
Very helpful indeed!


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Trouble in A little C programe

I need help with a short C++ program please!! www.computing.net/answers/programming/i-need-help-with-a-short-c-program-please/20280.html

Bike Trader C program www.computing.net/answers/programming/bike-trader-c-program/6557.html

Parsing text in a batch www.computing.net/answers/programming/parsing-text-in-a-batch/14235.html