Skip to content

Instantly share code, notes, and snippets.

@cgaebel
Created December 22, 2013 06:11
Show Gist options
  • Select an option

  • Save cgaebel/8079012 to your computer and use it in GitHub Desktop.

Select an option

Save cgaebel/8079012 to your computer and use it in GitHub Desktop.
test the speed of malloc and free
// To run, copy the below code into a .sh file and execute it.
//
// > #!/usr/bin/env bash
// > clang++ -std=c++11 -O3 alloctest.cpp -o alloctest
// > ./alloctest
// > rm alloctest
#include <stdio.h>
#include <time.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <utility>
#include <random>
#define NUM_ALLOCATIONS 10000000
#define MAX_MEM_SIZE (10*1024*1024)
static struct timespec gettime()
{
struct timespec ret;
clock_gettime(CLOCK_MONOTONIC_RAW, &ret);
return ret;
}
static uint64_t tdiff(struct timespec t2, struct timespec t1)
{
return 1000000000ULL * (uint64_t)(t2.tv_sec - t1.tv_sec)
+ (uint64_t)(t2.tv_nsec - t1.tv_nsec);
}
static double avgvec(const std::vector<uint64_t>& vec)
{
if(vec.size() == 0)
return 0;
double avg = vec[0];
for(size_t i = 1; i < vec.size(); ++i)
{
double di = (double)i;
double k = di/(di + 1);
avg = avg*k + vec[i]*(1-k);
}
return avg;
}
int main()
{
std::vector<void*> allocated_mem;
std::mt19937_64 rng;
std::vector<uint64_t> alloctimes;
std::vector<uint64_t> freetimes;
std::uniform_int_distribution<size_t> memsizedist(0, MAX_MEM_SIZE);
allocated_mem.reserve(NUM_ALLOCATIONS);
for(int i = 0; i < NUM_ALLOCATIONS; ++i)
{
bool should_allocate = (bool)(rng() & 1);
if(should_allocate || allocated_mem.size() == 0)
{
size_t size = memsizedist(rng);
struct timespec end, start = gettime();
void* mem = malloc(size);
end = gettime();
alloctimes.push_back(tdiff(end, start));
allocated_mem.push_back(mem);
}
else
{
size_t lastloc = allocated_mem.size() - 1;
std::uniform_int_distribution<size_t> freedist(0, lastloc);
std::swap(allocated_mem[freedist(rng)], allocated_mem[lastloc]);
void* tofree = allocated_mem[lastloc];
struct timespec end, start = gettime();
free(tofree);
end = gettime();
freetimes.push_back(tdiff(end, start));
allocated_mem.pop_back();
}
}
for(auto i = allocated_mem.begin(); i != allocated_mem.end(); ++i)
{
void* tofree = *i;
struct timespec end, start = gettime();
free(tofree);
end = gettime();
freetimes.push_back(tdiff(end, start));
}
auto minmaxalloc = std::minmax_element(alloctimes.begin(), alloctimes.end());
auto minmaxfree = std::minmax_element(freetimes.begin(), freetimes.end());
double avgalloc = avgvec(alloctimes);
double avgfree = avgvec(freetimes);
printf("alloc:\n");
printf(" min: %lu\n", *minmaxalloc.first);
printf(" max: %lu\n", *minmaxalloc.second);
printf(" avg: %lf\n", avgalloc);
printf("\n");
printf("free:\n");
printf(" min: %lu\n", *minmaxfree.first);
printf(" max: %lu\n", *minmaxfree.second);
printf(" avg: %lf\n", avgfree);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment