Skip to content

Commit 3642de9

Browse files
committed
Added benchmarks
1 parent 425c9cb commit 3642de9

File tree

5 files changed

+301
-171
lines changed

5 files changed

+301
-171
lines changed

index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ export declare class NdArray {
1010
get size(): number
1111
get ndim(): number
1212
get dtype(): string
13-
toString(): string
1413
get(indices: Array<number>): number
1514
add(other: NdArray): NdArray
1615
addScalar(scalar: number): NdArray
16+
chain(): NdArray
1717
}

js_array_test.js

Lines changed: 75 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -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

85129
runBenchmark()

numpy_test.py

Lines changed: 62 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,75 @@
11
import numpy as np
22
import time
33

4-
def run_benchmark(iterations=1000000):
5-
start_time = time.time()
4+
def run_benchmark(iterations=20):
5+
print("\nRunning NumPy Benchmark with larger arrays and more complex operations")
6+
print("================================================================")
7+
8+
# Create larger arrays for more meaningful benchmarks
9+
size = 100000
10+
arr1 = np.arange(size)
11+
arr2 = np.arange(size) * 2
12+
13+
print(f"Created arrays with {size} elements each")
614

7-
# Create arrays
8-
arr1 = np.array([1, 2, 3], dtype=np.float64)
9-
arr2 = np.array([4, 5, 6], dtype=np.float64)
15+
# Test array properties
16+
print("\nArray properties:")
17+
print("Array 1 shape:", arr1.shape)
18+
print("Array 1 size:", arr1.size)
19+
print("Array 1 ndim:", arr1.ndim)
20+
print("Array 1 dtype:", arr1.dtype)
1021

22+
# Test computationally intensive operations
23+
print("\nRunning computationally intensive operations...")
24+
25+
# Test 1: Vector addition (element-wise)
26+
start_time = time.time()
27+
result = None
1128
for _ in range(iterations):
12-
# Array properties
13-
shape = arr1.shape
14-
size = arr1.size
15-
ndim = arr1.ndim
16-
dtype = arr1.dtype
29+
result = arr1 + arr2
30+
end_time = time.time()
31+
add_duration = end_time - start_time
1732

18-
# Element access
19-
elem = arr1[1]
33+
# Test 2: Scalar operations
34+
start_time = time.time()
35+
for _ in range(iterations):
36+
result = arr1 + 10.5
37+
end_time = time.time()
38+
scalar_duration = end_time - start_time
2039

21-
# Array operations
22-
sum_array = arr1 + arr2
23-
scalar_sum = arr1 + 10
40+
# Test 3: Multiple operations (equivalent to method chaining)
41+
start_time = time.time()
42+
for _ in range(iterations):
43+
result = arr1 + arr2 + 5.0 + arr1 + 2.5 + arr2
44+
end_time = time.time()
45+
multi_duration = end_time - start_time
2446

47+
# Test 4: Complex operations with multiple arrays
48+
start_time = time.time()
49+
for _ in range(iterations):
50+
# Create a new array for each iteration to simulate more complex operations
51+
temp_arr = np.arange(size) * 3
52+
result = arr1 + arr2 + 5.0 + temp_arr + 2.5
2553
end_time = time.time()
26-
duration = end_time - start_time
27-
28-
# Print results once
29-
print(f"NumPy Results (first iteration):")
30-
print(f"Array 1 shape: {arr1.shape}")
31-
print(f"Array 1 size: {arr1.size}")
32-
print(f"Array 1 ndim: {arr1.ndim}")
33-
print(f"Array 1 dtype: {arr1.dtype}")
34-
print(f"Element at index 1: {arr1[1]}")
35-
print(f"Sum of arrays: {arr1 + arr2}")
36-
print(f"Array + scalar: {arr1 + 10}")
37-
print(f"\nBenchmark completed in {duration:.4f} seconds")
38-
print(f"Average time per iteration: {(duration/iterations)*1000000:.2f} microseconds")
54+
complex_duration = end_time - start_time
55+
56+
# Print benchmark results
57+
print("\nBenchmark Results:")
58+
print(f"Vector addition ({iterations} iterations): {add_duration:.4f} seconds")
59+
print(f"Average time per addition: {(add_duration * 1000000 / iterations):.2f} microseconds")
60+
61+
print(f"\nScalar operations ({iterations} iterations): {scalar_duration:.4f} seconds")
62+
print(f"Average time per scalar operation: {(scalar_duration * 1000000 / iterations):.2f} microseconds")
63+
64+
print(f"\nMultiple operations ({iterations} iterations): {multi_duration:.4f} seconds")
65+
print(f"Average time per multiple operation: {(multi_duration * 1000000 / iterations):.2f} microseconds")
66+
67+
print(f"\nComplex operations ({iterations} iterations): {complex_duration:.4f} seconds")
68+
print(f"Average time per complex operation: {(complex_duration * 1000000 / iterations):.2f} microseconds")
69+
70+
# Print sample results
71+
print("\nSample results:")
72+
print("First 5 elements of result:", result[0], result[1], result[2], result[3], result[4])
3973

4074
if __name__ == "__main__":
4175
run_benchmark()

rust_ndarray_test.js

Lines changed: 76 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,88 @@
11
const { NdArray } = require('./index')
22

