fork(1) download
  1. #include <iostream>
  2. #include <chrono>
  3.  
  4. using namespace std;
  5.  
  6. double calc1(const int* a, const int* b) {
  7.  
  8. auto start = chrono::system_clock::now();
  9.  
  10. int* c = new int[10000000];
  11.  
  12. for (int i = 0; i < 10000000; i++)
  13. c[i] = a[i] + b[i];
  14.  
  15. delete[] c;
  16.  
  17. return chrono::duration<double>( chrono::system_clock::now() -start).count();
  18. }
  19.  
  20. double calc2(const int*a, const int*b) {
  21.  
  22. auto start = chrono::system_clock::now();
  23.  
  24. int* c = new int[10000000];
  25.  
  26. for (int i = 0; i < 10000000; i+=4){
  27.  
  28. auto i0 = i;
  29. auto i1 = i+1;
  30. auto i2 = i+2;
  31. auto i3 = i+3;
  32.  
  33. c[i0] = a[i0] + b[i0];
  34. c[i1] = a[i1] + b[i1];
  35. c[i2] = a[i2] + b[i2];
  36. c[i3] = a[i3] + b[i3];
  37. }
  38.  
  39. delete[] c;
  40.  
  41. return chrono::duration<double>(chrono::system_clock::now() - start).count();
  42.  
  43. }
  44.  
  45. int main() {
  46. // your code goes here
  47. int* a = new int[10000000];
  48. int* b = new int[10000000];
  49.  
  50. std::cout << calc1(a, b) << '\n' << calc2(a, b);
  51.  
  52. delete[] a;
  53. delete[] b;
  54. }
Success #stdin #stdout 0.03s 15240KB
stdin
Standard input is empty
stdout
0.0125982
0.018405