Skip to content

Instantly share code, notes, and snippets.

@paroj
Created Mar 7, 2017
Embed
What would you like to do?
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