Skip to content

Instantly share code, notes, and snippets.

@RoyBellingan
Created January 25, 2016 21:15
Show Gist options
  • Save RoyBellingan/f99ab5fa52690e75ded3 to your computer and use it in GitHub Desktop.
Save RoyBellingan/f99ab5fa52690e75ded3 to your computer and use it in GitHub Desktop.
Just a useless test
#include <sys/time.h>
#include <stdlib.h>
#include <stdio.h>
#include <thread>
typedef struct quux {
int i;
} quux_t;
void foo(int run) {
quux_t *bar;
printf("thread on\n");
int i;
for(i = 0; i < run; i++) {
bar = (quux_t *)malloc(sizeof(quux_t));
free(bar);
}
}
int main(int argc, char **argv) {
struct timeval tv;
gettimeofday(&tv, NULL);
int i;
long usecs_start = tv.tv_usec + tv.tv_sec * 1000000;
int th = atoi(argv[2]);
std::thread threads[th];
for(i=0; i< th; i++){
threads[i] = std::thread(foo,atoi(argv[1]));
}
for(i=0; i< th; i++){
threads[i].join();
}
gettimeofday(&tv, NULL);
long usecs_end = tv.tv_usec + tv.tv_sec * 1000000;
printf("%ld\n", usecs_end - usecs_start);
return 0;
}
@RoyBellingan
Copy link
Author

Compile with

g++-5.2 0.cpp -O2 -Wno-unused-variable -Wno-unused-parameter -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free -std=c++14 -lpthread -ljemalloc

-Wno-unused-variable -Wno-unused-parameter are used to avoid the optimizer to just remove the unused result. resulting in light speed execution...

Non using jemalloc will result in 1sec slower execution

Cpu Amd A10 7400P @ 2.5Ghz; Linux 4.3. something

for i in {1..10} ; do ./a.out $N_OBJS 2; done|awk '{s += $1} END { print "avg: ", s/NR }'
avg: 345370

for i in {1..10} ; do ./a.out $N_OBJS 1; done|awk '{s += $1} END { print "avg: ", s/NR }'
avg: 344059

for i in {1..10} ; do ./a.out $N_OBJS 3; done|awk '{s += $1} END { print "avg: ", s/NR }'
avg: 355574

for i in {1..10} ; do ./a.out $N_OBJS 4; done|awk '{s += $1} END { print "avg: ", s/NR }'
avg: 380500

Of course after 4 thread time start to grow

@michalfita
Copy link

-W... are for warning, not for compiler features. For compiler features you should use -f...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment