Skip to content

Instantly share code, notes, and snippets.

@lisitsyn
Created July 25, 2012 21:03
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 lisitsyn/3178664 to your computer and use it in GitHub Desktop.
Save lisitsyn/3178664 to your computer and use it in GitHub Desktop.
#include <eigen3/Eigen/Dense>
#include <cblas.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
using namespace Eigen;
static double loop_dot(double* a, double* b, int d)
{
asm("#loop start");
double r = 0.0;
for (int i=0; i<d; i++)
r += a[i]*b[i];
asm("#loop end");
return r;
}
static double blas_dot(double* a, double* b, int d)
{
asm("#blas start");
double r = cblas_ddot(d,a,1,b,1);
asm("#blas end");
return r;
}
static double eigen_dot(double* a, double* b, int d)
{
asm("#eigen start");
Map<VectorXd> a_(a,d);
Map<VectorXd> b_(b,d);
double r = a_.dot(b_);
asm("#eigen end");
return r;
}
void measure(int d, int repeats, const char* name, double (*dot_impl)(double*,double*,int))
{
double* a = (double*)malloc(sizeof(double)*d);
double* b = (double*)malloc(sizeof(double)*d);
srand(time(NULL));
for (int i=0; i<d; i++)
{
a[i] = rand()/10.0;
b[i] = rand()/10.0;
}
clock_t start = clock();
for (int i=0; i<repeats; i++)
dot_impl(a,b,d);
clock_t end = clock();
printf("%s elapsed %f clocks \n", name, double(end-start)/repeats);
free(a);
free(b);
}
int main()
{
measure(1000000,20000,"",&blas_dot);
measure(1000000,20000,"blas",&blas_dot);
measure(1000000,20000,"loop",&loop_dot);
measure(1000000,20000,"eigen",&eigen_dot);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment