Skip to content

Commit ddc21b5

Browse files
committed
Add C benchmark snippets for native implementations
1 parent 442201f commit ddc21b5

File tree

3 files changed

+346
-0
lines changed

3 files changed

+346
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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 includes:
50+
INCLUDE ?=
51+
52+
# List of source files:
53+
SOURCE_FILES ?=
54+
55+
# List of C targets:
56+
c_targets := TODO.out
57+
58+
59+
# TARGETS #
60+
61+
# Default target.
62+
#
63+
# This target is the default target.
64+
65+
all: $(c_targets)
66+
67+
.PHONY: all
68+
69+
70+
# Compile C source.
71+
#
72+
# This target compiles C source files.
73+
74+
$(c_targets): %.out: %.c
75+
$(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< -lm
76+
77+
78+
# Run a benchmark.
79+
#
80+
# This target runs a benchmark.
81+
82+
run: $(c_targets)
83+
$(QUIET) ./$<
84+
85+
.PHONY: run
86+
87+
88+
# Perform clean-up.
89+
#
90+
# This target removes generated files.
91+
92+
clean:
93+
$(QUIET) -rm -f *.o *.out
94+
95+
.PHONY: clean
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/**
2+
* Benchmark `TODO`.
3+
*/
4+
#include <stdlib.h>
5+
#include <stdio.h>
6+
#include <math.h>
7+
#include <sys/time.h>
8+
#include "TODO.h"
9+
10+
#define NAME "TODO"
11+
#define ITERATIONS 1000000
12+
#define REPEATS 3
13+
14+
/**
15+
* Prints the TAP version.
16+
*/
17+
void print_version() {
18+
printf( "TAP version 13\n" );
19+
}
20+
21+
/**
22+
* Prints the TAP summary.
23+
*
24+
* @param total total number of tests
25+
* @param passing total number of passing tests
26+
*/
27+
void print_summary( int total, int passing ) {
28+
printf( "#\n" );
29+
printf( "1..%d\n", total ); // TAP plan
30+
printf( "# total %d\n", total );
31+
printf( "# pass %d\n", passing );
32+
printf( "#\n" );
33+
printf( "# ok\n" );
34+
}
35+
36+
/**
37+
* Prints benchmarks results.
38+
*
39+
* @param elapsed elapsed time in seconds
40+
*/
41+
void print_results( double elapsed ) {
42+
double rate = (double)ITERATIONS / elapsed;
43+
printf( " ---\n" );
44+
printf( " iterations: %d\n", ITERATIONS );
45+
printf( " elapsed: %0.9f\n", elapsed );
46+
printf( " rate: %0.9f\n", rate );
47+
printf( " ...\n" );
48+
}
49+
50+
/**
51+
* Returns a clock time.
52+
*
53+
* @return clock time
54+
*/
55+
double tic() {
56+
struct timeval now;
57+
gettimeofday( &now, NULL );
58+
return (double)now.tv_sec + (double)now.tv_usec/1.0e6;
59+
}
60+
61+
/**
62+
* Generates a random double on the interval [0,1].
63+
*
64+
* @return random double
65+
*/
66+
double rand_double() {
67+
int r = rand();
68+
return (double)r / ( (double)RAND_MAX + 1.0 );
69+
}
70+
71+
/**
72+
* Runs a benchmark.
73+
*
74+
* @return elapsed time in seconds
75+
*/
76+
double benchmark() {
77+
double elapsed;
78+
double x;
79+
double y;
80+
double t;
81+
int i;
82+
83+
t = tic();
84+
for ( i = 0; i < ITERATIONS; i++ ) {
85+
x = 0.0; // TODO
86+
y = 0.0; // TODO
87+
if ( y != y ) {
88+
printf( "should not return NaN\n" );
89+
break;
90+
}
91+
}
92+
elapsed = tic() - t;
93+
if ( y != y ) {
94+
printf( "should not return NaN\n" );
95+
}
96+
return elapsed;
97+
}
98+
99+
/**
100+
* Main execution sequence.
101+
*/
102+
int main( void ) {
103+
double elapsed;
104+
int i;
105+
106+
// Use the current time to seed the random number generator:
107+
srand( time( NULL ) );
108+
109+
print_version();
110+
for ( i = 0; i < REPEATS; i++ ) {
111+
printf( "# c::%s\n", NAME );
112+
elapsed = benchmark();
113+
print_results( elapsed );
114+
printf( "ok %d benchmark finished\n", i+1 );
115+
}
116+
print_summary( REPEATS, REPEATS );
117+
}
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/**
2+
* Benchmark `TODO`.
3+
*/
4+
#include <stdlib.h>
5+
#include <stdio.h>
6+
#include <math.h>
7+
#include <sys/time.h>
8+
#include "TODO.h"
9+
10+
#define NAME "TODO"
11+
#define ITERATIONS 10000000
12+
#define REPEATS 3
13+
#define MIN 1
14+
#define MAX 6
15+
16+
/**
17+
* Prints the TAP version.
18+
*/
19+
void print_version() {
20+
printf( "TAP version 13\n" );
21+
}
22+
23+
/**
24+
* Prints the TAP summary.
25+
*
26+
* @param total total number of tests
27+
* @param passing total number of passing tests
28+
*/
29+
void print_summary( int total, int passing ) {
30+
printf( "#\n" );
31+
printf( "1..%d\n", total ); // TAP plan
32+
printf( "# total %d\n", total );
33+
printf( "# pass %d\n", passing );
34+
printf( "#\n" );
35+
printf( "# ok\n" );
36+
}
37+
38+
/**
39+
* Prints benchmarks results.
40+
*
41+
* @param iterations number of iterations
42+
* @param elapsed elapsed time in seconds
43+
*/
44+
void print_results( int iterations, double elapsed ) {
45+
double rate = (double)iterations / elapsed;
46+
printf( " ---\n" );
47+
printf( " iterations: %d\n", iterations );
48+
printf( " elapsed: %0.9f\n", elapsed );
49+
printf( " rate: %0.9f\n", rate );
50+
printf( " ...\n" );
51+
}
52+
53+
/**
54+
* Returns a clock time.
55+
*
56+
* @return clock time
57+
*/
58+
double tic() {
59+
struct timeval now;
60+
gettimeofday( &now, NULL );
61+
return (double)now.tv_sec + (double)now.tv_usec/1.0e6;
62+
}
63+
64+
/**
65+
* Generates a random double on the interval [0,1].
66+
*
67+
* @return random double
68+
*/
69+
double rand_double() {
70+
int r = rand();
71+
return (double)r / ( (double)RAND_MAX + 1.0 );
72+
}
73+
74+
/**
75+
* Runs a benchmark.
76+
*
77+
* @param iterations number of iterations
78+
* @param len array length
79+
* @return elapsed time in seconds
80+
*/
81+
double benchmark( int iterations, int len ) {
82+
double elapsed;
83+
double x[ len ];
84+
double y;
85+
double t;
86+
int i;
87+
88+
for ( i = 0; i < len; i++ ) {
89+
x[ i ] = ( rand_double()*20000.0 ) - 10000.0;
90+
}
91+
t = tic();
92+
for ( i = 0; i < iterations; i++ ) {
93+
y = TODO( x );
94+
if ( TODO ) {
95+
printf( "unexpected return value\n" );
96+
break;
97+
}
98+
}
99+
elapsed = tic() - t;
100+
if ( TODO ) {
101+
printf( "unexpected return value\n" );
102+
}
103+
return elapsed;
104+
}
105+
106+
/**
107+
* Main execution sequence.
108+
*/
109+
int main( void ) {
110+
double elapsed;
111+
int count;
112+
int iter;
113+
int len;
114+
int i;
115+
int j;
116+
117+
// Use the current time to seed the random number generator:
118+
srand( time( NULL ) );
119+
120+
print_version();
121+
count = 0;
122+
for ( i = MIN; i <= MAX; i++ ) {
123+
len = pow( 10, i );
124+
iter = ITERATIONS / pow( 10, i-1 );
125+
for ( j = 0; j < REPEATS; j++ ) {
126+
count += 1;
127+
printf( "# c::native::%s:len=%d\n", NAME, len );
128+
elapsed = benchmark( iter, len );
129+
print_results( iter, elapsed );
130+
printf( "ok %d benchmark finished\n", count );
131+
}
132+
}
133+
print_summary( count, count );
134+
}

0 commit comments

Comments
 (0)