public class StringPerformance {
public static void main(String[] args) {
String str1 = "This is a short string.";
String str2 = "This is a much longer string for testing purposes.";
String str3 = "A very very very long string to measure performance.";
long startTime, endTime, duration;
// Measure performance of str1
startTime = System.nanoTime();
processString(str1);
endTime = System.nanoTime();
duration = (endTime - startTime);
System.out.println("String 1 performance: " + duration + " nanoseconds");
// Measure performance of str2
startTime = System.nanoTime();
processString(str2);
endTime = System.nanoTime();
duration = (endTime - startTime);
System.out.println("String 2 performance: " + duration + " nanoseconds");
// Measure performance of str3
startTime = System.nanoTime();
processString(str3);
endTime = System.nanoTime();
duration = (endTime - startTime);
System.out.println("String 3 performance: " + duration + " nanoseconds");
}
// Simulate a short-lived, synchronous task with string processing
public static void processString(String str) {
// Simulate some work being done on the string
for (int i = 0; i < 100000; i++) {
str.toUpperCase(); // Example synchronous operation - converting to uppercase
}
}
}
Add your comment