Java performance tips

前端 未结 14 1579
傲寒
傲寒 2021-02-01 20:04

I have a program I ported from C to Java. Both apps use quicksort to order some partitioned data (genomic coordinates).

The Java version runs fast, but I\'d like to get

14条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-01 20:37

    Is your sorting code executing only once, e.g. in a commandline utility that just sorts, or multiple times, e.g. a webapp that sorts in response to some user input?

    Chances are that performance would increase significantly after the code has been executed a few times because the HotSpot VM may optimize aggressively if it decides your code is a hotspot.

    This is a big advantage compared to C/C++.

    The VM, at runtime, optimizes code that is used often, and it does that quite well. Performance can actually rise beyond that of C/C++ because of this. Really. ;)

    Your custom Comparator could be a place for optimization, though.

    Try to check inexpensive stuff first (e.g. int comparison) before more expensive stuff (e.g. String comparison). I'm not sure if those tips apply because I don't know your Comparator.

    Use either Collections.sort(list, comparator) or Arrays.sort(array, comparator). The array variant will be a bit faster, see the respective documentation.

    As Andreas said before: don't try to outsmart the VM.

提交回复
热议问题