Skip to content

Instantly share code, notes, and snippets.

@ViralBShah
Created April 4, 2013 09:46
Show Gist options
  • Save ViralBShah/5309148 to your computer and use it in GitHub Desktop.
Save ViralBShah/5309148 to your computer and use it in GitHub Desktop.
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef OPENLIBM
#include "openlibm.h"
#else
#include <math.h>
#endif
#define N 10000000
int main(int argc, char *argv[]) {
double y;
if (argc < 2) y = 3.1; else y = atof(argv[1]);
clock_t t1, t2;
double *res = malloc(N*sizeof(double));
t1 = clock();
for (int i=0; i<N; ++i)
res[i] = pow((double) i, y);
t2 = clock();
printf("res[100] = %f, pow: %f, Time: %lf\n", res[100], y, (t2-t1)/((double) CLOCKS_PER_SEC));
free(res);
float *resf = malloc(N*sizeof(float));
t1 = clock();
for (int i=0; i<N; ++i)
resf[i] = pow((float) i, y);
t2 = clock();
printf("resf[100] = %f, powf: %f, Time: %lf\n", resf[100], y, (t2-t1)/(double) CLOCKS_PER_SEC);
free(resf);
}
@ViralBShah
Copy link
Author

Integer power > 4

viral-laptop 03:14:14 /tmp$ clang -I ~/julia/deps/openlibm/include -I ~/julia/deps/openlibm/src  -DOPENLIBM -O3 powperf.c -o powperf ~/julia/deps/openlibm/libopenlibm.a && ./powperf 5
res[100] = 10000000000.000000, pow: 5.000000, Time: 0.918617
resf[100] = 10000000000.000000, powf: 5.000000, Time: 0.921552
viral-laptop 03:17:34 /tmp$ clang -O3 powperf.c -o powperf && ./powperf 5
res[100] = 10000000000.000000, pow: 5.000000, Time: 0.349708
resf[100] = 10000000000.000000, powf: 5.000000, Time: 0.340465

Integer power < 4

viral-laptop 03:18:13 /tmp$ clang -I ~/julia/deps/openlibm/include -I ~/julia/deps/openlibm/src  -DOPENLIBM -O3 powperf.c -o powperf ~/julia/deps/openlibm/libopenlibm.a && ./powperf 3
res[100] = 1000000.000000, pow: 3.000000, Time: 0.126835
resf[100] = 1000000.000000, powf: 3.000000, Time: 0.108149
viral-laptop 03:18:16 /tmp$ clang -O3 powperf.c -o powperf && ./powperf 3 
res[100] = 1000000.000000, pow: 3.000000, Time: 0.309624
resf[100] = 1000000.000000, powf: 3.000000, Time: 0.304304

Non-integer power


viral-laptop 03:18:18 /tmp$ clang -I ~/julia/deps/openlibm/include -I ~/julia/deps/openlibm/src  -DOPENLIBM -O3 powperf.c -o powperf ~/julia/deps/openlibm/libopenlibm.a && ./powperf 2.7
res[100] = 251188.643151, pow: 2.700000, Time: 0.904921
resf[100] = 251188.640625, powf: 2.700000, Time: 0.893183
viral-laptop 03:19:20 /tmp$ clang -O3 powperf.c -o powperf && ./powperf 2.7
res[100] = 251188.643151, pow: 2.700000, Time: 0.773378
resf[100] = 251188.640625, powf: 2.700000, Time: 0.772976

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