Skip to content

spuhpointer/stack-vs-heap-benchmark

Repository files navigation

Stack VS Heap Benchmark

Overview

It is long standing debate which memory allocation technique is faster stack or heap. Heap allocation should take longer time because of fact that it requires to manage the pool of various size chunks in memory. Where stack does not requires any management, just change the stack pointer to reserve some memory block and that's it.

At this repository the benchmark is presented. Where code performs following actions:

  1. Allocate stack memory with out initializing it VS
  2. Allocate heap memory with out initializing it
  3. Allocate stack memory with initialization VS
  4. Allocate heap memory with initialization

Tests are performed in two modes: with GCC -O1 and -O2 optimizations.

Basic idea to have time benchmarks at certain memory chunk allocation (10x different sizes) in the loop. The measurement result is time taken for number of allocation attempts with different total chunk sizes.

Preparation

Project use github/endurox-dev/endurox standard library for plotting the results and measuring the time spent. Also R is used for generating pictures.

To install dependencies on Ubuntu quickly and run the benchmark do:

$ sudo apt-get install r-base
$ wget https://www.endurox.org/attachments/download/310/endurox-6.0.2-1.ubuntu18_04_GNU_epoll.x86_64_64.deb
$ sudo dpkg -i endurox-6.0.2-1.ubuntu18_04_GNU_epoll
$ git clone https://github.com/spuhpointer/stack-vs-heap-benchmark
$ cd stack-vs-heap-benchmark
$ ./build.sh

After wards see noinit1.png, filled1.png, noinit2.png, filled2.png.

Tests

Test machine is: Intel(R) Core(TM) i7-6600U CPU, Linux 64 bit, 4.15.0-50-generic, Spectre and Meltdown patches disabled.

Stack VS Heap - No INIT results -O1

Alt text

Stack VS Heap - With INIT results -O1

Alt text

Stack VS Heap - No INIT results -O2

Alt text

Stack VS Heap - With INIT results -O2

Alt text

Results

In the results we see that in case of no-init, stack is signficantly faster, but needs to take in account that loops are about 10M where in each position 10 allocations are made.

In case of memory init (e.g. software allocates some memory block and fills with some data), there is no significant difference in these two approaches).

Thus when writing software that actually uses the memory allocated, there is question are there real need for stack allocation approach?

About

This benchmark seeks to find out how significant faster stack allocation is versus heap

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published