fork(4) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <chrono>
  4. #include <cstring>
  5.  
  6. const int SIZE = 1024*1024;
  7.  
  8. void WithArray()
  9. {
  10. using namespace std;
  11. char* a = new char[SIZE];
  12. auto b = &a[0];
  13. auto e = &a[SIZE];
  14.  
  15. memset(a, 0, sizeof a);
  16. int total=0;
  17.  
  18. auto begin = chrono::system_clock::now();
  19. for(int c=0; c<100; c++) {
  20. for(auto p=b; p<e;) {
  21. total += *p++;
  22. }
  23. }
  24. auto end = chrono::system_clock::now();
  25. cout << total << ":" << chrono::duration_cast<chrono::milliseconds>(end-begin).count() << endl;
  26. delete[] a;
  27.  
  28. }
  29.  
  30. void WithVector()
  31. {
  32. using namespace std;
  33. vector<char> a(SIZE);
  34. auto b = a.begin();
  35. auto e = a.end();
  36. int total=0;
  37.  
  38. auto begin = chrono::system_clock::now();
  39. for(int c=0; c<100; c++) {
  40. for(auto it=b; it<e;) {
  41. total += *it++;
  42. }
  43. }
  44. auto end = chrono::system_clock::now();
  45. cout << total << ":" << chrono::duration_cast<chrono::milliseconds>(end-begin).count() << endl;
  46. }
  47.  
  48. int main()
  49. {
  50. WithArray();
  51. WithVector();
  52. WithArray();
  53. WithVector();
  54. }
  55.  
Success #stdin #stdout 0.57s 3300KB
stdin
Standard input is empty
stdout
0:137
0:147
0:137
0:150