Skip to content

Instantly share code, notes, and snippets.

@kig
Created March 11, 2009 17:15
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 kig/77579 to your computer and use it in GitHub Desktop.
Save kig/77579 to your computer and use it in GitHub Desktop.
/*
$ gcc -O3 mmul_bm.c -o mmul_bm
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#define ITERATIONS 1000000
void mmul4x4f (const float * a, const float * b, float * c)
{
int i,j;
for (i=0; i<16; i+=4) {
for (j=0; j<4; ++j) {
c[i+j] =
b[i+0] * a[j+0]
+ b[i+1] * a[j+4]
+ b[i+2] * a[j+8]
+ b[i+3] * a[j+12];
}
}
}
void mmul4x4d (const double * a, const double * b, double * c)
{
int i,j;
for (i=0; i<16; i+=4) {
for (j=0; j<4; ++j) {
c[i+j] =
b[i+0] * a[j+0]
+ b[i+1] * a[j+4]
+ b[i+2] * a[j+8]
+ b[i+3] * a[j+12];
}
}
}
void floats_malloc ()
{
float * m, * n, * p;
clock_t t1, t2;
int j;
m = malloc(16*sizeof(float));
n = malloc(16*sizeof(float));
p = malloc(16*sizeof(float));
t1 = clock();
for (j=0; j<ITERATIONS; ++j) {
mmul4x4f(m, n, p);
}
t2 = clock();
printf("floats_malloc: %f\n", ((double)t2-t1) / CLOCKS_PER_SEC);
}
void floats_memalign ()
{
float * m, * n, * p;
clock_t t1, t2;
int j;
posix_memalign((void**)&m, 16, 16*sizeof(float));
posix_memalign((void**)&n, 16, 16*sizeof(float));
posix_memalign((void**)&p, 16, 16*sizeof(float));
t1 = clock();
for (j=0; j<ITERATIONS; ++j) {
mmul4x4f(m, n, p);
}
t2 = clock();
printf("floats_memalign: %f\n", ((double)t2-t1) / CLOCKS_PER_SEC);
}
void doubles_malloc ()
{
double * m, * n, * p;
clock_t t1, t2;
int j;
m = malloc(16*sizeof(double));
n = malloc(16*sizeof(double));
p = malloc(16*sizeof(double));
t1 = clock();
for (j=0; j<ITERATIONS; ++j) {
mmul4x4d(m, n, p);
}
t2 = clock();
printf("doubles_malloc: %f\n", ((double)t2-t1) / CLOCKS_PER_SEC);
}
void doubles_memalign ()
{
double * m, * n, * p;
clock_t t1, t2;
int j;
posix_memalign((void**)&m, 16, 16*sizeof(double));
posix_memalign((void**)&n, 16, 16*sizeof(double));
posix_memalign((void**)&p, 16, 16*sizeof(double));
t1 = clock();
for (j=0; j<ITERATIONS; ++j) {
mmul4x4d(m, n, p);
}
t2 = clock();
printf("doubles_memalign: %f\n", ((double)t2-t1) / CLOCKS_PER_SEC);
}
int main(){
doubles_malloc();
doubles_memalign();
floats_malloc();
floats_memalign();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment