-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package org.rancore.utils; | ||
|
||
public class ComplexityAnalyzer { | ||
|
||
// Interface for a method that we want to analyze | ||
@FunctionalInterface | ||
public interface MethodToAnalyze { | ||
void execute(); | ||
} | ||
|
||
public static void analyze(MethodToAnalyze method) { | ||
// Measure time complexity | ||
long startTime = System.nanoTime(); | ||
method.execute(); | ||
long endTime = System.nanoTime(); | ||
long duration = endTime - startTime; | ||
|
||
// Measure space complexity | ||
Runtime runtime = Runtime.getRuntime(); | ||
runtime.gc(); // Call garbage collector to get more accurate measurement | ||
long startMemory = runtime.totalMemory() - runtime.freeMemory(); | ||
method.execute(); | ||
long endMemory = runtime.totalMemory() - runtime.freeMemory(); | ||
long memoryUsed = endMemory - startMemory; | ||
|
||
// Print results | ||
System.out.println("Time taken: " + duration + " nanoseconds"); | ||
System.out.println("Memory used: " + memoryUsed + " bytes"); | ||
} | ||
|
||
public static void main(String[] args) { | ||
// Example usage: | ||
analyze(() -> sumArray(new int[]{1, 2, 3, 4, 5})); | ||
} | ||
|
||
// Example method to analyze | ||
public static int sumArray(int[] array) { | ||
int sum = 0; | ||
for (int num : array) { | ||
sum += num; | ||
} | ||
return sum; | ||
} | ||
} |