Last active
June 12, 2020 04:09
-
-
Save tuoxie007/b9d1212bf5d3f52e26d0928a6cab099c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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