Skip to content

Instantly share code, notes, and snippets.

@dcolascione
Created November 7, 2016 16:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcolascione/ae9be560ecadc349c25e4f1e97648b99 to your computer and use it in GitHub Desktop.
Save dcolascione/ae9be560ecadc349c25e4f1e97648b99 to your computer and use it in GitHub Desktop.
#include <pthread.h>
#include <stdlib.h>
#include <sys/time.h>
#include <stdio.h>
static void*
thread_func(void* arg)
{
return NULL;
}
double
tv2double(struct timeval tv)
{
return (double) tv.tv_sec + (double) tv.tv_usec / 1e6;
}
int
main()
{
pthread_t thread;
void* rv;
unsigned iters = 1e6;
struct timeval start;
gettimeofday(&start, NULL);
for (unsigned i = 0; i < iters; ++i) {
if (pthread_create(&thread, NULL, thread_func, NULL))
abort();
if (pthread_join(thread, &rv))
abort();
}
struct timeval end;
gettimeofday(&end, NULL);
double elapsed = tv2double(end) - tv2double(start);
printf("%u iterations in %g seconds: %g iters/second\n",
iters, elapsed, iters / elapsed);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment