Skip to content

Instantly share code, notes, and snippets.

@tuoxie007
Last active June 12, 2020 04:09
Show Gist options
  • Save tuoxie007/b9d1212bf5d3f52e26d0928a6cab099c to your computer and use it in GitHub Desktop.
Save tuoxie007/b9d1212bf5d3f52e26d0928a6cab099c to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
// compile without optimization
// clang++ -std=c++17 -stdlib=libc++ -O0 test_alloc.cpp
// elapse 6.25 seconds with N=100*1000*1000
size_t test_alloc(size_t N) {
size_t r;
while (N--) {
auto p = new size_t(N); // malloc and init
r = *p; // read once
delete p; // dealloc
}
return N;
}
// elapse 0.17 seconds with N=100*1000*1000
size_t test_stack(size_t N) {
size_t r;
while (N--) {
size_t a = N; // define and init
r = a; // read once
}
return r;
}
int main() {
return test_alloc(1000*1000*100);
return test_stack(1000*1000*100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment