I am benchmarking the performance of iterative and recursive binary search algorithms in Java, specifically measuring both execution time and memory usage for different dataset sizes. However, I am encountering issues with accurately measuring memory usage during the execution of the search algorithms.
package backend;
import java.util.Arrays;
/**
* Benchmark for the performance of iterative and recursive binary search algorithms.
* It measures both execution time and memory usage for various dataset sizes.
*/
public class BinarySearchBenchmark {
private static final long[] EXTRA_ARRAY = new long[10_000_000];
static {
Arrays.fill(EXTRA_ARRAY, 1000);
}
/**
* The main method that runs the benchmarks for iterative and recursive binary search algorithms
* for different dataset sizes.
*
* @param args Command line arguments (not used)
*/
public static void main(String[] args) {
long[] dataset = new long[100_000];
for (long i = 0; i < dataset.length; i++) {
dataset[(int) i] = i;
}
long[] testSizes = {1_000, 5_000, 10_000, 25_000, 50_000, 100_000, 150_000, 200_000};
System.out.printf("%-15s %-20s %-20s %-20s %-20s%n",
"Input Size (n)", "Iterative Time (ms)", "Recursive Time (ms)",
"Iterative Memory (MB)", "Recursive Memory (MB)");
for (long size : testSizes) {
long[] subDataset = Arrays.copyOf(dataset, (int) size);
long iterativeTime = benchmark(() -> iterativeBinarySearch(subDataset, subDataset[(int) (size / 2)]));
long iterativeMemoryUsed = measureMemoryUsage(() -> iterativeBinarySearch(subDataset, subDataset[(int) (size / 2)]));
long recursiveTime = benchmark(() -> recursiveBinarySearch(subDataset, 0, (int) (subDataset.length - 1), subDataset[(int) (size / 2)]));
long recursiveMemoryUsed = measureMemoryUsage(() -> recursiveBinarySearch(subDataset, 0, (int) (subDataset.length - 1), subDataset[(int) (size / 2)]));
System.out.printf("%-15d %-20.2f %-20.2f %-20.2f %-20.2f%n",
size,
iterativeTime / 1_000_000.0,
recursiveTime / 1_000_000.0,
iterativeMemoryUsed / 1.0,
recursiveMemoryUsed / 1.0);
}
}
/**
* Performs the iterative binary search on the given array.
*
* @param array The array on which the search is performed
* @param target The target number to search for
*/
private static void iterativeBinarySearch(long[] array, long target) {
long low = 0, high = array.length - 1;
while (low <= high) {
long mid = low + (high - low) / 2;
if (array[(int) mid] == target) return;
if (array[(int) mid] < target) low = mid + 1;
else high = mid - 1;
}
}
/**
* Performs the recursive binary search on the given array.
*
* @param array The array on which the search is performed
* @param low The lowest index of the search range
* @param high The highest index of the search range
* @param target The target number to search for
* @return The index of the target number, or -1 if not found
*/
private static int recursiveBinarySearch(long[] array, long low, long high, long target) {
if (low > high) return -1;
long mid = low + (high - low) / 2;
if (array[(int) mid] == target) return (int) mid;
if (array[(int) mid] < target) return recursiveBinarySearch(array, mid + 1, high, target);
return recursiveBinarySearch(array, low, mid - 1, target);
}
/**
* Measures the time it takes to run the given search function.
*
* @param search The search function to measure
* @return The time in nanoseconds
*/
private static long benchmark(Runnable search) {
long startTime = System.nanoTime();
for (int i = 0; i < 100; i++) {
search.run();
}
return System.nanoTime() - startTime;
}
/**
* Measures the memory usage during the execution of the given search function.
*
* @param search The search function to measure
* @return The difference in memory usage in MB
*/
private static long measureMemoryUsage(Runnable search) {
System.gc();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
long beforeMemory = getUsedMemory();
search.run();
long afterMemory = getUsedMemory();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
return (afterMemory - beforeMemory) / (1024 * 1024);
}
/**
* Gets the used memory in the JVM in bytes.
*
* @return The used memory in bytes
*/
private static long getUsedMemory() {
return Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
}
}
The code uses System.gc() to trigger garbage collection and Thread.sleep(100) to allow GC to complete its work before measuring memory usage, but I am unsure whether this is accurately capturing the memory used by the search algorithms. Additionally, I am measuring memory usage before and after running the search 100 times, but I am unsure if the results reflect the true memory consumption of the algorithms.
Here are the methods I am using:
benchmark() – Measures execution time of the search algorithms by running them 100 times.
measureMemoryUsage() – Measures memory usage by comparing the used memory before and after the search algorithm execution, with garbage collection and a brief pause to let it complete.
Question:
How can I improve the accuracy of memory usage measurement for the iterative and recursive binary search algorithms in Java? Is there a more reliable way to measure memory usage during the execution of these algorithms?
What I Tried:
I used System.gc() to trigger garbage collection and Thread.sleep(100) to allow garbage collection to complete before measuring memory usage. This was done both before and after the search algorithm execution to capture the memory difference.
I measured memory usage by calculating the difference between the used memory before and after running the binary search 100 times, assuming that this would accurately reflect the memory consumed by the search algorithms.
I also tested both iterative and recursive binary search algorithms on various dataset sizes and measured both execution time and memory usage for comparison.
What I Expected:
I expected the measureMemoryUsage() method to give an accurate measurement of memory consumption during the execution of the binary search algorithms. I hoped to see a reliable difference in memory usage between the iterative and recursive implementations for different dataset sizes. The results would then help me compare the efficiency of the two approaches in terms of both time and memory usage.
Ersin Karaduman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3