3

Possible Duplicate:
Are there inline functions in java?

I come from C++, where I would write

for (int i = 0; i < numNonZero(); ++i)

knowing that numNonZero(), being very small and "inline" would simply vanish from the compiled code. How about in Java? Should I instead have an int _numNonZero in my class and write

for (int i = 0; i < _numNonZero; ++i) 

for maximum speed?

Community
  • 1
  • 1
Frank
  • 4,341
  • 8
  • 41
  • 57
  • 1
    What is meant by `being very small and "inline" would simply vanish from the compiled code.`? Vanish to where? – Caffeinated Jul 02 '12 at 15:33
  • Are you asking whether or not the compiler will optimise out the method call? The answer to that is no, in both javac and gcc. – lynks Jul 02 '12 at 15:33
  • 3
    Similar to http://stackoverflow.com/q/2096361/484072 – peacemaker Jul 02 '12 at 15:33
  • 1
    you have no control of inlining in Java it may be done at runtime by the JIT, also prefixing variable names with `_` in Java is not idiomatic Java and considered bad practice. –  Jul 02 '12 at 15:41
  • Lynks: that does not match my experience of looking at asm generated by gcc. – Frank Jul 02 '12 at 15:41
  • Adel: what I mean is that gcc can optimize numNonZero() to not generate a subroutine call in asm. – Frank Jul 02 '12 at 15:42

5 Answers5

10

Focus instead on writing the most readable code and let the HotSpot Java virtual machine optimize this at runtime.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
7

It does not matter. Java code is converted to executable code at runtime by the java virtual machine. Hotspot or other modern jvms has very advance inlining capabilities. For maximum performance I could recommend you to use -server flag, which will perform aggressive optimizations.

Deniz
  • 1,575
  • 1
  • 16
  • 27
  • 2
    Yep. It's the JIT's job to decide where things like this can be inlined, not the programmer's. – Louis Wasserman Jul 02 '12 at 15:37
  • 1
    The JIT doesn't always get it right. I just discovered it won't inline methods over 35 bytecodes. Manually doing this for one method in one loop of mine just saved me 15% cpu time. I look forward to the day when the JIT would have realised that :) – Sideshow Bob Oct 14 '14 at 21:15
5

Like the others said, you should worry first and foremost about readable code. However, to answer your question, although the compiler javac does no inlining, Hotspot--the JVM Optimizer--will inline it for you if it can.

Tim Pote
  • 27,191
  • 6
  • 63
  • 65
  • +1 for the better link, although mine offers the solace of noting that HotSpot is written in C++. :-) – trashgod Jul 02 '12 at 15:40
  • 1
    Well, the other day I was working with "Iterator" in Java, and I got a 5X speed-up by replacing them with ints. I'm suspicious of Java's capabilities now... – Frank Jul 02 '12 at 15:43
  • @Frank The JIT moves in mysterious ways. ;) – Tim Pote Jul 02 '12 at 15:45
  • @Frank: Fair enough, but `Integer` and `int` have different features. Java profilers are typically quite informative, allowing one to focus on the low-hanging fruit first.. – trashgod Jul 02 '12 at 16:04
3

The javac does almost no optimisations. Instead the optimisation are performed at runtime. You can see if a method has been inlined at runtime with -XX:+PrintCompilation

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 1
    The output of `-XX:+PrintCompilation` output is not very well documented. I found this article [About PrintCompilation](https://gist.github.com/1165804#file_notes.md) very useful. – Edwin Dalorzo Jul 02 '12 at 16:21
2

Recommended by Joshua Bloch in for(;;){;} :

for (int i = 0, n = numNonZero() ; i < n; ++i) { ..

If numNonZero() change (and return a String.length(), or made some computation), you code remain correct.

cl-r
  • 1,264
  • 1
  • 12
  • 26