Created
April 27, 2012 18:33
-
-
Save kespindler/2511605 to your computer and use it in GitHub Desktop.
demonstrate difference between heap and stack alloc.
This file contains hidden or 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 <ctime> | |
#include <iostream> | |
namespace { | |
class empty { }; // even empty classes take up 1 byte of space, minimum | |
} | |
int main() | |
{ | |
std::clock_t start = std::clock(); | |
for (int i = 0; i < 100000; ++i) | |
empty e; | |
std::clock_t duration = std::clock() - start; | |
std::cout << "stack allocation took " << duration << " clock ticks\n"; | |
start = std::clock(); | |
for (int i = 0; i < 100000; ++i) { | |
empty* e = new empty; | |
delete e; | |
}; | |
duration = std::clock() - start; | |
std::cout << "heap allocation took " << duration << " clock ticks\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment