Skip to content

Instantly share code, notes, and snippets.

@spikebike
Created September 25, 2014 22:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spikebike/1f5d06b42a96258430b8 to your computer and use it in GitHub Desktop.
Save spikebike/1f5d06b42a96258430b8 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
/* crude wall clock time keeping, returns a double of seconds */
double
seconds()
{
struct timeval tp;
struct timezone tzp;
gettimeofday (&tp, &tzp);
return ((double) tp.tv_sec + (double) tp.tv_usec * 1.e-6);
}
/* allocate 3 512MB arrays, 64M each */
#define SIZE 67108864
main()
{
double *a,*b,*c;
double start,stop,diff;
int i,mem;
a=(double *) malloc(SIZE*sizeof(double));
b=(double *) malloc(SIZE*sizeof(double));
c=(double *) malloc(SIZE*sizeof(double));
for (i=0;i<SIZE;i++)
{
b[i]=i*1.25;
c[i]=i*1.9;
}
start=seconds();
for (i=0;i<SIZE;i++)
{
a[i]=b[i]*c[i];
}
stop=seconds();
diff=stop-start;
mem=(SIZE*sizeof(double)*3);
printf ("%d double flops took %f seconds\n",SIZE,diff);
printf ("%f Mflops/sec\n",SIZE/(diff*1000000));
printf ("total size of 3 arrays = %d MB\n",mem/(1024*1024));
printf ("memory bandwidth = %5.2f GB/sec\n",(mem/(1048*1024*1024))/diff);
}
@spikebike
Copy link
Author

$ ./a.out
67108864 double flops took 0.123589 seconds
543.000129 Mflops/sec
total size of 3 arrays = 1536 MB
memory bandwidth = 8.09 GB/sec

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