freerun.c
#include <pthread.h>
#include <stdio.h>
#include <time.h>
typedef struct {
int i;
int loops;
} shared;
void *worker( void *arg ) {
shared *s = (shared*)arg;
int i = 0;
for( i=0; i < s->loops; ++i ) {
++s->i;
}
return;
}
int main( int argc, char **argv ) {
int i = 0, nThreads = 4;
clock_t start, finish;
double elapsed;
pthread_t threads[32];
shared s = { 0, 1000000 };
if( argc > 1 ) nThreads = atoi( argv[1] );
if( argc > 2 ) s.loops = atoi( argv[2] );
printf( "threads:%d loops:%d\n", nThreads, s.loops );
start = clock();
for( i=0; i < nThreads; ++i )
pthread_create( &threads[ i ], NULL, &worker, &s );
for( i=0; i < nThreads; ++i )
pthread_join( threads[ i ], NULL );
finish = clock();
elapsed = (double)(finish - start) / CLOCKS_PER_SEC;
printf( "count: %u time:%.6f\n", s.i, elapsed );
}
results on my Mac:
threads:4 loops:1000000
count: 2172720 time:0.079071
results on Ubuntu 9.04
threads:4 loops:1000000
count: 3215463 time:0.070000
mutex.c
#include <pthread.h>
#include <stdio.h>
#include <time.h>
typedef struct {
int i;
int loops;
pthread_mutex_t mutex;
} shared;
void *worker( void *arg ) {
shared *s = (shared*)arg;
int i = 0;
for( i=0; i < s->loops; ++i ) {
pthread_mutex_lock( &s->mutex );
++s->i;
pthread_mutex_unlock( &s->mutex );
}
return;
}
int main( int argc, char **argv ) {
int i = 0, nThreads = 4;
clock_t start, finish;
double elapsed;
pthread_t threads[32];
shared s = { 0, 1000000 };;
pthread_mutex_init( &s.mutex, NULL );
if( argc > 1 ) nThreads = atoi( argv[1] );
if( argc > 2 ) s.loops = atoi( argv[2] );
printf( "threads:%d loops:%d\n", nThreads, s.loops );
start = clock();
for( i=0; i < nThreads; ++i )
pthread_create( &threads[ i ], NULL, &worker, &s );
for( i=0; i < nThreads; ++i )
pthread_join( threads[ i ], NULL );
finish = clock();
elapsed = (double)(finish - start) / CLOCKS_PER_SEC;
printf( "count: %u time:%.6f\n", s.i, elapsed );
}
Ran like dogsh*t on my Mac
But on Ubuntu 9.04
threads:4 loops:1000000
count: 4000000 time:1.050000
The Ubuntu 9.04 machine I was testing on was a Dell. FYI, if you are on a Linux machine, and want to get all the CPU info, just 'cat /proc/cpuinfo':
processor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 15
model name : Intel(R) Core(TM)2 Duo CPU E4600 @ 2.40GHz
stepping : 13
cpu MHz : 1200.000
cache size : 2048 KB
(above info is per core)
Other Info:
kernel: linux 2.6.18-11 (64-bit)
gcc: 4.5.1
libc: 2.9
gcc -o mutex mutex.c -lpthread
freelock.c
#include <pthread.h>
#include <stdio.h>
#include <time.h>
typedef struct {
int i;
int loops;
} shared;
//shared s = { 0, 1000000 };
void *worker( void *arg ) {
shared *s = (shared*)arg;
int i = 0;
for( i=0; i < s->loops; ++i ) {
//__sync_fetch_and_add( &s->i, s->i + 1 );
__sync_fetch_and_add( &s->i, 1 );
}
return;
}
void main( int argc, char **argv ) {
int i = 0, nThreads = 4;
clock_t start, finish;
double elapsed;
pthread_t threads[32];
shared s = { 0, 1000000 };;
if( argc > 1 ) nThreads = atoi( argv[1] );
if( argc > 2 ) s.loops = atoi( argv[2] );
printf( "threads:%d loops:%d\n", nThreads, s.loops );
start = clock();
for( i=0; i < nThreads; ++i )
pthread_create( &threads[ i ], NULL, &worker, &s );
for( i=0; i < nThreads; ++i )
pthread_join( threads[ i ], NULL );
finish = clock();
elapsed = (double)(finish - start) / CLOCKS_PER_SEC;
printf( "count: %u time:%.6f\n", s.i, elapsed );
}
results on same Dell:
threads:4 loops:1000000
count: 4000000 time:0.240000
Just a note that, obviously, this would be intel-specific, and could perhaps vary significantly based on x86 type.
|