I have a performance issue related to string comparison (in Java).
I am working on a project that is supposed to sort a huge list (TableViewer in Eclipse). Anyway, I have identified a bottleneck for calling compareTo () for the string to be compared.
Is there a way to optimize string comparison performance? I searched and googled to no avail ...
Since the project is strictly limited to the Win32 environment, I thought it might be possible to use this ...
Any suggestions are welcome.
EDIT: I forgot to mention that I will need both a digital comparison and a literal string comparison.
EDIT2: The goal is to speed up the user interface, because it is unacceptable to wait a few seconds each time you click on the table title to sort. I am studying, perhaps, value caching to speed up the comparison. Since the strings are pretty static, I think that would be possible.
EDIT3: I know that many of you were concerned about the try () application - catch (). Actually, this is less of a concern, because even if I delete this code and execute only the catch block (one compareTo ()), it still runs at the same speed as the source code. If, however, I also comment on compareTo (); leaving me only the overhead of the comparison function (getting tags, etc.), it is lightning fast. So I still need a better way to compare strings. Either by caching, or by magic.
Unfortunately, changing the sorting algorithm is not possible, but I doubt it is slow because it manages to sort pure integers pretty quickly.
UPDATE:
The comparison function is implemented as part of the TableViewer environment for sorting operations, which means that I do not implement a specific sorting algorithm, but implements SWT / JFace. I only implement the comparison function.
What's even more interesting is the fact that sorting code doubles faster than string comparisons. Itโs faster to sort columns with only numbers than with actual literal rows .... Which leads me to the conclusion that something suspicious is happening in the compareTo () method ...
Here is the core of the function:
// e1Label and e2Label is Strings to be compared // // Be smart about the comparison and use non-lexical comparison if // possible (ie if both strings are actually numbers...) // // Warning: This is only "semi-smart" as the sorting might get "a bit" // messed up if some of the values in a column can be parsed as // doubles while others can not... // try { // Try using numeric (double) comparison of label values // double e1_double = Double.parseDouble(e1Label); double e2_double = Double.parseDouble(e2Label); rc = Double.compare(e1_double, e2_double); } catch (NumberFormatException e) { // Use lexical comparison if double comparison is not possible // rc = e1Label.compareToIgnoreCase(e2Label); }