1

I have used the following code to calculate the exection time of the searching function in milliseconds.

sBegin = clock();
searching();
sEnd = clock();
searchingTime = ((float)(sEnd-sBegin)/CLOCKS_PER_SEC)*1000;

The result is for example 952.000000. I want to know why all the digits after the decimal are always zero? How can I solve it? Is there a better way to calculate the execution time in C language?

2 Answers2

1

You're doing integer division, try

    searchingTime = ((sEnd-sBegin)/(float)CLOCKS_PER_SEC)*1000;

There are other places you can place the cast and get the same thing, this is just one possibility

EDIT:

Here, check out the questions here and here. I think these may answer your question (basically it doesn't look like clock() is going to cut it). My answer above is just wrong.

Community
  • 1
  • 1
SirGuy
  • 10,660
  • 2
  • 36
  • 66
  • I don't want to get the same thing. I want to have digits after decimal. The digits after the decimal in my program are always zero. –  Apr 03 '12 at 15:14
  • I meant the same thing as my example, not the same answer as when you're doing integer division – SirGuy Apr 03 '12 at 15:16
  • @GuyGreer: This is not the cause (see corrected question). Anyway, 952 is not a multiple of 1000. – undur_gongor Apr 03 '12 at 15:45
  • @undur_gongor: I put the missing one in the right place, but it did not cause a float division. –  Apr 03 '12 at 15:48
  • @GHAZALRAHBARI: You are already doing a float division. This is not the issue. Did you read Kerrek SB's comment? – undur_gongor Apr 03 '12 at 15:56
0

See Kerrek SB's comment:

It depends on the resolution of clock. If it returns values with an accuracy of 1 ms, you cannot measure differences finer than 1ms. This seems to be the case on your system.

There is no better portable way to measure times. Maybe your platform provides specific functions to get the system time with a better resolution.

Community
  • 1
  • 1
undur_gongor
  • 15,657
  • 5
  • 63
  • 75