-
-
Notifications
You must be signed in to change notification settings - Fork 804
/
Copy pathbenchmark.js
65 lines (49 loc) · 1013 Bytes
/
benchmark.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
'use strict';
// MODULES //
var round = require( '@stdlib/math/base/special/round' );
var isnan = require( '@stdlib/math/base/utils/is-nan' );
var atan = require( './../lib' );
// VARIABLES //
var N = 1e7;
// FUNCTIONS //
function warmup() {
var i;
var j;
j = 0;
for ( i = 0; i < 1e6; i++ ) {
j += i * 2;
}
if ( j > 0 ) {
return true;
}
return false;
}
function test( name, fcn ) {
var start;
var time;
var bool;
var x;
var y;
var i;
bool = warmup();
if ( !bool ) {
throw new Error( 'Something went wrong.' );
}
start = process.hrtime();
for ( i = 0; i < N; i++ ) {
x = round( Math.random()*10000.0 );
y = fcn( x );
}
time = process.hrtime( start );
time = time[ 0 ] + time[ 1 ]*1.0e-9;
if ( isnan( y ) ) {
throw new Error( 'Something went wrong.' );
}
console.log( 'Test: %s. Elapsed time: %d sec. Iterations: %d. ops/s: %d.', name, time, N, N/time );
}
function benchmark() {
test( 'Math.atan', Math.atan );
test( 'atan', atan );
}
// MAIN //
benchmark();