#include <iostream>
#include <vector>
#include <chrono>
#include <cstring>
const int SIZE = 1024*1024;
void WithArray()
{
using namespace std;
char* a = new char[SIZE];
auto b = &a[0];
auto e = &a[SIZE];
memset(a, 0, sizeof a);
int total=0;
auto begin = chrono::system_clock::now();
for(int c=0; c<100; c++) {
for(auto p=b; p<e;) {
total += *p++;
}
}
auto end = chrono::system_clock::now();
cout << total << ":" << chrono::duration_cast<chrono::milliseconds>(end-begin).count() << endl;
delete[] a;
}
void WithVector()
{
using namespace std;
vector<char> a(SIZE);
auto b = a.begin();
auto e = a.end();
int total=0;
auto begin = chrono::system_clock::now();
for(int c=0; c<100; c++) {
for(auto it=b; it<e;) {
total += *it++;
}
}
auto end = chrono::system_clock::now();
cout << total << ":" << chrono::duration_cast<chrono::milliseconds>(end-begin).count() << endl;
}
int main()
{
WithArray();
WithVector();
WithArray();
WithVector();
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8Y2hyb25vPgojaW5jbHVkZSA8Y3N0cmluZz4KCmNvbnN0IGludCBTSVpFID0gMTAyNCoxMDI0OwoKdm9pZCBXaXRoQXJyYXkoKQp7Cgl1c2luZyBuYW1lc3BhY2Ugc3RkOwoJY2hhciogYSA9IG5ldyBjaGFyW1NJWkVdOwoJYXV0byBiID0gJmFbMF07CglhdXRvIGUgPSAmYVtTSVpFXTsKCgltZW1zZXQoYSwgMCwgc2l6ZW9mIGEpOwoJaW50IHRvdGFsPTA7CgkKCWF1dG8gYmVnaW4gPSBjaHJvbm86OnN5c3RlbV9jbG9jazo6bm93KCk7Cglmb3IoaW50IGM9MDsgYzwxMDA7IGMrKykgewoJCWZvcihhdXRvIHA9YjsgcDxlOykgewoJCQl0b3RhbCArPSAqcCsrOwoJCX0KCX0KCWF1dG8gZW5kICA9IGNocm9ubzo6c3lzdGVtX2Nsb2NrOjpub3coKTsKCWNvdXQgPDwgdG90YWwgPDwgIjoiIDw8IGNocm9ubzo6ZHVy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