fork download
  1. #include <bits/stdc++.h>
  2. #include <thread>
  3. using namespace std;
  4. using namespace std::chrono;
  5.  
  6.  
  7. int oddSum=0;
  8. int evenSum=0;
  9.  
  10.  
  11. void findOddSum(int start,int end){
  12. for(int i=start;i<=end;i++){
  13. if(i%2)
  14. oddSum+=i;
  15. }
  16. }
  17.  
  18. void findEvenSum(int start,int end){
  19. for(int i=start;i<=end;i++){
  20. if(i%2==0)
  21. evenSum+=i;
  22. }
  23. }
  24.  
  25.  
  26. int32_t main(){
  27.  
  28. auto startTime=high_resolution_clock::now();
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44. thread t1(findEvenSum,1,1e9);
  45. thread t2(findOddSum,1,1e9);
  46.  
  47. t1.join();
  48. t2.join();
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. auto stopTime=high_resolution_clock::now();
  71. auto duration=duration_cast<microseconds>(stopTime-startTime);
  72.  
  73. cout<<"Seconds : "<<duration.count()/1e6<<endl;
  74.  
  75.  
  76.  
  77. }
  78.  
  79.  
  80.  
Success #stdin #stdout 1.51s 5380KB
stdin
Standard input is empty
stdout
Seconds : 1.5055