Skip to content

Instantly share code, notes, and snippets.

@ssarangi
Created April 16, 2014 05:54
Show Gist options
  • Save ssarangi/10813424 to your computer and use it in GitHub Desktop.
Save ssarangi/10813424 to your computer and use it in GitHub Desktop.
#pragma once
#if TC_MALLOC == 1
#if defined _DEBUG
# pragma comment(lib, "libtcmalloc_minimal-debug.lib")
#else
# pragma comment(lib, "libtcmalloc_minimal.lib")
#endif
#endif
#define TC_MALLOC 1
#define ELIBEN 0
#include "hr_timer.h"
#include "allocators.h"
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <thread>
#include <vector>
#if ELIBEN == 1
#include "memmgr.h"
#define malloc memmgr_alloc
#define free memmgr_free
#else
#define memmgr_print_stats() {}
#define memmgr_init() {}
#endif
#define MAX_THREADS 1024;
#define KB 1024
#define MB 1024 * KB
void* allocateMem(int mem_size)
{
void* p = (void *)malloc(mem_size);
return p;
}
void freeMem(void* mem)
{
free(mem);
}
void allocateAndFreeMem(int mem_size, bool measureTime)
{
CStopWatch stopWatch;
if (measureTime)
stopWatch.startTimer();
void *p = allocateMem(mem_size);
freeMem(p);
if (measureTime)
{
stopWatch.stopTimer();
}
}
void allocateAndFreeMemMultipleTimes(int iteration, int mem_size)
{
for (int i = 0; i < iteration; ++i)
{
allocateAndFreeMem(mem_size, false);
}
}
double allocateAndFreeMemInLoop(int iterations, int mem_size)
{
// Measure the per allocation & free times as well as total.
CStopWatch stopWatch;
stopWatch.startTimer();
for (int i = 0; i < iterations; ++i)
{
allocateAndFreeMem(mem_size, true);
}
stopWatch.stopTimer();
stopWatch.printTimer();
return stopWatch.getElapsedTime();
}
double MTAllocateAndFreeMemInLoop(int iterations, int mem_size)
{
CStopWatch stopWatch;
std::thread myThreads[1024];
stopWatch.startTimer();
std::vector<std::thread> threads;
for (int tn = 0; tn < iterations; tn++)
{
threads.emplace_back(allocateAndFreeMemMultipleTimes, 100, mem_size);
}
for (auto &_thread : threads)
{
_thread.join();
}
stopWatch.stopTimer();
stopWatch.printTimer();
return stopWatch.getElapsedTime();
}
int main()
{
memmgr_init();
#if TC_MALLOC == 1
std::cout << "Using TCMalloc" << std::endl;
#elif ELIBEN == 1
std::cout << "Using Eli Bendersky" << std::endl;
#else
std::cout << "Without TCMalloc" << std::endl;
#endif
double singleThreadAvg = 0.0;
double MTAvg = 0.0;
int num_iterations = 10;
for (int i = 0; i < 10; ++i)
{
std::cout << "Non MT allocations in loop" << std::endl;
singleThreadAvg += allocateAndFreeMemInLoop(1000, 16 * KB);
std::cout << "MT Allocations in loop" << std::endl;
MTAvg += MTAllocateAndFreeMemInLoop(1000, 16 * KB);
}
singleThreadAvg /= (double)num_iterations;
MTAvg /= (double)MTAvg;
std::cout << "Single Thread Avg: " << singleThreadAvg << std::endl;
std::cout << "MT Thread Avg: " << MTAvg << std::endl;
memmgr_print_stats();
system("pause");
}
#include <windows.h>
#ifndef hr_timer
#include "hr_timer.h"
#define hr_timer
#endif
double CStopWatch::LIToSecs(LARGE_INTEGER & L) {
return ((double)L.QuadPart / (double)frequency.QuadPart);
}
CStopWatch::CStopWatch(){
timer.start.QuadPart = 0;
timer.stop.QuadPart = 0;
QueryPerformanceFrequency(&frequency);
}
void CStopWatch::startTimer() {
QueryPerformanceCounter(&timer.start);
}
void CStopWatch::stopTimer() {
QueryPerformanceCounter(&timer.stop);
}
double CStopWatch::getElapsedTime() {
LARGE_INTEGER time;
time.QuadPart = timer.stop.QuadPart - timer.start.QuadPart;
return LIToSecs(time);
}
#pragma once
#include <windows.h>
#include <iostream>
typedef struct {
LARGE_INTEGER start;
LARGE_INTEGER stop;
} stopWatch;
class CStopWatch {
private:
stopWatch timer;
LARGE_INTEGER frequency;
double LIToSecs(LARGE_INTEGER & L);
public:
CStopWatch();
void startTimer();
void stopTimer();
double getElapsedTime();
void printTimer()
{
std::cout << "Time Taken in secs: " << this->getElapsedTime() << std::endl;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment