fork download
  1. #include <iostream>
  2. #include <ctime>
  3. #include <cstdlib>
  4.  
  5. using namespace std;
  6.  
  7. #define N 200
  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<5; ++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 0.06s 3864KB
stdin
Standard input is empty
stdout
1: 7442
2: 5318
1: 6310
2: 5513
1: 6015
2: 5223
1: 5880
2: 5252
1: 6130
2: 5751

Results: 31777, 27057