chore: update benchmark suite to include new sorting algorithm tests
This commit is contained in:
+175
-3
@@ -1,4 +1,4 @@
|
||||
public class Benchmark {
|
||||
class Benchmark {
|
||||
public static int fibonacci(int n) {
|
||||
if (n <= 1) {
|
||||
return n;
|
||||
@@ -187,7 +187,120 @@ public class Benchmark {
|
||||
return arr[0];
|
||||
}
|
||||
|
||||
public static int main() {
|
||||
public static int quickSortPartition(int[] arr, int low, int high) {
|
||||
int pivot = arr[high];
|
||||
int i = low - 1;
|
||||
for (int j = low; j < high; j++) {
|
||||
if (arr[j] < pivot) {
|
||||
i++;
|
||||
int swap = arr[i];
|
||||
arr[i] = arr[j];
|
||||
arr[j] = swap;
|
||||
}
|
||||
}
|
||||
int swap2 = arr[i + 1];
|
||||
arr[i + 1] = arr[high];
|
||||
arr[high] = swap2;
|
||||
return i + 1;
|
||||
}
|
||||
|
||||
public static int quickSortHelper(int[] arr, int low, int high) {
|
||||
if (low < high) {
|
||||
int pi = quickSortPartition(arr, low, high);
|
||||
quickSortHelper(arr, low, pi - 1);
|
||||
quickSortHelper(arr, pi + 1, high);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static int quickSort(int size) {
|
||||
int[] arr = new int[size];
|
||||
for (int i = 0; i < size; i++) {
|
||||
arr[i] = size - i;
|
||||
}
|
||||
quickSortHelper(arr, 0, size - 1);
|
||||
return arr[0];
|
||||
}
|
||||
|
||||
public static int binarySearch(int[] arr, int target, int low, int high) {
|
||||
if (low > high) {
|
||||
return -1;
|
||||
}
|
||||
int mid = low + (high - low) / 2;
|
||||
if (arr[mid] == target) {
|
||||
return mid;
|
||||
}
|
||||
if (arr[mid] > target) {
|
||||
return binarySearch(arr, target, low, mid - 1);
|
||||
}
|
||||
return binarySearch(arr, target, mid + 1, high);
|
||||
}
|
||||
|
||||
public static int binarySearchBench(int size) {
|
||||
int[] arr = new int[size];
|
||||
for (int i = 0; i < size; i++) {
|
||||
arr[i] = i;
|
||||
}
|
||||
int sum = 0;
|
||||
for (int ii = 0; ii < size; ii = ii + 100) {
|
||||
int result = binarySearch(arr, ii, 0, size - 1);
|
||||
sum = sum + result;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
public static int stringConcat(int iterations) {
|
||||
int len = 0;
|
||||
for (int i = 0; i < iterations; i++) {
|
||||
String s = "Hello";
|
||||
s = s + "World";
|
||||
s = s + i;
|
||||
len = len + s.length();
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
public static int bitwiseOps(int iterations) {
|
||||
int result = 0;
|
||||
for (int i = 1; i < iterations; i++) {
|
||||
result = result ^ (i << 1);
|
||||
result = result | (i >> 1);
|
||||
result = result & (i >>> 2);
|
||||
result = result ^ ~i;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static int arrayCopy(int size) {
|
||||
int[] src = new int[size];
|
||||
for (int i = 0; i < size; i++) {
|
||||
src[i] = i;
|
||||
}
|
||||
int[] dst = new int[size];
|
||||
for (int ii = 0; ii < size; ii++) {
|
||||
dst[ii] = src[ii];
|
||||
}
|
||||
return dst[size - 1];
|
||||
}
|
||||
|
||||
public static int insertionSort(int size) {
|
||||
int[] arr = new int[size];
|
||||
for (int i = 0; i < size; i++) {
|
||||
arr[i] = size - i;
|
||||
}
|
||||
for (int ii = 1; ii < size; ii++) {
|
||||
int key = arr[ii];
|
||||
int j = ii - 1;
|
||||
while (j >= 0 && arr[j] > key) {
|
||||
arr[j + 1] = arr[j];
|
||||
j = j - 1;
|
||||
}
|
||||
arr[j + 1] = key;
|
||||
}
|
||||
return arr[0];
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("=== Rava Comprehensive Benchmark Suite ===");
|
||||
|
||||
long start = System.nanoTime();
|
||||
@@ -340,7 +453,66 @@ public class Benchmark {
|
||||
System.out.print(bubbleTime);
|
||||
System.out.println(" ms");
|
||||
|
||||
start = System.nanoTime();
|
||||
int quick = quickSort(1000);
|
||||
end = System.nanoTime();
|
||||
long quickTime = (end - start) / 1000000;
|
||||
System.out.print("Quick sort (1000 elements) = ");
|
||||
System.out.println(quick);
|
||||
System.out.print("Time: ");
|
||||
System.out.print(quickTime);
|
||||
System.out.println(" ms");
|
||||
|
||||
start = System.nanoTime();
|
||||
int insertion = insertionSort(3000);
|
||||
end = System.nanoTime();
|
||||
long insertionTime = (end - start) / 1000000;
|
||||
System.out.print("Insertion sort (3000 elements) = ");
|
||||
System.out.println(insertion);
|
||||
System.out.print("Time: ");
|
||||
System.out.print(insertionTime);
|
||||
System.out.println(" ms");
|
||||
|
||||
start = System.nanoTime();
|
||||
int binsearch = binarySearchBench(100000);
|
||||
end = System.nanoTime();
|
||||
long binsearchTime = (end - start) / 1000000;
|
||||
System.out.print("Binary search (100K, 1K searches) = ");
|
||||
System.out.println(binsearch);
|
||||
System.out.print("Time: ");
|
||||
System.out.print(binsearchTime);
|
||||
System.out.println(" ms");
|
||||
|
||||
start = System.nanoTime();
|
||||
int strconcat = stringConcat(50000);
|
||||
end = System.nanoTime();
|
||||
long strconcatTime = (end - start) / 1000000;
|
||||
System.out.print("String concatenation (50K) = ");
|
||||
System.out.println(strconcat);
|
||||
System.out.print("Time: ");
|
||||
System.out.print(strconcatTime);
|
||||
System.out.println(" ms");
|
||||
|
||||
start = System.nanoTime();
|
||||
int bitwise = bitwiseOps(10000000);
|
||||
end = System.nanoTime();
|
||||
long bitwiseTime = (end - start) / 1000000;
|
||||
System.out.print("Bitwise operations (10M) = ");
|
||||
System.out.println(bitwise);
|
||||
System.out.print("Time: ");
|
||||
System.out.print(bitwiseTime);
|
||||
System.out.println(" ms");
|
||||
|
||||
start = System.nanoTime();
|
||||
int arrcopy = arrayCopy(1000000);
|
||||
end = System.nanoTime();
|
||||
long arrcopyTime = (end - start) / 1000000;
|
||||
System.out.print("Array copy (1M elements) = ");
|
||||
System.out.println(arrcopy);
|
||||
System.out.print("Time: ");
|
||||
System.out.print(arrcopyTime);
|
||||
System.out.println(" ms");
|
||||
|
||||
System.out.println("=== Benchmark Complete ===");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
#!/usr/bin/env python3
|
||||
import time
|
||||
import sys
|
||||
|
||||
sys.setrecursionlimit(10000)
|
||||
|
||||
def fibonacci(n):
|
||||
if n <= 1:
|
||||
@@ -136,6 +139,85 @@ def bubble_sort(size):
|
||||
|
||||
return arr[0]
|
||||
|
||||
def quick_sort_partition(arr, low, high):
|
||||
pivot = arr[high]
|
||||
i = low - 1
|
||||
for j in range(low, high):
|
||||
if arr[j] < pivot:
|
||||
i += 1
|
||||
arr[i], arr[j] = arr[j], arr[i]
|
||||
arr[i + 1], arr[high] = arr[high], arr[i + 1]
|
||||
return i + 1
|
||||
|
||||
def quick_sort_helper(arr, low, high):
|
||||
if low < high:
|
||||
pi = quick_sort_partition(arr, low, high)
|
||||
quick_sort_helper(arr, low, pi - 1)
|
||||
quick_sort_helper(arr, pi + 1, high)
|
||||
|
||||
def quick_sort(size):
|
||||
arr = [0] * size
|
||||
for i in range(size):
|
||||
arr[i] = size - i
|
||||
quick_sort_helper(arr, 0, size - 1)
|
||||
return arr[0]
|
||||
|
||||
def binary_search(arr, target, low, high):
|
||||
if low > high:
|
||||
return -1
|
||||
mid = low + (high - low) // 2
|
||||
if arr[mid] == target:
|
||||
return mid
|
||||
if arr[mid] > target:
|
||||
return binary_search(arr, target, low, mid - 1)
|
||||
return binary_search(arr, target, mid + 1, high)
|
||||
|
||||
def binary_search_bench(size):
|
||||
arr = [i for i in range(size)]
|
||||
total = 0
|
||||
for i in range(0, size, 100):
|
||||
result = binary_search(arr, i, 0, size - 1)
|
||||
total += result
|
||||
return total
|
||||
|
||||
def string_concat(iterations):
|
||||
length = 0
|
||||
for i in range(iterations):
|
||||
s = "Hello"
|
||||
s = s + "World"
|
||||
s = s + str(i)
|
||||
length += len(s)
|
||||
return length
|
||||
|
||||
def bitwise_ops(iterations):
|
||||
result = 0
|
||||
for i in range(1, iterations):
|
||||
result ^= (i << 1)
|
||||
result |= (i >> 1)
|
||||
result &= (i >> 2)
|
||||
result ^= ~i
|
||||
return result
|
||||
|
||||
def array_copy(size):
|
||||
src = [i for i in range(size)]
|
||||
dst = [0] * size
|
||||
for i in range(size):
|
||||
dst[i] = src[i]
|
||||
return dst[size - 1]
|
||||
|
||||
def insertion_sort(size):
|
||||
arr = [0] * size
|
||||
for i in range(size):
|
||||
arr[i] = size - i
|
||||
for i in range(1, size):
|
||||
key = arr[i]
|
||||
j = i - 1
|
||||
while j >= 0 and arr[j] > key:
|
||||
arr[j + 1] = arr[j]
|
||||
j -= 1
|
||||
arr[j + 1] = key
|
||||
return arr[0]
|
||||
|
||||
def main():
|
||||
print("=== Python Comprehensive Benchmark Suite ===")
|
||||
|
||||
@@ -244,6 +326,48 @@ def main():
|
||||
print(f"Bubble sort (5000 elements) = {bubble}")
|
||||
print(f"Time: {bubble_time} ms")
|
||||
|
||||
start = time.perf_counter_ns()
|
||||
quick = quick_sort(1000)
|
||||
end = time.perf_counter_ns()
|
||||
quick_time = (end - start) // 1000000
|
||||
print(f"Quick sort (1000 elements) = {quick}")
|
||||
print(f"Time: {quick_time} ms")
|
||||
|
||||
start = time.perf_counter_ns()
|
||||
insertion = insertion_sort(3000)
|
||||
end = time.perf_counter_ns()
|
||||
insertion_time = (end - start) // 1000000
|
||||
print(f"Insertion sort (3000 elements) = {insertion}")
|
||||
print(f"Time: {insertion_time} ms")
|
||||
|
||||
start = time.perf_counter_ns()
|
||||
binsearch = binary_search_bench(100000)
|
||||
end = time.perf_counter_ns()
|
||||
binsearch_time = (end - start) // 1000000
|
||||
print(f"Binary search (100K, 1K searches) = {binsearch}")
|
||||
print(f"Time: {binsearch_time} ms")
|
||||
|
||||
start = time.perf_counter_ns()
|
||||
strconcat = string_concat(50000)
|
||||
end = time.perf_counter_ns()
|
||||
strconcat_time = (end - start) // 1000000
|
||||
print(f"String concatenation (50K) = {strconcat}")
|
||||
print(f"Time: {strconcat_time} ms")
|
||||
|
||||
start = time.perf_counter_ns()
|
||||
bitwise = bitwise_ops(10000000)
|
||||
end = time.perf_counter_ns()
|
||||
bitwise_time = (end - start) // 1000000
|
||||
print(f"Bitwise operations (10M) = {bitwise}")
|
||||
print(f"Time: {bitwise_time} ms")
|
||||
|
||||
start = time.perf_counter_ns()
|
||||
arrcopy = array_copy(1000000)
|
||||
end = time.perf_counter_ns()
|
||||
arrcopy_time = (end - start) // 1000000
|
||||
print(f"Array copy (1M elements) = {arrcopy}")
|
||||
print(f"Time: {arrcopy_time} ms")
|
||||
|
||||
print("=== Benchmark Complete ===")
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user