I've been reading sites that talk about clock() but I don't really get an answer by doing that. I mean, I still need to confirm and know some more stuff. I hope you could help me...

Here's what i would like to know:
1] what does the value returned by clock() mean?
---system clock ticks? if yes, what exactly is system clock ticks
compared to wall clock/ wristwatch clock?


2] what does the value returned by CLOCKS_PER_SEC mean?
---is its value always 1000? and is it how much the clock() value increases in a second?


3] how does these two codes differ in how they function?
---from http://www.daniweb.com/forums/thread120452.html#

if (clock() > FiveSecondsLater)
      {
                // Do whatever it is you want to do.
      }

and this one

void wait (float seconds)
      {
      clock_t EndTime = seconds * CLOCKS_PER_SEC;
                while (EndTime > clock())
                {
                // this will loop until clock() equals EndTime
                }
      }

---i mean, aren't they the same wait function?


4] does the first code need a loop(outside) for it to become a wait function? and if yes, what condition shall i put to it?

im trying to make dots appear in succession
and im using codeblocks

from this code...

/* clock example: countdown */
#include <stdio.h>
#include <time.h>
  int n;
void wait ( int seconds )
{
  clock_t endwait;
  endwait = clock () + seconds * CLOCKS_PER_SEC ;
  while (clock() < endwait) {}
}

int main ()
{

  printf ("Starting countdown...\n");
  
  for (n=10; n>0; n--)
  {
    printf ("%d\n",n);
    wait (1);
  }
  
  printf ("FIRE!!!\n");
  return 0;
}

i came to this

void waiter(){
    int x=0;
    while(x<3){
    cout<<".";

        clock_t stopwait;
        stopwait = clock () + 1 * CLOCKS_PER_SEC ;
        while (clock() < stopwait) {}
    x++;
    }
}//end of waiter

the code works perfectly already but i dont know how the innermost "while" does.
i tried removing it since it does not contain codes between its braces, but doing so will alter the program~

Nick Evan commented: Excellent first post! +12

clock() returns the time in mili seconds, CLOCKS_PER_SEC is the conversion
factor from the value returned by clock() into seconds.

while (clock() < stopwait) {}

That code waits until the value returned by clock() is greater than
stopwait, meaning, it waits X amount of milliseconds, where x is stopwait.

thanks.
i figured it out too.
im just confused by the innermost while since its my first time seeing a while without contents.

clock() returns the time in mili seconds,

Not so. This returns the number of clock ticks since the last call to clock(). What this number means varies by hardware and os.

CLOCKS_PER_SEC is the conversion
factor from the value returned by clock() into seconds.

That is so. so:
double secs = clock() / (double)CLOCKS_PER_SEC;
will give you the time in seconds since the last call to clock.

thanks.
i figured it out too.
im just confused by the innermost while since its my first time seeing a while without contents.

This is basically what you call busy waiting. The loop will essentially do nothing but check the loop condition. Once the loop condition is false, the program can move on. In general, busy waiting is not a desirable way to wait for a condition to resolve. The problem is that, for long intervals, this loop will waste a lot of processor cycles to no purpose. If you are concerned about correctness, you would need to calculate how much time remains until the condition resolves and then sleep for that interval. Sleep tells the OS that the processor can be used for other computations until a certain time interval has passed. Unfortunately, sleep() and usleep() are not c++ standards, so you would have to choose a platform dependant method of non-busy waiting. For simple experimental cases, however, busy waiting will be fine. Just be sure that you don't give it too long of a waiting interval, because you will find your computer's performance on other processes tanks while the wait loop is executing.

<me> clock() returns the time in mili seconds
<you>This returns the number of clock ticks since the last call to clock().

I take that back, clock returns the number of clock ticks since the
program was launched, not since the last call to clock().

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.