fork(3) download
  1. #include <iostream>
  2. #include <chrono>
  3. using namespace std;
  4.  
  5. int main() {
  6.  
  7. double a[10000],b[10000];
  8. for(int i=0;i<10000;i++){
  9. b[i]=(double)(rand()+1)/(double)(RAND_MAX+1);
  10. a[i]=b[i];
  11. }
  12. auto start = chrono::steady_clock::now();
  13.  
  14. int x1=0,x2=0,i1,index;
  15. for ( i1 = 1; i1 <= 10000; i1++){
  16. for ( index = 0; index < 10000; index++){
  17.  
  18.  
  19. x1++;
  20. if(x1>=100){
  21. x1=0;
  22. x2++;
  23. }
  24.  
  25. if (x1 > 10)
  26. {
  27. a[index] += a[index - 10*1];
  28. }
  29. if (x2 < 95)
  30. {
  31. a[index] += a[index + 5*100];
  32. }
  33. }
  34. }
  35. auto end = chrono::steady_clock::now();
  36. auto diff = end - start;
  37.  
  38. cout << "method 8: " << chrono::duration <double, nano> (diff).count() << " ns" << endl;
  39.  
  40.  
  41. double temp;
  42. for(int i=0;i<10000;i++){
  43. temp=a[i];
  44. a[i]=b[i];
  45. b[i]=temp;
  46. }
  47.  
  48. start = chrono::steady_clock::now();
  49. for ( i1 = 1; i1 <= 10000; i1++){
  50. index=-1;
  51.  
  52. for ( x2 = 0; x2 < 100; x2++){
  53. for ( x1 = 0; x1 < 100; x1++){
  54.  
  55. index++;
  56.  
  57. if (x1 > 10){
  58. a[index] += a[index - 10*1];
  59. }
  60. if (x2 < 95){
  61. a[index] += a[index + 5*100];
  62. }
  63. }
  64. }
  65. }
  66. end = chrono::steady_clock::now();
  67. diff = end - start;
  68.  
  69. cout << "method 6 " << chrono::duration <double, nano> (diff).count() << " ns" << endl;
  70. return 0;
  71. }
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
method 8: 462 ns
method 6 297 ns