@@ -44,42 +44,86 @@ class JsArray {
4444 }
4545}
4646
47- function runBenchmark ( iterations = 1000000 ) {
48- const startTime = process . hrtime . bigint ( )
47+ function runBenchmark ( iterations = 20 ) {
48+ console . log ( "\nRunning Pure JavaScript Array Benchmark with larger arrays and more complex operations" )
49+ console . log ( "================================================================" )
4950
50- // Create arrays
51- const arr1 = new JsArray ( [ 1 , 2 , 3 ] )
52- const arr2 = new JsArray ( [ 4 , 5 , 6 ] )
51+ // Create larger arrays for more meaningful benchmarks
52+ const size = 100000
53+ const arr1 = Array ( size ) . fill ( 0 ) . map ( ( _ , i ) => i )
54+ const arr2 = Array ( size ) . fill ( 0 ) . map ( ( _ , i ) => i * 2 )
5355
56+ console . log ( `Created arrays with ${ size } elements each` )
57+
58+ // Test array properties
59+ console . log ( "\nArray properties:" )
60+ console . log ( "Array 1 length:" , arr1 . length )
61+
62+ // Test computationally intensive operations
63+ console . log ( "\nRunning computationally intensive operations..." )
64+
65+ // Test 1: Vector addition (element-wise)
66+ const addStartTime = process . hrtime . bigint ( )
67+ let result
68+ for ( let i = 0 ; i < iterations ; i ++ ) {
69+ result = arr1 . map ( ( val , idx ) => val + arr2 [ idx ] )
70+ }
71+ const addEndTime = process . hrtime . bigint ( )
72+ const addDuration = Number ( addEndTime - addStartTime ) / 1e9
73+
74+ // Test 2: Scalar operations
75+ const scalarStartTime = process . hrtime . bigint ( )
76+ for ( let i = 0 ; i < iterations ; i ++ ) {
77+ result = arr1 . map ( val => val + 10.5 )
78+ }
79+ const scalarEndTime = process . hrtime . bigint ( )
80+ const scalarDuration = Number ( scalarEndTime - scalarStartTime ) / 1e9
81+
82+ // Test 3: Multiple operations (equivalent to method chaining)
83+ const multiStartTime = process . hrtime . bigint ( )
84+ for ( let i = 0 ; i < iterations ; i ++ ) {
85+ result = arr1
86+ . map ( ( val , idx ) => val + arr2 [ idx ] )
87+ . map ( val => val + 5.0 )
88+ . map ( ( val , idx ) => val + arr1 [ idx ] )
89+ . map ( val => val + 2.5 )
90+ . map ( ( val , idx ) => val + arr2 [ idx ] )
91+ }
92+ const multiEndTime = process . hrtime . bigint ( )
93+ const multiDuration = Number ( multiEndTime - multiStartTime ) / 1e9
94+
95+ // Test 4: Complex operations with multiple arrays
96+ const complexStartTime = process . hrtime . bigint ( )
5497 for ( let i = 0 ; i < iterations ; i ++ ) {
55- // Array properties
56- const shape = arr1 . shape
57- const size = arr1 . size
58- const ndim = arr1 . ndim
59- const dtype = arr1 . dtype
60-
61- // Element access
62- const elem = arr1 . get ( [ 1 ] )
63-
64- // Array operations
65- const sumArray = arr1 . add ( arr2 )
66- const scalarSum = arr1 . addScalar ( 10 )
98+ // Create a new array for each iteration to simulate more complex operations
99+ const tempArr = Array ( size ) . fill ( 0 ) . map ( ( _ , i ) => i * 3 )
100+ result = arr1
101+ . map ( ( val , idx ) => val + arr2 [ idx ] )
102+ . map ( val => val + 5.0 )
103+ . map ( ( val , idx ) => val + tempArr [ idx ] )
104+ . map ( val => val + 2.5 )
67105 }
106+ const complexEndTime = process . hrtime . bigint ( )
107+ const complexDuration = Number ( complexEndTime - complexStartTime ) / 1e9
108+
109+ // Print benchmark results
110+ console . log ( "\nBenchmark Results:" )
111+ console . log ( `Vector addition (${ iterations } iterations): ${ addDuration . toFixed ( 4 ) } seconds` )
112+ console . log ( `Average time per addition: ${ ( addDuration * 1000000 / iterations ) . toFixed ( 2 ) } microseconds` )
113+
114+ console . log ( `\nScalar operations (${ iterations } iterations): ${ scalarDuration . toFixed ( 4 ) } seconds` )
115+ console . log ( `Average time per scalar operation: ${ ( scalarDuration * 1000000 / iterations ) . toFixed ( 2 ) } microseconds` )
116+
117+ console . log ( `\nMultiple operations (${ iterations } iterations): ${ multiDuration . toFixed ( 4 ) } seconds` )
118+ console . log ( `Average time per multiple operation: ${ ( multiDuration * 1000000 / iterations ) . toFixed ( 2 ) } microseconds` )
119+
120+ console . log ( `\nComplex operations (${ iterations } iterations): ${ complexDuration . toFixed ( 4 ) } seconds` )
121+ console . log ( `Average time per complex operation: ${ ( complexDuration * 1000000 / iterations ) . toFixed ( 2 ) } microseconds` )
68122
69- const endTime = process . hrtime . bigint ( )
70- const duration = Number ( endTime - startTime ) / 1e9 // Convert to seconds
71-
72- // Print results once
73- console . log ( "Pure JavaScript Results (first iteration):" )
74- console . log ( "Array 1 shape:" , arr1 . shape )
75- console . log ( "Array 1 size:" , arr1 . size )
76- console . log ( "Array 1 ndim:" , arr1 . ndim )
77- console . log ( "Array 1 dtype:" , arr1 . dtype )
78- console . log ( "Element at index 1:" , arr1 . get ( [ 1 ] ) )
79- console . log ( "Sum of arrays:" , arr1 . add ( arr2 ) . toString ( ) )
80- console . log ( "Array + scalar:" , arr1 . addScalar ( 10 ) . toString ( ) )
81- console . log ( `\nBenchmark completed in ${ duration . toFixed ( 4 ) } seconds` )
82- console . log ( `Average time per iteration: ${ ( duration * 1000000 / iterations ) . toFixed ( 2 ) } microseconds` )
123+ // Print sample results
124+ console . log ( "\nSample results:" )
125+ console . log ( "First 5 elements of result:" ,
126+ result [ 0 ] , result [ 1 ] , result [ 2 ] , result [ 3 ] , result [ 4 ] )
83127}
84128
85129runBenchmark ( )
0 commit comments