c - Difference between clock_t and time_t or time(NULL) and clock() -


I just write AC programs, which prints the integer value as a practice every single second, this program After the wait of one prints the integer second before printing the next integer so that it takes 5 seconds to complete the program execution. I am using clock_t and time () as the timer and it works but when I Clock_t and clock This does not work as a timer.

I know that time_t and time () is full time and using the remaining seconds of 1970. Use the time since running clock_t and clock () programs.

This is the first code using time_t and time (null)

  #include  Int main (zero) {int i; INT = 1; Time_t start, end; Start = time (zero); For (i = 1; i & lt; = 5; i ++) {time_t wait = seconds + time (zero); While (Time (NULL) & lt; wait) {} printf ("% d \ n", i); } End = time (zero); Printf ("The program takes% d seconds \ n", (unsigned integer) FIFTIM (end, start)); Return 0; }  

And this is when the program is started:

  1 2 3 4 5 takes 5 seconds to program   

program, or wait a second before printing the next integer

use this clock_t and clock The second program is doing ()

  #include & lt; Stdio.h & gt; # Include & lt; Time.h> Int main (zero) {int sec = 1; Int i; Clock_t start, end; Start = clock (); For (i = 1; i & lt; = 5; i ++) {clock_t wait = seconds * CLOCKS_PER_SEC; While (clock ()  

The result of the second program using this clock_t and clock ()

  1 The 2 3 4 5 program takes 1 99 801 seconds and after execution it does not print at the other one, but wait for the first time for a second and after that it prints all integers instead of waiting. One second before printing the next integer.  

Please tell about another program and what is the difference between time () and clock () function?

In your first program, it will:

  time_t wait = sec + Time (zero);  

The wait for to change each time through the loop, causes a second to be composed of the current time (on the system where Time_t is measured in seconds, at least, which is normal, but not required).

In your other program, it will:

  clock_t wait = seconds * CLOCKS_PER_SEC;  

Once you execute it, then the value is the same, because the value of seconds never changes, in its other program, the loop You will wait for the first repetition until the start of the first repetitive program, and will not wait for subsequent iterations again, because more than one second program starts and clock ();

In short, your two ends are not really close to doing the same thing, so there is no reason that you are expecting them to behave in the same way. If you have changed:

  clock_t wait = seconds * CLOCKS_PER_SEC;  

to:

  clock_t wait = i * CLOCKS_PER_SEC;  

Then you can find something near what you are expecting.


Comments

Popular posts from this blog

winforms - C# Form - Property Change -

java - Algorithm negotiation fail SSH in Jenkins -

java - Messages from .properties file do not display UTF-8 characters -