Advertisement
llsumitll

calculate function time used.

Jul 10th, 2023
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.28 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <thread>
  3. #include <chrono> // this is required header file to use the high_resolution_clock class
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7. using namespace chrono;
  8.  
  9. typedef unsigned long long ull;
  10.  
  11.  ull oddSum = 0;
  12.  ull evenSum = 0;
  13.  
  14. void findEven(ull start, ull end)
  15. {
  16.     for (ull i = start ; i <= end; ++i)
  17.     {
  18.         if((i%2)== 0)
  19.         {
  20.             evenSum = evenSum + i;
  21.         }
  22.     }
  23. }
  24. void findOdd(ull start, ull end)
  25. {
  26.     for (ull i = start; i <= end ; ++i)
  27.         if(( i % 2) != 1)
  28.         {
  29.             oddSum = oddSum+i;
  30.         }
  31.    
  32. }
  33. int  main()
  34. {
  35.     ull start = 0; ull end = 1900000000;
  36.    
  37.     auto startime = high_resolution_clock::now(); // your time start now.
  38.     findEven(start, end);
  39.     findOdd(start, end);
  40.     auto stoptime = high_resolution_clock::now(); // Time up .
  41.    
  42.     auto duration = duration_cast<microseconds>(stoptime - startime); // total time in microseconds
  43.    
  44.     cout << "Odd Sum : " << oddSum << endl;
  45.     cout << "even sum : " << evenSum << endl;
  46.     cout <<"Total time taken : " <<  duration.count()/1000000 << endl;
  47.    
  48.     return 0;
  49. }
  50.  
  51.  
  52. /*
  53. output :
  54. Odd Sum : 902500000950000000
  55. even sum : 902500000950000000
  56. Total time taken : 8
  57. Program ended with exit code: 0
  58. */
Tags: cpp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement