fork(3) 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, SIZE);
  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. void WithVector()
  30. {
  31. using namespace std;
  32. vector<char> a(SIZE);
  33. auto b = a.begin();
  34. auto e = a.end();
  35. int total=0;
  36.  
  37. auto begin = chrono::system_clock::now();
  38. for(int c=0; c<100; c++) {
  39. for(auto it=b; it<e;) {
  40. total += *it++;
  41. }
  42. }
  43. auto end = chrono::system_clock::now();
  44. cout << total << ":" << chrono::duration_cast<chrono::milliseconds>(end-begin).count() << endl;
  45. }
  46.  
  47. int main()
  48. {
  49. WithArray();
  50. WithVector();
  51. WithArray();
  52. WithVector();
  53. }
  54.  
Success #stdin #stdout 0.51s 3300KB
stdin
Standard input is empty
stdout
0:107
0:146
0:99
0:156