Skip to content

Commit 0f0ec01

Browse files
committed
Add benchmark snippets
1 parent acff705 commit 0f0ec01

File tree

7 files changed

+516
-0
lines changed

7 files changed

+516
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
2+
# VARIABLES #
3+
4+
ifndef VERBOSE
5+
QUIET := @
6+
endif
7+
8+
# Determine the OS:
9+
#
10+
# [1]: https://en.wikipedia.org/wiki/Uname#Examples
11+
# [2]: http://stackoverflow.com/a/27776822/2225624
12+
OS ?= $(shell uname)
13+
ifneq (, $(findstring MINGW,$(OS)))
14+
OS := WINNT
15+
else
16+
ifneq (, $(findstring MSYS,$(OS)))
17+
OS := WINNT
18+
else
19+
ifneq (, $(findstring CYGWIN,$(OS)))
20+
OS := WINNT
21+
endif
22+
endif
23+
endif
24+
25+
# Define the program used for compiling C source files:
26+
ifdef C_COMPILER
27+
CC := $(C_COMPILER)
28+
else
29+
CC := gcc
30+
endif
31+
32+
# Define the command-line options when compiling C files:
33+
CFLAGS ?= \
34+
-std=c99 \
35+
-O3 \
36+
-Wall \
37+
-pedantic
38+
39+
# Determine whether to generate [position independent code][1]:
40+
#
41+
# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options
42+
# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option
43+
ifeq ($(OS), WINNT)
44+
fPIC ?=
45+
else
46+
fPIC ?= -fPIC
47+
endif
48+
49+
# List of C targets:
50+
c_targets := benchmark.out
51+
52+
53+
# TARGETS #
54+
55+
# Default target.
56+
#
57+
# This target is the default target.
58+
59+
all: $(c_targets)
60+
61+
.PHONY: all
62+
63+
64+
# Compile C source.
65+
#
66+
# This target compiles C source files.
67+
68+
$(c_targets): %.out: %.c
69+
$(QUIET) $(CC) $(CFLAGS) $(fPIC) -o $@ $<
70+
71+
72+
# Run a benchmark.
73+
#
74+
# This target runs a benchmark.
75+
76+
run: $(c_targets)
77+
$(QUIET) ./$<
78+
79+
.PHONY: run
80+
81+
82+
# Perform clean-up.
83+
#
84+
# This target removes generated files.
85+
86+
clean:
87+
$(QUIET) -rm -f *.o *.out
88+
89+
.PHONY: clean
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/**
2+
* Benchmark `TODO`.
3+
*/
4+
#include <stdlib.h>
5+
#include <stdio.h>
6+
#include <math.h>
7+
#include <time.h>
8+
9+
#define ITERATIONS 1000000
10+
#define REPEATS 3
11+
12+
/**
13+
* Prints the TAP version.
14+
*/
15+
void print_version() {
16+
printf( "TAP version 13\n" );
17+
}
18+
19+
/**
20+
* Prints the TAP summary.
21+
*
22+
* @param total total number of tests
23+
* @param passing total number of passing tests
24+
*/
25+
void print_summary( int total, int passing ) {
26+
printf( "#\n" );
27+
printf( "1..%d\n", total ); // TAP plan
28+
printf( "# total %d\n", total );
29+
printf( "# pass %d\n", passing );
30+
printf( "#\n" );
31+
printf( "# ok\n" );
32+
}
33+
34+
/**
35+
* Prints benchmarks results.
36+
*
37+
* @param elapsed elapsed time in seconds
38+
*/
39+
void print_results( double elapsed ) {
40+
double rate = (double)ITERATIONS / elapsed;
41+
printf( " ---\n" );
42+
printf( " iterations: %d\n", ITERATIONS );
43+
printf( " elapsed: %0.9f\n", elapsed );
44+
printf( " rate: %0.9f\n", rate );
45+
printf( " ...\n" );
46+
}
47+
48+
/**
49+
* Returns a clock time.
50+
*
51+
* @returns clock time
52+
*/
53+
double tic() {
54+
struct timeval now;
55+
gettimeofday( &now, NULL );
56+
return (double)now.tv_sec + (double)now.tv_usec/1.0e6;
57+
}
58+
59+
/**
60+
* Generates a random double on the interval [0,1].
61+
*
62+
* @return random double
63+
*/
64+
double rand_double() {
65+
int r = rand();
66+
return (double)r / ( (double)RAND_MAX + 1.0 );
67+
}
68+
69+
/**
70+
* Runs a benchmark.
71+
*
72+
* @return elapsed time in seconds
73+
*/
74+
double benchmark() {
75+
double elapsed;
76+
double x;
77+
double y;
78+
double t;
79+
int i;
80+
81+
t = tic();
82+
for ( i = 0; i < ITERATIONS; i++ ) {
83+
x = 0.0; // TODO
84+
y = 0.0; // TODO
85+
if ( y != y ) {
86+
printf( "should not return NaN\n" );
87+
break;
88+
}
89+
}
90+
elapsed = tic() - t;
91+
if ( y != y ) {
92+
printf( "should not return NaN\n" );
93+
}
94+
return elapsed;
95+
}
96+
97+
/**
98+
* Main execution sequence.
99+
*/
100+
int main( void ) {
101+
double elapsed;
102+
int i;
103+
104+
// Use the current time to seed the random number generator:
105+
srand( time( NULL ) );
106+
107+
print_version();
108+
for ( i = 0; i < REPEATS; i++ ) {
109+
printf( "# c::%s\n", "TODO" );
110+
elapsed = benchmark();
111+
print_results( elapsed );
112+
printf( "ok %d benchmark finished\n", i+1 );
113+
}
114+
print_summary( REPEATS, REPEATS );
115+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
julia 0.4
2+
BenchmarkTools 0.0.8
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
#!/usr/bin/env julia
2+
3+
import BenchmarkTools
4+
5+
# Benchmark variables:
6+
name = "TODO";
7+
repeats = 3;
8+
9+
"""
10+
print_version()
11+
12+
Prints the TAP version.
13+
14+
# Examples
15+
16+
``` julia
17+
julia> print_version()
18+
```
19+
"""
20+
function print_version()
21+
@printf( "TAP version 13\n" );
22+
end
23+
24+
"""
25+
print_summary( total, passing )
26+
27+
Print the benchmark summary.
28+
29+
# Arguments
30+
31+
* `total`: total number of tests
32+
* `passing`: number of passing tests
33+
34+
# Examples
35+
36+
``` julia
37+
julia> print_summary( 3, 3 )
38+
```
39+
"""
40+
function print_summary( total, passing )
41+
@printf( "#\n" );
42+
@printf( "1..%d\n", total ); # TAP plan
43+
@printf( "# total %d\n", total );
44+
@printf( "# pass %d\n", passing );
45+
@printf( "#\n" );
46+
@printf( "# ok\n" );
47+
end
48+
49+
"""
50+
print_results( iterations, elapsed )
51+
52+
Print benchmark results.
53+
54+
# Arguments
55+
56+
* `iterations`: number of iterations
57+
* `elapsed`: elapsed time (in seconds)
58+
59+
# Examples
60+
61+
``` julia
62+
julia> print_results( 1000000, 0.131009101868 )
63+
```
64+
"""
65+
function print_results( iterations, elapsed )
66+
rate = iterations / elapsed
67+
68+
@printf( " ---\n" );
69+
@printf( " iterations: %d\n", iterations );
70+
@printf( " elapsed: %0.9f\n", elapsed );
71+
@printf( " rate: %0.9f\n", rate );
72+
@printf( " ...\n" );
73+
end
74+
75+
"""
76+
benchmark()
77+
78+
Run a benchmark.
79+
80+
# Notes
81+
82+
* Benchmark results are returned as a two-element array: [ iterations, elapsed ].
83+
* The number of iterations is not the true number of iterations. Instead, an 'iteration' is defined as a 'sample', which is a computed estimate for a single evaluation.
84+
* The elapsed time is in seconds.
85+
86+
# Examples
87+
88+
``` julia
89+
julia> benchmark();
90+
```
91+
"""
92+
function benchmark()
93+
t = BenchmarkTools.@benchmark TODO samples=1e6
94+
95+
# Compute the total "elapsed" time and convert from nanoseconds to seconds:
96+
s = sum( t.times ) / 1.0e9;
97+
98+
# Determine the number of "iterations":
99+
iter = length( t.times );
100+
101+
# Return the results:
102+
[ iter, s ];
103+
end
104+
105+
"""
106+
main()
107+
108+
Run benchmarks.
109+
110+
# Examples
111+
112+
``` julia
113+
julia> main();
114+
```
115+
"""
116+
function main()
117+
print_version();
118+
for i in 1:3
119+
@printf( "# julia::%s\n", name );
120+
results = benchmark();
121+
print_results( results[ 1 ], results[ 2 ] );
122+
@printf( "ok %d benchmark finished\n", i );
123+
end
124+
print_summary( repeats, repeats );
125+
end
126+
127+
main();

0 commit comments

Comments
 (0)