After being intrigued by this question I tried to copy the algorithm that the user Simon Nickerson suggested as well as the algorithm that the top answer provided from docjar: docjar algorithm.
I tested both out (which hopefully I did not make any performance breaking changes by removing the surrogate pair - dealing parts) and I found docjar algorithm to be almost four times faster than Simon's algorithm. Could anyone point out the significant difference between these two algorithms that has this impact on speed because I don't seem to be able to figure it out myself.
Cheers!
(Update) I did 100 trials using System.nanoTime() and I found docjar to average around 4380 nano seconds and the slower algorithm around 13684 nano seconds.
My modified docjar algorithm (4 times faster):
public static String fastReverse(String original)
{
char[] comps = original.toCharArray();
int n = comps.length - 1;
for(int j = (n - 1) >> 1 ; j >= 0; --j)
{
char temp = comps[j];
char temp2 = comps[n - j];
comps[j] = temp2;
comps[n - j] = temp;
}
return new String(comps);
}
Simon's modified Algorithm(slower):
public static String reverse(String original)
{
char[] comps = original.toCharArray();
int n = comps.length;
for(int i = 0; i < n / 2; i++)
{
char temp = comps[i];
comps[i] = comps[n - i - 1];
comps[n - i - 1] = temp;
}
return new String(comps);
}