Skip to content

Instantly share code, notes, and snippets.

@kespindler
Created April 27, 2012 18:33
Show Gist options
  • Save kespindler/2511605 to your computer and use it in GitHub Desktop.
Save kespindler/2511605 to your computer and use it in GitHub Desktop.
demonstrate difference between heap and stack alloc.
#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