Skip to content

Instantly share code, notes, and snippets.

@dmitryvk
Created September 28, 2010 19:10
Show Gist options
  • Save dmitryvk/601592 to your computer and use it in GitHub Desktop.
Save dmitryvk/601592 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <pthread.h>
struct data {
int c1;
int padding[1024*1024*64];
int c2;
} data;
int repeats = 1;
int count = 100000000;
void* t1(void* arg)
{
int reps = repeats;
int cnt = count;
int i, j;
for (j = 0; j < reps; ++j)
for (i = 0; i < cnt; ++i)
#ifdef BARE
data.c1++;
#endif
#ifdef FNA
__sync_fetch_and_add(&data.c1, 1);
#endif
#ifdef ANF
__sync_add_and_fetch(&data.c1, 1);
#endif
fprintf(stderr, "%d\n", data.c1);
}
void* t2(void* arg)
{
int reps = repeats;
int cnt = count;
int i, j;
for (j = 0; j < reps; ++j)
for (i = 0; i < cnt; ++i)
#ifdef BARE
data.c2++;
#endif
#ifdef FNA
__sync_fetch_and_add(&data.c2, 1);
#endif
#ifdef ANF
__sync_add_and_fetch(&data.c2, 1);
#endif
fprintf(stderr, "%d\n", data.c2);
}
int main(int argc, char * argv[])
{
pthread_t th1, th2;
void *rv;
pthread_create(&th1, NULL, t1, NULL);
pthread_create(&th2, NULL, t2, NULL);
pthread_join(th1, &rv);
pthread_join(th2, &rv);
return 0;
}
dvk@dvk-laptop ~/tmp $ gcc -O0 -DFNA cas.c -o cas -lpthread
dvk@dvk-laptop ~/tmp $ time ./cas
100000000
100000000
real 0m2.445s
user 0m4.755s
sys 0m0.001s
dvk@dvk-laptop ~/tmp $ gcc -O0 -DANF cas.c -o cas -lpthread
dvk@dvk-laptop ~/tmp $ time ./cas
100000000
100000000
real 0m2.448s
user 0m4.768s
sys 0m0.004s
dvk@dvk-laptop ~/tmp $ gcc -O0 -DBARE cas.c -o cas -lpthread
dvk@dvk-laptop ~/tmp $ time ./cas
100000000
100000000
real 0m0.432s
user 0m0.656s
sys 0m0.001s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment