Skip to content

Instantly share code, notes, and snippets.

@paroj
Created March 7, 2017 13:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paroj/2154ad326f154d63fbfd6327bf83298c to your computer and use it in GitHub Desktop.
Save paroj/2154ad326f154d63fbfd6327bf83298c to your computer and use it in GitHub Desktop.
memory allocation performance
#include <cstring>
#include <cmath>
#include <cstdio>
#include <ctime>
#include <cstdlib>
#include <vector>
#define NITER 100//5000000
int main() {
clock_t t;
t = clock();
for(size_t i = 0; i < NITER; i++) {
char* buf = (char*)malloc(8*1024*1024);
asm volatile ("" :: "g"(buf) : "memory");
free(buf);
}
printf("pure %fs\n", float(clock() - t)/CLOCKS_PER_SEC);
t = clock();
for(size_t i = 0; i < NITER; i++) {
char* buf = (char*)malloc(8*1024*1024);
memset(buf, buf[0], 8*1024*1024);
asm volatile ("" :: "g"(buf) : "memory");
free(buf);
}
printf("memset %fs\n", float(clock() - t)/CLOCKS_PER_SEC);
t = clock();
std::vector<char> buf;
for(size_t i = 0; i < NITER; i++) {
buf.resize(8*1024*1024);
asm volatile ("" :: "g"(&buf[0]) : "memory");
buf.clear();
}
printf("vector %fs\n", float(clock() - t)/CLOCKS_PER_SEC);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment