Skip to content

Commit fc1136f

Browse files
committed
Add Boost benchmark
1 parent 57d41f4 commit fc1136f

File tree

2 files changed

+211
-0
lines changed

2 files changed

+211
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
2+
# VARIABLES #
3+
4+
ifndef VERBOSE
5+
QUIET := @
6+
endif
7+
8+
# Specify the path to Boost:
9+
BOOST ?=
10+
11+
# Determine the OS:
12+
#
13+
# [1]: https://en.wikipedia.org/wiki/Uname#Examples
14+
# [2]: http://stackoverflow.com/a/27776822/2225624
15+
OS ?= $(shell uname)
16+
ifneq (, $(findstring MINGW,$(OS)))
17+
OS := WINNT
18+
else
19+
ifneq (, $(findstring MSYS,$(OS)))
20+
OS := WINNT
21+
else
22+
ifneq (, $(findstring CYGWIN,$(OS)))
23+
OS := WINNT
24+
endif
25+
endif
26+
endif
27+
28+
# Define the program used for compiling C++ source files:
29+
ifdef CXX_COMPILER
30+
CXX := $(CXX_COMPILER)
31+
else
32+
CXX := g++
33+
endif
34+
35+
# Define the command-line options when compiling C++ files:
36+
CXXFLAGS ?= \
37+
-std=c++11 \
38+
-O3 \
39+
-Wall \
40+
-pedantic
41+
42+
# Determine whether to generate [position independent code][1]:
43+
#
44+
# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options
45+
# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option
46+
ifeq ($(OS), WINNT)
47+
fPIC ?=
48+
else
49+
fPIC ?= -fPIC
50+
endif
51+
52+
# List of C++ targets:
53+
cxx_targets := benchmark.boost.out
54+
55+
56+
# TARGETS #
57+
58+
# Default target.
59+
#
60+
# This target is the default target.
61+
62+
all: $(cxx_targets)
63+
64+
.PHONY: all
65+
66+
67+
# Compile C++ source.
68+
#
69+
# This target compiles C++ source files.
70+
71+
$(cxx_targets): %.out: %.cpp $(BOOST)
72+
$(QUIET) $(CXX) $(CXXFLAGS) $(fPIC) -I $(BOOST) -o $@ $< -lm
73+
74+
75+
# Run a benchmark.
76+
#
77+
# This target runs a benchmark.
78+
79+
run: $(cxx_targets)
80+
$(QUIET) ./$<
81+
82+
.PHONY: run
83+
84+
85+
# Perform clean-up.
86+
#
87+
# This target removes generated files.
88+
89+
clean:
90+
$(QUIET) -rm -f *.o *.out
91+
92+
.PHONY: clean
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/**
2+
* Benchmark Boost `powm1`.
3+
*/
4+
#include <stdlib.h>
5+
#include <stdio.h>
6+
#include <math.h>
7+
#include <sys/time.h>
8+
#include <boost/random/mersenne_twister.hpp>
9+
#include <boost/random/uniform_real_distribution.hpp>
10+
#include <boost/math/special_functions/powm1.hpp>
11+
12+
using boost::random::uniform_real_distribution;
13+
using boost::random::mt19937;
14+
15+
#define NAME "powm1"
16+
#define ITERATIONS 1000000
17+
#define REPEATS 3
18+
19+
/**
20+
* Prints the TAP version.
21+
*/
22+
void print_version() {
23+
printf( "TAP version 13\n" );
24+
}
25+
26+
/**
27+
* Prints the TAP summary.
28+
*
29+
* @param total total number of tests
30+
* @param passing total number of passing tests
31+
*/
32+
void print_summary( int total, int passing ) {
33+
printf( "#\n" );
34+
printf( "1..%d\n", total ); // TAP plan
35+
printf( "# total %d\n", total );
36+
printf( "# pass %d\n", passing );
37+
printf( "#\n" );
38+
printf( "# ok\n" );
39+
}
40+
41+
/**
42+
* Prints benchmarks results.
43+
*
44+
* @param elapsed elapsed time in seconds
45+
*/
46+
void print_results( double elapsed ) {
47+
double rate = (double)ITERATIONS / elapsed;
48+
printf( " ---\n" );
49+
printf( " iterations: %d\n", ITERATIONS );
50+
printf( " elapsed: %0.9f\n", elapsed );
51+
printf( " rate: %0.9f\n", rate );
52+
printf( " ...\n" );
53+
}
54+
55+
/**
56+
* Returns a clock time.
57+
*
58+
* @returns clock time
59+
*/
60+
double tic() {
61+
struct timeval now;
62+
gettimeofday( &now, NULL );
63+
return (double)now.tv_sec + (double)now.tv_usec/1.0e6;
64+
}
65+
66+
/**
67+
* Runs a benchmark.
68+
*
69+
* @return elapsed time in seconds
70+
*/
71+
double benchmark() {
72+
double elapsed;
73+
double x;
74+
double y;
75+
double z;
76+
double t;
77+
int i;
78+
79+
// Define a new pseudorandom number generator:
80+
mt19937 rng;
81+
82+
// Define a uniform distribution for generating pseudorandom numbers as "doubles" between a minimum value (inclusive) and a maximum value (exclusive):
83+
uniform_real_distribution<> randu1( 0.0, 2.0 );
84+
uniform_real_distribution<> randu2( -50.0, 50.0 );
85+
86+
t = tic();
87+
for ( i = 0; i < ITERATIONS; i++ ) {
88+
x = randu1( rng );
89+
y = randu2( rng );
90+
z = boost::math::powm1( x, y );
91+
if ( y != y ) {
92+
printf( "should not return NaN\n" );
93+
break;
94+
}
95+
}
96+
elapsed = tic() - t;
97+
if ( z != z ) {
98+
printf( "should not return NaN\n" );
99+
}
100+
return elapsed;
101+
}
102+
103+
/**
104+
* Main execution sequence.
105+
*/
106+
int main( void ) {
107+
double elapsed;
108+
int i;
109+
110+
print_version();
111+
for ( i = 0; i < REPEATS; i++ ) {
112+
printf( "# cpp::boost::%s\n", NAME );
113+
elapsed = benchmark();
114+
print_results( elapsed );
115+
printf( "ok %d benchmark finished\n", i+1 );
116+
}
117+
print_summary( REPEATS, REPEATS );
118+
return 0;
119+
}

0 commit comments

Comments
 (0)