Skip to content

Commit 4264040

Browse files
committed
feat: benchmarks added
1 parent f5ef8a0 commit 4264040

File tree

4 files changed

+394
-0
lines changed

4 files changed

+394
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var randu = require( '@stdlib/random/base/randu' );
25+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
26+
var Complex64 = require( '@stdlib/complex/float32/ctor' );
27+
var real = require( '@stdlib/complex/float32/real' );
28+
var imag = require( '@stdlib/complex/float32/imag' );
29+
var uniform = require( '@stdlib/random/base/uniform' );
30+
var isArray = require( '@stdlib/assert/is-array' );
31+
var floorf = require( '@stdlib/math/base/special/floorf' );
32+
var pkg = require( './../package.json' ).name;
33+
var cfloorf = require( './../lib' );
34+
35+
36+
// MAIN //
37+
38+
bench( pkg, function benchmark( b ) {
39+
var values;
40+
var y;
41+
var i;
42+
43+
values = [
44+
new Complex64( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) ),
45+
new Complex64( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) )
46+
];
47+
48+
b.tic();
49+
for ( i = 0; i < b.iterations; i++ ) {
50+
y = cfloorf( ( values[ i%values.length ] ) );
51+
if ( isnanf( real( y ) ) ) {
52+
b.fail( 'should not return NaN' );
53+
}
54+
}
55+
b.toc();
56+
if ( isnanf( imag( y ) ) ) {
57+
b.fail( 'should not return NaN' );
58+
}
59+
b.pass( 'benchmark finished' );
60+
b.end();
61+
});
62+
63+
bench( pkg+'::manual', function benchmark( b ) {
64+
var re;
65+
var im;
66+
var y;
67+
var i;
68+
69+
b.tic();
70+
for ( i = 0; i < b.iterations; i++ ) {
71+
re = ( randu()*1000.0 ) - 500.0;
72+
im = ( randu()*1000.0 ) - 500.0;
73+
y = [ floorf( re ), floorf( im ) ];
74+
if ( y.length === 0 ) {
75+
b.fail( 'should not be empty' );
76+
}
77+
}
78+
b.toc();
79+
if ( !isArray( y ) ) {
80+
b.fail( 'should return an array' );
81+
}
82+
b.pass( 'benchmark finished' );
83+
b.end();
84+
});
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var resolve = require( 'path' ).resolve;
24+
var bench = require( '@stdlib/bench' );
25+
var uniform = require( '@stdlib/random/base/uniform' );
26+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
27+
var Complex64 = require( '@stdlib/complex/float32/ctor' );
28+
var real = require( '@stdlib/complex/float32/real' );
29+
var tryRequire = require( '@stdlib/utils/try-require' );
30+
var pkg = require( './../package.json' ).name;
31+
32+
33+
// VARIABLES //
34+
35+
var cfloorf = tryRequire( resolve( __dirname, './../lib/native.js' ) );
36+
var opts = {
37+
'skip': ( cfloorf instanceof Error )
38+
};
39+
40+
41+
// MAIN //
42+
43+
bench( pkg+'::native', opts, function benchmark( b ) {
44+
var values;
45+
var y;
46+
var i;
47+
48+
values = [
49+
new Complex64( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) ),
50+
new Complex64( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) )
51+
];
52+
53+
b.tic();
54+
for ( i = 0; i < b.iterations; i++ ) {
55+
y = cfloorf( values[ i%values.length ] );
56+
if ( isnanf( real( y ) ) ) {
57+
b.fail( 'should not return NaN' );
58+
}
59+
}
60+
b.toc();
61+
if ( isnanf( real( y ) ) ) {
62+
b.fail( 'should not return NaN' );
63+
}
64+
b.pass( 'benchmark finished' );
65+
b.end();
66+
});
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#/
2+
# @license Apache-2.0
3+
#
4+
# Copyright (c) 2018 The Stdlib Authors.
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#/
18+
19+
20+
# VARIABLES #
21+
22+
ifndef VERBOSE
23+
QUIET := @
24+
endif
25+
26+
# Determine the OS:
27+
#
28+
# [1]: https://en.wikipedia.org/wiki/Uname#Examples
29+
# [2]: http://stackoverflow.com/a/27776822/2225624
30+
OS ?= $(shell uname)
31+
ifneq (, $(findstring MINGW,$(OS)))
32+
OS := WINNT
33+
else
34+
ifneq (, $(findstring MSYS,$(OS)))
35+
OS := WINNT
36+
else
37+
ifneq (, $(findstring CYGWIN,$(OS)))
38+
OS := WINNT
39+
endif
40+
endif
41+
endif
42+
43+
# Define the program used for compiling C source files:
44+
ifdef C_COMPILER
45+
CC := $(C_COMPILER)
46+
else
47+
CC := gcc
48+
endif
49+
50+
# Define the command-line options when compiling C files:
51+
CFLAGS ?= \
52+
-std=c99 \
53+
-O3 \
54+
-Wall \
55+
-pedantic
56+
57+
# Determine whether to generate [position independent code][1]:
58+
#
59+
# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options
60+
# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option
61+
ifeq ($(OS), WINNT)
62+
fPIC ?=
63+
else
64+
fPIC ?= -fPIC
65+
endif
66+
67+
# List of C targets:
68+
c_targets := benchmark.out
69+
70+
71+
# TARGETS #
72+
73+
# Default target.
74+
#
75+
# This target is the default target.
76+
77+
all: $(c_targets)
78+
79+
.PHONY: all
80+
81+
82+
# Compile C source.
83+
#
84+
# This target compiles C source files.
85+
86+
$(c_targets): %.out: %.c
87+
$(QUIET) $(CC) $(CFLAGS) $(fPIC) -o $@ $< -lm
88+
89+
90+
# Run a benchmark.
91+
#
92+
# This target runs a benchmark.
93+
94+
run: $(c_targets)
95+
$(QUIET) ./$<
96+
97+
.PHONY: run
98+
99+
100+
# Perform clean-up.
101+
#
102+
# This target removes generated files.
103+
104+
clean:
105+
$(QUIET) -rm -f *.o *.out
106+
107+
.PHONY: clean
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#include <stdlib.h>
20+
#include <stdio.h>
21+
#include <math.h>
22+
#include <complex.h>
23+
#include <sys/time.h>
24+
25+
#define NAME "cfloorf"
26+
#define ITERATIONS 1000000
27+
#define REPEATS 3
28+
29+
/**
30+
* Prints the TAP version.
31+
*/
32+
static void print_version( void ) {
33+
printf( "TAP version 13\n" );
34+
}
35+
36+
/**
37+
* Prints the TAP summary.
38+
*
39+
* @param total total number of tests
40+
* @param passing total number of passing tests
41+
*/
42+
static void print_summary( int total, int passing ) {
43+
printf( "#\n" );
44+
printf( "1..%d\n", total ); // TAP plan
45+
printf( "# total %d\n", total );
46+
printf( "# pass %d\n", passing );
47+
printf( "#\n" );
48+
printf( "# ok\n" );
49+
}
50+
51+
/**
52+
* Prints benchmarks results.
53+
*
54+
* @param elapsed elapsed time in seconds
55+
*/
56+
static void print_results( double elapsed ) {
57+
double rate = (double)ITERATIONS / elapsed;
58+
printf( " ---\n" );
59+
printf( " iterations: %d\n", ITERATIONS );
60+
printf( " elapsed: %0.9f\n", elapsed );
61+
printf( " rate: %0.9f\n", rate );
62+
printf( " ...\n" );
63+
}
64+
65+
/**
66+
* Returns a clock time.
67+
*
68+
* @return clock time
69+
*/
70+
static double tic( void ) {
71+
struct timeval now;
72+
gettimeofday( &now, NULL );
73+
return (double)now.tv_sec + (double)now.tv_usec/1.0e6;
74+
}
75+
76+
/**
77+
* Generates a random number on the interval [0,1).
78+
*
79+
* @return random number
80+
*/
81+
static float rand_float( void ) {
82+
int r = rand();
83+
return (float)r / ( (float)RAND_MAX + 1.0f );
84+
}
85+
86+
/**
87+
* Runs a benchmark.
88+
*
89+
* @return elapsed time in seconds
90+
*/
91+
static double benchmark( void ) {
92+
double elapsed;
93+
float re;
94+
float im;
95+
double t;
96+
int i;
97+
98+
float complex z;
99+
float complex y;
100+
101+
t = tic();
102+
for ( i = 0; i < ITERATIONS; i++ ) {
103+
re = ( 1000.0f * rand_float() ) - 500.0f;
104+
im = ( 1000.0f * rand_float() ) - 500.0f;
105+
z = re + im*I;
106+
y = floorf( crealf(z) ) + floorf( cimagf(z) )*I;
107+
if ( y != y ) {
108+
printf( "should not return NaN\n" );
109+
break;
110+
}
111+
}
112+
elapsed = tic() - t;
113+
if ( y != y ) {
114+
printf( "should not return NaN\n" );
115+
}
116+
return elapsed;
117+
}
118+
119+
/**
120+
* Main execution sequence.
121+
*/
122+
int main( void ) {
123+
double elapsed;
124+
int i;
125+
126+
// Use the current time to seed the random number generator:
127+
srand( time( NULL ) );
128+
129+
print_version();
130+
for ( i = 0; i < REPEATS; i++ ) {
131+
printf( "# c::%s\n", NAME );
132+
elapsed = benchmark();
133+
print_results( elapsed );
134+
printf( "ok %d benchmark finished\n", i+1 );
135+
}
136+
print_summary( REPEATS, REPEATS );
137+
}

0 commit comments

Comments
 (0)