Skip to content

Instantly share code, notes, and snippets.

@bjodah
Last active August 29, 2015 14:01
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 bjodah/16a1342ff414af21436b to your computer and use it in GitHub Desktop.
Save bjodah/16a1342ff414af21436b to your computer and use it in GitHub Desktop.
Benchmark pow() vs explicit multiplication
#if __STDC_VERSION__ >= 199901L /* POSIX 2008 SUS v4 */
#define _XOPEN_SOURCE 700
#else
#define _XOPEN_SOURCE 500
#endif /* __STDC_VERSION__ */
#include <math.h> // pow
#include <time.h> // clock_gettime
#include <stdio.h> // printf
#include <stdlib.h> // rand
#define SAMPLE_N 10000000
int main(int argc, char **argv){
if (argc != 2)
return 1;
int idx = atoi(argv[1]);
double in[SAMPLE_N], out[SAMPLE_N];
struct timespec start, finish;
for (int i=0; i<SAMPLE_N; ++i){
in[i] = ((double)rand()) / RAND_MAX;
}
for (int j=0; j<10; ++j){
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);
for (int i=0; i<SAMPLE_N; ++i){
out[i] = pow(in[i], 5);
}
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &finish);
printf("optimization guard: %5.3f'", out[idx]);
printf("pow : %12.8f\n", finish.tv_sec-start.tv_sec + 1e-9*(finish.tv_nsec-start.tv_nsec));
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);
for (int i=0; i<SAMPLE_N; ++i){
out[i] = in[i]*in[i]*in[i]*in[i]*in[i];
}
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &finish);
printf("optimization guard: %5.3f'", out[idx]);
printf("explicit: %12.8f\n", finish.tv_sec-start.tv_sec + 1e-9*(finish.tv_nsec-start.tv_nsec));
}
return 0;
}
# Enabling -ffast-math makes the two versions competetive, without it explicit is much faster...
# (~20x for exponent of 5)
CFLAGS ?= -std=c99 -O3 #-ffast-math
LIBS ?= -lrt -lm
.PHONY: all
output.txt: main
$(CC) --version > $@
./$< 42 >> $@
main: main.c
$(CC) $(CFLAGS) -o $@ $^ $(LIBS)
gcc (Ubuntu 4.8.2-19ubuntu1) 4.8.2
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
optimization guard: 0.010'pow : 1.90185076
optimization guard: 0.010'explicit: 0.08865624
optimization guard: 0.010'pow : 1.88004106
optimization guard: 0.010'explicit: 0.09030047
optimization guard: 0.010'pow : 1.87685020
optimization guard: 0.010'explicit: 0.08904781
optimization guard: 0.010'pow : 1.88149224
optimization guard: 0.010'explicit: 0.08866682
optimization guard: 0.010'pow : 1.87501423
optimization guard: 0.010'explicit: 0.08858129
optimization guard: 0.010'pow : 1.87789195
optimization guard: 0.010'explicit: 0.08893130
optimization guard: 0.010'pow : 1.87002738
optimization guard: 0.010'explicit: 0.08871757
optimization guard: 0.010'pow : 1.87417454
optimization guard: 0.010'explicit: 0.08991346
optimization guard: 0.010'pow : 1.88419079
optimization guard: 0.010'explicit: 0.08897720
optimization guard: 0.010'pow : 1.87802256
optimization guard: 0.010'explicit: 0.08939641
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment