Skip to content

Commit 8f1fb02

Browse files
committed
Add C benchmark
1 parent 83f6a42 commit 8f1fb02

File tree

2 files changed

+205
-0
lines changed

2 files changed

+205
-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 $@ $< -lm
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: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/**
2+
* Benchmark `isnan`.
3+
*/
4+
#include <stdlib.h>
5+
#include <stdio.h>
6+
#include <math.h>
7+
#include <sys/time.h>
8+
9+
#define NAME "isnan"
10+
#define ITERATIONS 1000000
11+
#define REPEATS 3
12+
13+
/**
14+
* Prints the TAP version.
15+
*/
16+
void print_version() {
17+
printf( "TAP version 13\n" );
18+
}
19+
20+
/**
21+
* Prints the TAP summary.
22+
*
23+
* @param total total number of tests
24+
* @param passing total number of passing tests
25+
*/
26+
void print_summary( int total, int passing ) {
27+
printf( "#\n" );
28+
printf( "1..%d\n", total ); // TAP plan
29+
printf( "# total %d\n", total );
30+
printf( "# pass %d\n", passing );
31+
printf( "#\n" );
32+
printf( "# ok\n" );
33+
}
34+
35+
/**
36+
* Prints benchmarks results.
37+
*
38+
* @param elapsed elapsed time in seconds
39+
*/
40+
void print_results( double elapsed ) {
41+
double rate = (double)ITERATIONS / elapsed;
42+
printf( " ---\n" );
43+
printf( " iterations: %d\n", ITERATIONS );
44+
printf( " elapsed: %0.9f\n", elapsed );
45+
printf( " rate: %0.9f\n", rate );
46+
printf( " ...\n" );
47+
}
48+
49+
/**
50+
* Returns a clock time.
51+
*
52+
* @return clock time
53+
*/
54+
double tic() {
55+
struct timeval now;
56+
gettimeofday( &now, NULL );
57+
return (double)now.tv_sec + (double)now.tv_usec/1.0e6;
58+
}
59+
60+
/**
61+
* Generates a random double on the interval [0,1].
62+
*
63+
* @return random double
64+
*/
65+
double rand_double() {
66+
int r = rand();
67+
return (double)r / ( (double)RAND_MAX + 1.0 );
68+
}
69+
70+
/**
71+
* Runs a benchmark.
72+
*
73+
* @return elapsed time in seconds
74+
*/
75+
double benchmark() {
76+
double elapsed;
77+
double x;
78+
double t;
79+
int y;
80+
int i;
81+
82+
t = tic();
83+
for ( i = 0; i < ITERATIONS; i++ ) {
84+
x = ( 1.0e7*rand_double() ) - 5.0e6;
85+
y = isnan( x );
86+
if ( y != 0 && y != 1 ) {
87+
printf( "should return 0 or 1\n" );
88+
break;
89+
}
90+
}
91+
elapsed = tic() - t;
92+
if ( y != 0 && y != 1 ) {
93+
printf( "should return 0 or 1\n" );
94+
}
95+
return elapsed;
96+
}
97+
98+
/**
99+
* Main execution sequence.
100+
*/
101+
int main( void ) {
102+
double elapsed;
103+
int i;
104+
105+
// Use the current time to seed the random number generator:
106+
srand( time( NULL ) );
107+
108+
print_version();
109+
for ( i = 0; i < REPEATS; i++ ) {
110+
printf( "# c::%s\n", NAME );
111+
elapsed = benchmark();
112+
print_results( elapsed );
113+
printf( "ok %d benchmark finished\n", i+1 );
114+
}
115+
print_summary( REPEATS, REPEATS );
116+
}

0 commit comments

Comments
 (0)