fork download
  1. #include <iostream>
  2. #include <ctime>
  3. #include <cstdlib>
  4.  
  5. using namespace std;
  6.  
  7. #define N 100
  8.  
  9. void mult1(int **a, int **b, int **c)
  10. {
  11. for (unsigned q=0; q<N; ++q)
  12. for (unsigned w=0; w<N; ++w)
  13. c[q][w] = 0;
  14.  
  15. for (unsigned q=0; q<N; ++q)
  16. for (unsigned w=0; w<N; ++w)
  17. for (unsigned e=0; e<N; ++e)
  18. c[q][w] += a[q][e] * b[e][w];
  19. }
  20.  
  21. void mult2(int **a, int **b, int **c)
  22. {
  23. for (unsigned q=0; q<N; ++q)
  24. for (unsigned w=0; w<N; ++w)
  25. c[q][w] = 0;
  26.  
  27. for (unsigned q=0; q<N; ++q)
  28. for (unsigned w=0; w<N; ++w)
  29. for (unsigned e=0; e<N; ++e)
  30. c[q][w] += a[q][e] * b[w][e];
  31. }
  32.  
  33. void mult3(int **a, int **b, int **c)
  34. {
  35. for (unsigned q=0; q<N; ++q)
  36. for (unsigned w=0; w<N; ++w)
  37. c[q][w] = 0;
  38.  
  39. for (unsigned q=0; q<N; ++q)
  40. {
  41. int *aa = a[q];
  42. for (unsigned w=0; w<N; ++w)
  43. {
  44. int *bb = b[q];
  45. for (unsigned e=0; e<N; ++e)
  46. c[q][w] += aa[e] * bb[e];
  47. }
  48. }
  49. }
  50.  
  51. int main()
  52. {
  53. int **a = new int*[N];
  54. int **b = new int*[N];
  55. int **c = new int*[N];
  56.  
  57. for (unsigned q=0; q<N; ++q)
  58. {
  59. a[q] = new int[N];
  60. b[q] = new int[N];
  61. c[q] = new int[N];
  62.  
  63. for (unsigned w=0; w<N; ++w)
  64. {
  65. a[q][w] = rand();
  66. b[q][w] = rand();
  67. }
  68. }
  69.  
  70. long m1=0, m2=0, m3=0;
  71.  
  72. for (unsigned q=0; q<10; ++q)
  73. {
  74. long t = clock();
  75. mult1(a, b, c);
  76. m1 += t = clock() - t;
  77. cout << "1: " << t << endl;
  78.  
  79. t = clock();
  80. mult2(a, b, c);
  81. m2 += t = clock() - t;
  82. cout << "2: " << t << endl;
  83.  
  84. t = clock();
  85. mult3(a, b, c);
  86. m3 += t = clock() - t;
  87. cout << "3: " << t << endl;
  88. }
  89.  
  90. cout << endl << "Results: " << m1 << ", " << m2 << ", " << m3 << endl;
  91.  
  92. return 0;
  93. }
Success #stdin #stdout 0.02s 3472KB
stdin
Standard input is empty
stdout
1: 822
2: 769
3: 787
1: 816
2: 774
3: 785
1: 814
2: 713
3: 687
1: 720
2: 681
3: 743
1: 788
2: 661
3: 658
1: 700
2: 661
3: 705
1: 743
2: 674
3: 659
1: 740
2: 661
3: 669
1: 769
2: 662
3: 668
1: 699
2: 661
3: 732

Results: 7611, 6917, 7093