3-
function runBenchmark(iterations = 1000000) {
4-
const startTime = process.hrtime.bigint()
3+
function runBenchmark(iterations = 20) {
4+
console.log("\nRunning Rust NdArray Benchmark with larger arrays and more complex operations")
5+
console.log("================================================================")
56

6-
// Create arrays
7-
const arr1 = new NdArray([1, 2, 3])
8-
const arr2 = new NdArray([4, 5, 6])
7+
// Create larger arrays for more meaningful benchmarks
8+
const size = 100000
9+
const arr1 = new NdArray(Array(size).fill(0).map((_, i) => i))
10+
const arr2 = new NdArray(Array(size).fill(0).map((_, i) => i * 2))
911

10-
for (let i = 0; i < iterations; i++) {
11-
// Array properties
12-
const shape = arr1.shape
13-
const size = arr1.size
14-
const ndim = arr1.ndim
15-
const dtype = arr1.dtype
16-
17-
// Element access
18-
const elem = arr1.get([1])
19-
20-
// Array operations
21-
const sumArray = arr1.add(arr2)
22-
const scalarSum = arr1.addScalar(10)
23-
}
24-
25-
const endTime = process.hrtime.bigint()
26-
const duration = Number(endTime - startTime) / 1e9 // Convert to seconds
12+
console.log(`Created arrays with ${size} elements each`)
2713

28-
// Print results once
29-
console.log("Rust NdArray Results (first iteration):")
14+
// Test array properties
15+
console.log("\nArray properties:")
3016
console.log("Array 1 shape:", arr1.shape)
3117
console.log("Array 1 size:", arr1.size)
3218
console.log("Array 1 ndim:", arr1.ndim)
3319
console.log("Array 1 dtype:", arr1.dtype)
34-
console.log("Element at index 1:", arr1.get([1]))
35-
console.log("Sum of arrays:", arr1.add(arr2).toString())
36-
console.log("Array + scalar:", arr1.addScalar(10).toString())
37-
console.log(`\nBenchmark completed in ${duration.toFixed(4)} seconds`)
38-
console.log(`Average time per iteration: ${(duration * 1000000 / iterations).toFixed(2)} microseconds`)
20+
21+
// Test computationally intensive operations
22+
console.log("\nRunning computationally intensive operations...")
23+
24+
// Test 1: Vector addition (element-wise)
25+
const addStartTime = process.hrtime.bigint()
26+
let result
27+
for (let i = 0; i < iterations; i++) {
28+
result = arr1.add(arr2)
29+
}
30+
const addEndTime = process.hrtime.bigint()
31+
const addDuration = Number(addEndTime - addStartTime) / 1e9
32+
33+
// Test 2: Scalar operations
34+
const scalarStartTime = process.hrtime.bigint()
35+
for (let i = 0; i < iterations; i++) {
36+
result = arr1.addScalar(10.5)
37+
}
38+
const scalarEndTime = process.hrtime.bigint()
39+
const scalarDuration = Number(scalarEndTime - scalarStartTime) / 1e9
40+
41+
// Test 3: Method chaining with multiple operations
42+
const chainStartTime = process.hrtime.bigint()
43+
for (let i = 0; i < iterations; i++) {
44+
result = arr1.chain()
45+
.add(arr2)
46+
.addScalar(5.0)
47+
.add(arr1)
48+
.addScalar(2.5)
49+
.add(arr2)
50+
}
51+
const chainEndTime = process.hrtime.bigint()
52+
const chainDuration = Number(chainEndTime - chainStartTime) / 1e9
53+
54+
// Test 4: Complex operations with multiple arrays
55+
const complexStartTime = process.hrtime.bigint()
56+
for (let i = 0; i < iterations; i++) {
57+
// Create a new array for each iteration to simulate more complex operations
58+
const tempArr = new NdArray(Array(size).fill(0).map((_, i) => i * 3))
59+
result = arr1.chain()
60+
.add(arr2)
61+
.addScalar(5.0)
62+
.add(tempArr)
63+
.addScalar(2.5)
64+
}
65+
const complexEndTime = process.hrtime.bigint()
66+
const complexDuration = Number(complexEndTime - complexStartTime) / 1e9
67+
68+
// Print benchmark results
69+
console.log("\nBenchmark Results:")
70+
console.log(`Vector addition (${iterations} iterations): ${addDuration.toFixed(4)} seconds`)
71+
console.log(`Average time per addition: ${(addDuration * 1000000 / iterations).toFixed(2)} microseconds`)
72+
73+
console.log(`\nScalar operations (${iterations} iterations): ${scalarDuration.toFixed(4)} seconds`)
74+
console.log(`Average time per scalar operation: ${(scalarDuration * 1000000 / iterations).toFixed(2)} microseconds`)
75+
76+
console.log(`\nMethod chaining (${iterations} iterations): ${chainDuration.toFixed(4)} seconds`)
77+
console.log(`Average time per chain operation: ${(chainDuration * 1000000 / iterations).toFixed(2)} microseconds`)
78+
79+
console.log(`\nComplex operations (${iterations} iterations): ${complexDuration.toFixed(4)} seconds`)
80+
console.log(`Average time per complex operation: ${(complexDuration * 1000000 / iterations).toFixed(2)} microseconds`)
81+
82+
// Print sample results
83+
console.log("\nSample results:")
84+
console.log("First 5 elements of result:",
85+
result.get([0]), result.get([1]), result.get([2]), result.get([3]), result.get([4]))
3986
}
4087

4188
runBenchmark()

0 commit comments

Comments
 (0)