What can I do in Java code to optimize for CPU caching?

后端 未结 5 1901
栀梦
栀梦 2020-12-12 18:53

When writing a Java program, do I have influence on how the CPU will utilize its cache to store my data? For example, if I have an array that is accessed a lot, does it help

5条回答
  •  北海茫月
    2020-12-12 19:43

    If you are down to where an improvement of a few percent makes a difference, use C where you'll get an improvement of 50-100%!

    If you think that the ease of use of Java makes it a better language to use, then don't screw it up with questionable optimizations.

    The good news is that Java will do a lot of stuff beneath the covers to improve your code at runtime, but it almost certainly won't do the kind of optimizations you're talking about.

    If you decide to go with Java, just write your code as clearly as you can, don't take minor optimizations into account at all. (Major ones like using the right collections for the right job, not allocating/freeing objects inside a loop, etc. are still worth while)

提交回复
热议问题