Skip to content

Commit c521b13

Browse files
committed
chore: stuff from code review
1 parent b2d7b22 commit c521b13

File tree

5 files changed

+116
-43
lines changed

5 files changed

+116
-43
lines changed

lib/node_modules/@stdlib/math/base/special/cabsf/benchmark/c/benchmark.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@ static float rand_float( void ) {
9191
static double benchmark( void ) {
9292
float complex z;
9393
double elapsed;
94+
double t;
9495
float re;
9596
float im;
96-
double t;
9797
float y;
9898
int i;
9999

lib/node_modules/@stdlib/math/base/special/cabsf/benchmark/c/native/benchmark.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ static double benchmark( void ) {
9595
double elapsed;
9696
float re[ 100 ];
9797
float im[ 100 ];
98-
float y;
9998
double t;
99+
float y;
100100
int i;
101101

102102
for ( i = 0; i < 100; i++ ) {

lib/node_modules/@stdlib/math/base/special/cabsf/benchmark/julia/benchmark.jl

Lines changed: 109 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,58 +16,129 @@
1616
# See the License for the specific language governing permissions and
1717
# limitations under the License.
1818

19-
import JSON
19+
import BenchmarkTools
20+
using Printf
21+
22+
# Benchmark variables:
23+
name = "cabsf";
24+
repeats = 3;
2025

2126
"""
22-
gen( re, im, name )
27+
print_version()
28+
29+
Prints the TAP version.
2330
24-
Generate fixture data and write to file.
31+
# Examples
32+
33+
``` julia
34+
julia> print_version()
35+
```
36+
"""
37+
function print_version()
38+
@printf( "TAP version 13\n" );
39+
end
40+
41+
"""
42+
print_summary( total, passing )
43+
44+
Print the benchmark summary.
2545
2646
# Arguments
2747
28-
* `re`: domain (real components)
29-
* `im`: domain (imaginary components)
30-
* `name::AbstractString`: output filename
48+
* `total`: total number of tests
49+
* `passing`: number of passing tests
3150
3251
# Examples
3352
3453
``` julia
35-
julia> re = range( -1.0, stop = 1.0, length = 2001 );
36-
julia> im = range( -1.0, stop = 1.0, length = 2001 );
37-
julia> gen( re, im, \"data.json\" );
54+
julia> print_summary( 3, 3 )
3855
```
3956
"""
40-
function gen( re, im, name )
41-
z = complex.( re, im );
42-
y = Array{Float32}( undef, length(z) );
43-
for i in eachindex(z)
44-
y[i] = abs( ComplexF32( z[i] ) );
45-
end
57+
function print_summary( total, passing )
58+
@printf( "#\n" );
59+
@printf( "1..%d\n", total ); # TAP plan
60+
@printf( "# total %d\n", total );
61+
@printf( "# pass %d\n", passing );
62+
@printf( "#\n" );
63+
@printf( "# ok\n" );
64+
end
65+
66+
"""
67+
print_results( iterations, elapsed )
68+
69+
Print benchmark results.
70+
71+
# Arguments
72+
73+
* `iterations`: number of iterations
74+
* `elapsed`: elapsed time (in seconds)
75+
76+
# Examples
77+
78+
``` julia
79+
julia> print_results( 1000000, 0.131009101868 )
80+
```
81+
"""
82+
function print_results( iterations, elapsed )
83+
rate = iterations / elapsed
4684

47-
# Store data to be written to file as a collection:
48-
data = Dict([
49-
("re", re),
50-
("im", im),
51-
("expected", y)
52-
]);
53-
54-
# Based on the script directory, create an output filepath:
55-
filepath = joinpath( dir, name );
56-
57-
# Write the data to the output filepath as JSON:
58-
outfile = open( filepath, "w" );
59-
write( outfile, JSON.json(data) );
60-
write( outfile, "\n" );
61-
close( outfile );
85+
@printf( " ---\n" );
86+
@printf( " iterations: %d\n", iterations );
87+
@printf( " elapsed: %0.9f\n", elapsed );
88+
@printf( " rate: %0.9f\n", rate );
89+
@printf( " ...\n" );
6290
end
6391

64-
# Get the filename:
65-
file = @__FILE__;
92+
"""
93+
benchmark()
94+
95+
Run a benchmark.
96+
97+
# Notes
98+
99+
* Benchmark results are returned as a two-element array: [ iterations, elapsed ].
100+
* 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.
101+
* The elapsed time is in seconds.
102+
103+
# Examples
104+
105+
``` julia
106+
julia> out = benchmark();
107+
```
108+
"""
109+
function benchmark()
110+
t = BenchmarkTools.@benchmark abs( ComplexF32( (rand()*1000.0) - 500.0, (rand()*1000.0) - 500.0 ) ) samples=1e6
111+
112+
# Compute the total "elapsed" time and convert from nanoseconds to seconds:
113+
s = sum( t.times ) / 1.0e9;
114+
115+
# Determine the number of "iterations":
116+
iter = length( t.times );
66117

67-
# Extract the directory in which this file resides:
68-
dir = dirname( file );
118+
# Return the results:
119+
[ iter, s ];
120+
end
121+
122+
"""
123+
main()
124+
125+
Run benchmarks.
126+
127+
# Examples
128+
129+
``` julia
130+
julia> main();
131+
```
132+
"""
133+
function main()
134+
print_version();
135+
for i in 1:repeats
136+
@printf( "# julia::%s\n", name );
137+
results = benchmark();
138+
print_results( results[ 1 ], results[ 2 ] );
139+
@printf( "ok %d benchmark finished\n", i );
140+
end
141+
print_summary( repeats, repeats );
142+
end
69143

70-
# Generate fixture data:
71-
re = range( -1000.0, stop = 1000.0, length = 2003 );
72-
im = range( -1000.0, stop = 1000.0, length = 2003 );
73-
gen( re, im, "data.json" );
144+
main();

lib/node_modules/@stdlib/math/base/special/cabsf/benchmark/julia/data.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

lib/node_modules/@stdlib/math/base/special/cabsf/test/fixtures/julia/runner.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,11 @@ julia> gen( re, im, \"data.json\" );
3838
```
3939
"""
4040
function gen( re, im, name )
41-
z = Complex{ Float32 }.( Float32.( re ), Float32.( im ) );
42-
y = abs.( z );
41+
z = complex.( re, im );
42+
y = Array{Float32}( undef, length(z) );
43+
for i in eachindex(z)
44+
y[i] = abs( ComplexF32( z[i] ) );
45+
end
4346

4447
# Store data to be written to file as a collection:
4548
data = Dict([

0 commit comments

Comments
 (0)