fork download
  1. #include <iostream>
  2. #include <ctime>
  3. #include <cstdlib>
  4.  
  5. using namespace std;
  6.  
  7. #define N 500
  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. int main()
  34. {
  35. int **a = new int*[N];
  36. int **b = new int*[N];
  37. int **c = new int*[N];
  38.  
  39. for (unsigned q=0; q<N; ++q)
  40. {
  41. a[q] = new int[N];
  42. b[q] = new int[N];
  43. c[q] = new int[N];
  44.  
  45. for (unsigned w=0; w<N; ++w)
  46. {
  47. a[q][w] = rand();
  48. b[q][w] = rand();
  49. }
  50. }
  51.  
  52. long m1=0, m2=0;
  53.  
  54. for (unsigned q=0; q<10; ++q)
  55. {
  56. long t = clock();
  57. mult1(a, b, c);
  58. m1 += t = clock() - t;
  59. cout << "1: " << t << endl;
  60.  
  61. t = clock();
  62. mult2(a, b, c);
  63. m2 += t = clock() - t;
  64. cout << "2: " << t << endl;
  65. }
  66.  
  67. cout << endl << "Results: " << m1 << ", " << m2 << endl;
  68.  
  69. return 0;
  70. }
Success #stdin #stdout 1.86s 6372KB
stdin
Standard input is empty
stdout
1: 107045
2: 78591
1: 106390
2: 78578
1: 106600
2: 78721
1: 106352
2: 78212
1: 106978
2: 78166
1: 106401
2: 78655
1: 106400
2: 77978
1: 106432
2: 78071
1: 106269
2: 78073
1: 106523
2: 77941

Results: 1065390, 782986