fork(2) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. struct timeit {
  5. decltype(chrono::high_resolution_clock::now()) begin;
  6. const string label;
  7. timeit(string label = "???") : label(label) { begin = chrono::high_resolution_clock::now(); }
  8. ~timeit() {
  9. auto end = chrono::high_resolution_clock::now();
  10. auto duration = chrono::duration_cast<chrono::milliseconds>(end - begin).count();
  11. cerr << duration << "ms elapsed [" << label << "]" << endl;
  12. }
  13. };
  14. const int MAXN = 1<<8;
  15. int i, j, a[MAXN][MAXN], rows[MAXN], cols[MAXN];
  16. const int NUMITERS = 10000;
  17. int main() {
  18. for (int i=0; i<MAXN; i++) {
  19. rows[i] = i;
  20. cols[i] = i;
  21. }
  22. {
  23. timeit timer("blocked");
  24. int n = MAXN;
  25. int B = 8;
  26. for (int t = 0; t < NUMITERS; t++) {
  27. for (int i = 0; i < n; i += B) {
  28. for (int j = 0; j < n; j += B) {
  29. for (int x = i; x < i + B; x++) {
  30. for (int y = j; y < j + B; y++) {
  31. a[x][y] = rows[x] * cols[y];
  32. }
  33. }
  34. }
  35. }
  36. }
  37. cout<<a[20][20]<<endl;
  38. }
  39. {
  40. timeit x("normal");
  41. int n = MAXN;
  42. for (int t = 0; t < NUMITERS; t++) {
  43. for (i = 0; i < MAXN; i++) {
  44. for (j = 0; j < MAXN; j++) {
  45. a[i][j] = rows[i]*cols[j];
  46. }
  47. }
  48. }
  49. cout<<a[20][20]<<endl;
  50. }
  51. }
  52.  
Success #stdin #stdout #stderr 0.99s 4384KB
stdin
Standard input is empty
stdout
400
400
stderr
600ms elapsed [blocked]
386ms elapsed [normal]