1- import { Bench , hrtimeNow } from "tinybench" ;
21import { init as initBootsharp } from "./bootsharp/init.mjs" ;
32import { init as initDotNet } from "./dotnet/init.mjs" ;
3+ import { init as initDotNetLLVM } from "./dotnet-llvm/init.mjs" ;
44import { init as initGo } from "./go/init.mjs" ;
55import { init as initRust } from "./rust/init.mjs" ;
66
@@ -11,19 +11,46 @@ import { init as initRust } from "./rust/init.mjs";
1111 * @property {(n: number) => number } fi
1212 */
1313
14- await run ( "Rust" , await initRust ( ) ) ;
15- await run ( "DotNet" , await initDotNet ( ) ) ;
16- await run ( "Bootsharp" , await initBootsharp ( ) ) ;
17- await run ( "Go" , await initGo ( ) ) ;
14+ const lang = process . argv [ 2 ] ;
15+ const baseline = [ ] ;
1816
19- /** @param {string } lang */
20- /** @param {Exports } exports */
17+ if ( ! lang || lang . toLowerCase ( ) === "rust" )
18+ await run ( "Rust" , await initRust ( ) ) ;
19+ if ( ! lang || lang . toLowerCase ( ) === "llvm" )
20+ await run ( ".NET LLVM" , await initDotNetLLVM ( ) ) ;
21+ if ( ! lang || lang . toLowerCase ( ) === "net" )
22+ await run ( ".NET" , await initDotNet ( ) ) ;
23+ if ( ! lang || lang . toLowerCase ( ) === "boot" )
24+ await run ( "Bootsharp" , await initBootsharp ( ) ) ;
25+ if ( ! lang || lang . toLowerCase ( ) === "go" )
26+ await run ( "Go" , await initGo ( ) ) ;
27+
28+ /** @param {string } lang
29+ * @param {Exports } exports */
2130async function run ( lang , exports ) {
22- console . log ( `Running ${ lang } ...` ) ;
23- const bench = new Bench ( { hrtimeNow } ) ;
24- bench . add ( "Echo Number" , exports . echoNumber ) ;
25- bench . add ( "Echo Struct" , exports . echoStruct ) ;
26- bench . add ( "Fibonacci" , ( ) => exports . fi ( 25 ) ) ;
27- await bench . run ( ) ;
28- console . table ( bench . table ( ) )
31+ console . log ( `\n\nBenching ${ lang } ...\n` ) ;
32+ console . log ( `Echo number: ${ iterate ( 0 , exports . echoNumber , 100 , 3 , 1000 ) } ` ) ;
33+ console . log ( `Echo struct: ${ iterate ( 1 , exports . echoStruct , 100 , 3 , 100 ) } ` ) ;
34+ console . log ( `Fibonacci: ${ iterate ( 2 , ( ) => exports . fi ( 33 ) , 100 , 3 , 1 ) } ` ) ;
35+ }
36+
37+ function iterate ( idx , fn , iterations , warms , loops ) {
38+ const results = [ ] ;
39+ warms *= - 1 ;
40+ for ( let i = warms ; i < iterations ; i ++ ) {
41+ const start = performance . now ( ) ;
42+ for ( let l = 0 ; l < loops ; l ++ ) fn ( ) ;
43+ if ( i >= 0 ) results . push ( performance . now ( ) - start ) ;
44+ }
45+ let media = median ( results ) ;
46+ if ( baseline [ idx ] ) return `${ ( media / baseline [ idx ] ) . toFixed ( 1 ) } ` ;
47+ else baseline [ idx ] = media ;
48+ return `${ media . toFixed ( 3 ) } ms` ;
49+ }
50+
51+ function median ( numbers ) {
52+ const sorted = [ ...numbers ] . sort ( ( a , b ) => a - b ) ;
53+ const middle = Math . floor ( sorted . length / 2 ) ;
54+ if ( sorted . length % 2 === 1 ) return sorted [ middle ] ;
55+ return ( sorted [ middle - 1 ] + sorted [ middle ] ) / 2 ;
2956}
0 commit comments