fork download
  1. #include <iostream>
  2. #include <thread>
  3. #include <chrono>
  4. #include <algorithm>
  5. using namespace std;
  6. using namespace std::chrono;
  7. typedef long long int ull;
  8.  
  9. void findEven(ull start, ull end, ull* EvenSum) {
  10. for (ull i = start; i <= end; ++i){
  11. if (!(i & 1)){
  12. *(EvenSum) += i;
  13. }
  14. }
  15. }
  16.  
  17. void findOdd(ull start, ull end, ull* OddSum) {
  18. for (ull i = start; i <= end; ++i){
  19. if (i & 1){
  20. (*OddSum) += i;
  21. }
  22. }
  23. }
  24.  
  25. int main() {
  26.  
  27. ull start = 0, end = 1900000000;
  28.  
  29. ull OddSum = 0;
  30. ull EvenSum = 0;
  31.  
  32. auto startTime = high_resolution_clock::now();
  33.  
  34. // // WITH THREAD
  35. std::thread t1(findEven, start, end, &(EvenSum));
  36. std::thread t2(findOdd, start, end, &(OddSum));
  37.  
  38. t1.join();
  39. t2.join();
  40.  
  41. // // WITHOUT THREAD
  42. // findEven(start,end, &EvenSum);
  43. // findOdd(start, end, &OddSum);
  44. auto stopTime = high_resolution_clock::now();
  45. auto duration = duration_cast<microseconds>(stopTime - startTime);
  46.  
  47. cout << "OddSum : " << OddSum << endl;
  48. cout << "EvenSum : " << EvenSum << endl;
  49.  
  50. cout << "Sec: " << duration.count()/1000000 << endl;
  51.  
  52. return 0;
  53. }
Success #stdin #stdout 3.31s 5276KB
stdin
Standard input is empty
stdout
OddSum : 902500000000000000
EvenSum : 902500000950000000
Sec: 3