JAVA
java
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
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;
public class TestReverseString {
public static void main(String[] args) {
String input = "How to make Java code faster";
testReverseStringBuilder(input);
testReverseAsByteArray(input);
testReverseToCharArray(input);
testReverseSwappingToCharArray(input);
testReverseListIteratorToCharArray(input);
testReverseStringBuffer(input);
}
public static void testReverseStringBuilder(String input) {
long startTime = System.nanoTime();
StringBuilder input1 = new StringBuilder(input);
System.out.println(input1.reverse());
long endTime= System.nanoTime();
System.out.println("Reverse and output string with StringBuilder(reverse method). Time: "+(endTime -startTime)/1000000.00+" miliseconds\n");
}
public static void testReverseAsByteArray(String input) {
long startTime = System.nanoTime();
byte [] strAsByteArray = input.getBytes();
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run