-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark-algo.ts
154 lines (130 loc) · 3.98 KB
/
benchmark-algo.ts
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import { performance } from 'perf_hooks';
import {
binarySearch,
exponentialSearch,
heapSort,
hybridSearch,
linearSearch,
mergeSort,
ternarySearch,
timSort,
} from './src/index.ts';
const DATA_SIZES = {
SMALL: 1000,
MEDIUM: 10000,
LARGE: 100000,
EXTRA_LARGE: 1000000,
};
type BenchmarkResult = {
algorithm: string;
operation: string;
size: number;
time: number;
};
function generateData(size: number): number[] {
return Array.from({ length: size }, () => Math.floor(Math.random() * size));
}
function benchmarkSortAlgorithms(data: number[], size: number): BenchmarkResult[] {
const results: BenchmarkResult[] = [];
const startHeapSort = performance.now();
heapSort([...data], (a, b) => a - b);
results.push({
algorithm: 'Heap Sort',
operation: 'Sort',
size,
time: performance.now() - startHeapSort,
});
const startMergeSort = performance.now();
mergeSort([...data], (a, b) => a - b);
results.push({
algorithm: 'Merge Sort',
operation: 'Sort',
size,
time: performance.now() - startMergeSort,
});
const startTimSort = performance.now();
timSort([...data], (a, b) => a - b);
results.push({
algorithm: 'Tim Sort',
operation: 'Sort',
size,
time: performance.now() - startTimSort,
});
const startNativeSort = performance.now();
[...data].sort((a, b) => a - b);
results.push({
algorithm: 'Native Sort',
operation: 'Sort',
size,
time: performance.now() - startNativeSort,
});
return results;
}
function benchmarkSearchAlgorithms(
data: number[],
target: number,
size: number,
): BenchmarkResult[] {
const results: BenchmarkResult[] = [];
// Ensure data is sorted for binary search variants
const sortedData = [...data].sort((a, b) => a - b);
const startBinarySearch = performance.now();
binarySearch(sortedData, target, { compareFn: (a, b) => a - b, isSorted: true });
results.push({
algorithm: 'Binary Search',
operation: 'Search',
size,
time: performance.now() - startBinarySearch,
});
const startExponentialSearch = performance.now();
exponentialSearch(sortedData, target, { compareFn: (a, b) => a - b, isSorted: true });
results.push({
algorithm: 'Exponential Search',
operation: 'Search',
size,
time: performance.now() - startExponentialSearch,
});
const startHybridSearch = performance.now();
hybridSearch(sortedData, target, { compareFn: (a, b) => a - b, isSorted: true });
results.push({
algorithm: 'Hybrid Search',
operation: 'Search',
size,
time: performance.now() - startHybridSearch,
});
const startLinearSearch = performance.now();
linearSearch(data, target, (a, b) => a - b);
results.push({
algorithm: 'Linear Search',
operation: 'Search',
size,
time: performance.now() - startLinearSearch,
});
const startTernarySearch = performance.now();
ternarySearch(sortedData, target, 0, sortedData.length - 1, {
compareFn: (a, b) => a - b,
isSorted: true,
});
results.push({
algorithm: 'Ternary Search',
operation: 'Search',
size,
time: performance.now() - startTernarySearch,
});
return results;
}
function runBenchmarks(): BenchmarkResult[] {
const allResults: BenchmarkResult[] = [];
for (const [, size] of Object.entries(DATA_SIZES)) {
const data = generateData(size);
const target = data[Math.floor(Math.random() * size)];
// Sorting Benchmarks
allResults.push(...benchmarkSortAlgorithms(data, size));
// Searching Benchmarks
allResults.push(...benchmarkSearchAlgorithms(data, target, size));
}
return allResults;
}
// Run the benchmark and log the results
const benchmarkResults = runBenchmarks();
console.table(benchmarkResults);