Java performance tips

前端 未结 14 1581
傲寒
傲寒 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条回答
  • Also try tweaking the runtime arguments of the VM - the latest release of the VM for example includes the following flag which can improve performance in certain scenarios.

    -XX:+DoEscapeAnalysis 
    
    0 讨论(0)
  • 2021-02-01 20:50

    Here's what I would do, in any language. If samples show that your sort-comparison routine is active a large percentage of the time, you might find a way to simplify it. But maybe the time is going elsewhere. Diagnose first, to see what's broken, before you fix anything. Chances are, if you fix the biggest thing, then something else will be the biggest thing, and so on, until you've really gotten a pretty good speedup.

    0 讨论(0)
  • 2021-02-01 20:51

    Don't optimize prematurely.

    Measure performance, then optimize.

    Use final variables whenever possible. It will not only allow JVM to optimize more, but also make your code easier to read and maintain.

    If you make your objects immutable, you don't have to clone them.

    Optimize by changing the algorithm first, then by changing the implementation.

    Sometimes you need to resort to old-style techniques, like loop unrolling or caching precalculated values. Remember about them, even if they don't look nice, they can be useful.

    0 讨论(0)
  • 2021-02-01 20:51

    Profile and tune your java program and host machine. Most code follows 80/20 rule. That is 20% of code 80% of time, so find that 20% and make it as fast as possible. For example, the article Tuning Java Servers (http://www.infoq.com/articles/Tuning-Java-Servers) provides a description of drill down from command line and then isolate the problem using tools like Java Flight recorder, Eclipse Memory Analyser, and JProfiler.

    0 讨论(0)
  • 2021-02-01 20:52

    If your algorithm is CPU-heavy, you may want to consider taking advantage of parallelisation. You may be able to sort in multiple threads and merge the results back later.

    This is however not a decision to be taken lightly, as writing concurrent code is hard.

    0 讨论(0)
  • 2021-02-01 20:53

    Use a profiler:

    • visualvm ( free, limited )
    • jprofiler ( commercial )
    • yourkit java profiler ( commercial )
    • hprof ( free, limited, console only )

    Use the latest version of JVM from your provider. Incidentally Sun's Java 6 update 14 does bring performance improvements.

    Measure your GC throughput and pick the best garbage collector for your workload.

    0 讨论(0)
提交回复
热议问题