Skip to content

Instantly share code, notes, and snippets.

@xk xk/threads.c
Last active May 2, 2017

What would you like to do?
Another useless benchmark to illustrate a point
// 2017-04-30
// See
#include <pthread.h>
#include <stdlib.h>
#define BUF_SIZE 16*1024*1024
pthread_t t1, t2, t3, t4;
double* nu_buffer () { return (double*) malloc(BUF_SIZE); }
void* thread_proc (void* arg) {
int i= BUF_SIZE/8;
double* buffer= nu_buffer();
while (i--) buffer[i]= (((double*) arg)[i] + buffer[i]) / 2.0;
return buffer;
void do_threads_stuff (double* buffer) {
pthread_create(&t1, NULL, thread_proc, buffer);
pthread_create(&t2, NULL, thread_proc, buffer);
pthread_create(&t3, NULL, thread_proc, buffer);
pthread_create(&t4, NULL, thread_proc, buffer);
void* r;
pthread_join(t1, &r), free(r);
pthread_join(t2, &r), free(r);
pthread_join(t3, &r), free(r);
pthread_join(t4, &r), free(r);
int main (void) {
double* buffer= nu_buffer();
//free(buffer); return 0;
The i7 Mac:
unibodySierra:Desktop admin$ gcc -O0 threads.c -o threads
unibodySierra:Desktop admin$ time ./threads
real 0m0.054s
user 0m0.084s
sys 0m0.073s
And the OPi Zero:
pi@orangepi:~$ gcc -lpthread -O0 threads.c -o threads
pi@orangepi:~$ time ./threads
real 0m0.166s
user 0m0.440s
sys 0m0.140s
And the Mac is 0,166/0,054= only 3x times faster.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.