C ++ function clock () gives invalid values

I tried to program the Timer class (not suspecting that boost was one), and then when this did not work, I tried to simply output the clock () value using this code:

#include <ctime>
#include <iostream>

int main()
{
 for(int i = 0; i < 50; ++i)
 {
  std::cout << std::clock() << " ";
 }
 return 0;
}

When I run the program, I get a series of 0. I have a similar experience in using the functions of a sleeping thread in a thread to stretch the time longer (although after a few seconds it jumps from 0 to 10,000 and continues to output 10,000).

I am running Gentoo Linux. Is this a platform? C ++ thing? What's happening?

: , 10000 , . , , 10000. , . , , ()? ( , , ?)

+3
4

clock() . , clock(), . , , 10 . , clock() - , . , , , , ​​- , .

+5

std::clock . 10ms. , .

0

:

#include <ctime>
#include <iostream>

int main()
{
   for(int i = 0; i < 50; ++i)
   {
      for (int j = 0; j < 500; ++j )
      {
         std::cout << j << " ";
      }
      std::cout << std::endl;
      std::cout << std::clock() << std::endl;
   }
   std::cout << std::endl;
   return 0;
}     

clock(), 0, - 10000. 10000 . std:: cout , clock() 0 . , clock() 10000.

j sqrt(), clock() 50000, 10000.

0

in my 64-bit operating system, the speed of CLOCKS_PER_SEC is 1000. The clock values ​​come in milliseconds. The ideal time will be extracted from the code below.

int main(){

clock_t a,b;
int c,d,e,f;
c=clock();
scanf("%d",&e);
d=clock();
f=(d-c)/CLOCKS_PER_SECOND;
printf("timing is %d seconds",f);/*for 64 bit operating system         
CLOCKS_PER_SECOND is 1000*/




}
0
source

Source: https://habr.com/ru/post/1762210/


All Articles