Advertisement
SpandITime

4thr.cpp

Jan 9th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.30 KB | None | 0 0
  1. #include <thread>
  2. #include <sys/time.h>
  3. #include <stdio.h>
  4.  
  5.  
  6. void Thr1(){
  7.     float num;
  8.     for(unsigned int i=0;i<25000U;i++){
  9.         num=1.00001f;
  10.         for(unsigned int j=0;j<1000000U;j++){
  11.             num*=1.00001f;
  12.         }
  13.     }
  14. }
  15. void Thr2(){
  16.     float num;
  17.     for(unsigned int i=0;i<25000U;i++){
  18.         num=1.00001f;
  19.         for(unsigned int j=0;j<1000000U;j++){
  20.             num*=1.00001f;
  21.         }
  22.     }
  23. }
  24. void Thr3(){
  25.     float num;
  26.     for(unsigned int i=0;i<25000U;i++){
  27.         num=1.00001f;
  28.         for(unsigned int j=0;j<1000000U;j++){
  29.             num*=1.00001f;
  30.         }
  31.     }
  32. }
  33. void Thr4(){
  34.     float num;
  35.     for(unsigned int i=0;i<25000U;i++){
  36.         num=1.00001f;
  37.         for(unsigned int j=0;j<1000000U;j++){
  38.             num*=1.00001f;
  39.         }
  40.     }
  41. }
  42. int main(){
  43.     timeval n,e;
  44.     double exec_time=0;
  45.     float num;
  46.     std::thread thr1,thr2,thr3,thr4;
  47.     gettimeofday(&n,NULL);
  48.  
  49.     thr1=std::thread(Thr1);
  50.     thr2=std::thread(Thr2);
  51.     thr3=std::thread(Thr3);
  52.     thr4=std::thread(Thr4);
  53.    
  54.     thr1.join();
  55.     thr2.join();
  56.     thr3.join();
  57.     thr4.join();
  58.    
  59.     gettimeofday(&e,NULL);
  60.     exec_time=((double)e.tv_sec-n.tv_sec)*1000.0f+((double)e.tv_usec-n.tv_usec)/1000.0f;
  61.     printf("%f\n",exec_time);
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement