fork download
  1. #include <time.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. int main() {
  6. // create and init array
  7. const size_t arr_size = 50000000;
  8. double *arr = (double*)malloc(arr_size * sizeof(double));
  9. size_t i, k;
  10. for (i = 0; i < arr_size; ++i)
  11. arr[i] = 10;
  12.  
  13. double result = 0;
  14. clock_t start, end;
  15. const int c_iterations = 10; // iterations of experiment
  16. float faster_avg = 0;
  17. // -----------------------------------------------------------------
  18.  
  19.  
  20. for (k = 0; k < c_iterations; ++k) {
  21. result = 0;
  22.  
  23. // Sequential
  24. start = clock();
  25.  
  26. for (i = 0; i < arr_size; i += 8) {
  27. //result += arr[i + 0] + arr[i + 1] + arr[i + 2] + arr[i + 3] + arr[i + 4] + arr[i + 5] + arr[i + 6] + arr[i + 7];
  28.  
  29. result += arr[i + 0];
  30. result += arr[i + 1];
  31. result += arr[i + 2];
  32. result += arr[i + 3];
  33. result += arr[i + 4];
  34. result += arr[i + 5];
  35. result += arr[i + 6];
  36. result += arr[i + 7];
  37. }
  38.  
  39. end = clock();
  40. const float c_time_seq = (float)(end - start) / CLOCKS_PER_SEC;
  41. printf("seq: %f sec, res: %u, ", c_time_seq, result);
  42. // -----------------------------------------------------------------
  43.  
  44. result = 0;
  45.  
  46. // IPL-optimization
  47. start = clock();
  48.  
  49. register double v0, v1, v2, v3, v4, v5, v6, v7;
  50. v0 = v1 = v2 = v3 = v4 = v5 = v6 = v7 = 0;
  51.  
  52. for (i = 0; i < arr_size; i += 8) {
  53. v0 += arr[i + 0];
  54. v1 += arr[i + 1];
  55. v2 += arr[i + 2];
  56. v3 += arr[i + 3];
  57. v4 += arr[i + 4];
  58. v5 += arr[i + 5];
  59. v6 += arr[i + 6];
  60. v7 += arr[i + 7];
  61. }
  62. result = v0 + v1 + v2 + v3 + v4 + v5 + v6 + v7;
  63.  
  64.  
  65. end = clock();
  66. const float c_time_ipl = (float)(end - start) / CLOCKS_PER_SEC;
  67. const float c_faster = c_time_seq / c_time_ipl;
  68.  
  69. printf("ipl: %f sec, faster %f X, res: %u \n", c_time_ipl, c_faster, result);
  70. faster_avg += c_faster;
  71. }
  72.  
  73. faster_avg = faster_avg / c_iterations;
  74. printf("faster AVG: %f \n", faster_avg);
  75.  
  76.  
  77. return 0;
  78. }
Success #stdin #stdout 2.21s 392896KB
stdin
Standard input is empty
stdout
seq: 0.100000 sec, res: 0, ipl: 0.090000 sec, faster 1.111111 X, res: 0 
seq: 0.100000 sec, res: 0, ipl: 0.090000 sec, faster 1.111111 X, res: 0 
seq: 0.100000 sec, res: 0, ipl: 0.090000 sec, faster 1.111111 X, res: 0 
seq: 0.100000 sec, res: 0, ipl: 0.100000 sec, faster 1.000000 X, res: 0 
seq: 0.100000 sec, res: 0, ipl: 0.090000 sec, faster 1.111111 X, res: 0 
seq: 0.100000 sec, res: 0, ipl: 0.090000 sec, faster 1.111111 X, res: 0 
seq: 0.100000 sec, res: 0, ipl: 0.090000 sec, faster 1.111111 X, res: 0 
seq: 0.110000 sec, res: 0, ipl: 0.080000 sec, faster 1.375000 X, res: 0 
seq: 0.100000 sec, res: 0, ipl: 0.090000 sec, faster 1.111111 X, res: 0 
seq: 0.110000 sec, res: 0, ipl: 0.080000 sec, faster 1.375000 X, res: 0 
faster AVG: 1.152778