views:

342

answers:

5

Can someone come up with a disassembled machine code assembler listing?

I mean there must be some overhead compared to the normal function call in C.

The VM needs to track calls to find hotspots and when it uses compiled code it needs to provid ways to change the compiled method on fly if a new loaded class requires a recompilation.

I guess there is also a return stack overflow check somewhere.

EDIT: I think i should mention that i'm not a java programmer but a compiler writer and want to find out why java applications are so slow while the java micro benchmarks are comparable to native compiled code. So looking at some the details is important for me here, even if they take only a few dozend nanoseconds and a few additional instructions.

+4  A: 

Java doesn't compile directly to machine code, it compiles to bytecode which is then either interpreted or compiled to machine code at runtime--I have no idea how to get to the machine code at runtime, I just imagine it as this huge mass of shifting, changing bytes that just ends up executing DAMN quickly and reliably.

A small method call should compile out completely at runtime. Even a large method call can be written as in-line machine code by the VM if enough references can be resolved or ignored.

Using Final can help a lot because it gives the VM hints as to how it might optimize even more.

Since a method call can actually compile out completely and at best has a minimal cost anyway--you really shouldn't worry about it. Just code your best and worry about performance problems when you have a failing performance spec (at which point spot-optimizing will do MUCH better than trying to eliminate method calls across your code, ruining your codebase for everyone involved).

Note that because of the runtime analysis, it can actually be faster in some very rare cases than similar code in C (The c compiler won't profile at runtime and hand-optimize your code for you, you have to do all that yourself).

Bill K
Thats why i'm using the VM word in the subject. My point is i'm a compiler writer myself so i'm interested in the details. Second i want do find out why Java Apps are so slow. Third i found that method calls in Java 1.3 SUN VM (the last i ever used) were extremely expensive almost doubling the speed as inlining was obviously working far far from perfect.
Lothar
I'm extremely interested in where you get your speed stats. The test results I've seen on the HotSpot VM have been nearly C-speed, but I haven't really tested myself. If you have another source, I'd love to read through it. I've been getting my speed comparisons from the programming shootout game (google it) which generally put Java as the fastest language after C, and in some cases rate it as fast. The only time Java really seems to suffer is in loading the VM (which does not concur at all with your assertion). More info please!
Bill K
@Lothar: You can't base your opinion of Java VMs on the performance of the 1.3 VM, that was *9 years ago*. Modern JVMs are blindingly fast, and completelky unrecognisable in performance terms from the old 1.3 stuff.
skaffman
A: 

This is heavily dependent on the JVM you are using (especially when using one capable of just-in-time compiling to native code), since the optimizations performed over your code will ultimately determine this.

For instance, the JVM may decide to inline your method, and then you do not have calling overhead at all. If the method is not inlined, but still compiled into native code, you have an equivalent overhead to calling a method/function using a pointer in C/C++, plus the machine-specific requirements (for instance, setting up stack stace, arguments, epilogue/prologue). This should sum up to about the time to execute 10 to 30 native instructions, but it is an approximation---you should measure this if really important.

Figures should be very different if you are using a interpreting-only JVM, possibly with much higher overhead (never checked it myself, but if I had to guess, I'd bet on one extra magnitude---so around 100 to 300 instructions).

rnsanchez
+3  A: 

In the updated question, the OP wrote this:

I think i should mention that i'm not a java programmer but a compiler writer and want to find out why java applications are so slow while the java micro benchmarks are comparable to native compiled code.

Then in a comment the OP wrote this:

... i found that method calls in Java 1.3 SUN VM (the last i ever used) were extremely expensive almost doubling the speed as inlining was obviously working far far from perfect.

I think that the real problem is that your view that "Java is slow" is based on experiences with a really old release of Java. The java JIT compilers have improved significantly in the 9 years since Java 1.3 was released.

So looking at some the details is important for me here, even if they take only a few dozend nanoseconds and a few additional instructions.

If you are (by your own admission) a not a java programmer, but a (non-Java I assume) compiler writer, why are these details important to you?

UPDATE: I found this page on the Sun Wikis that may help. It applies to Java 7 which is only available as development builds, but there may be enough clues to help you disassemble JIT compiled code for current Java releases.

Stephen C
Well i never programmed Java since 1.3, but i'm using Eclipse, Netbeans and IntelliJ frequently and all of them are so slow compared to native GUI programs. The details are important for the marketing whitepaper where the differences between java and our native compiler are highlighted. After getting a real good discussionts about the implementation of "synchronized" monitors i thought someone has the same in depth information about method calls.
Lothar
Ah yes. I knew there had to be a hidden agenda :-)
Stephen C
Have you considered the possibility that Eclipse/Netbeans/IntelliJ are slow because of reasons other than Java methods not being inlined? Like having an open architectures, portable UIs, etc?
Stephen C
Well overengineered solutions are the main factor why most Java Programs are slow an Eclipse is definetly overengineered. But it can't be only this and the misuse of java.util containers or dynamic runtime casts in collections. And in the case of java i'm a fan of the guilty until proven innocent rule.
Lothar
"And in the case of java i'm a fan of the guilty until proven innocent rule." Funny. That's how I'd summarize my attitude to marketing whitepapers and people who write them :-)
Stephen C
This be either good or bad depending on your evaluation, but did you give the Java IDEs you are testing enough memory? Eclipse needs at least 256MB -- see http://stackoverflow.com/questions/142357/what-are-the-best-eclipse-34-jvm-settingsOn a 2.4 GHz Core Duo laptop with these settings I see no "slowness" except at cold startup (nothing in filesystem cache) and only then for the first file of each type I open as Eclipse loads the java, xml, etc editors.
AngerClown
+1  A: 

Well i never programmed Java since 1.3, but i'm using Eclipse, Netbeans and IntelliJ frequently and all of them are so slow compared to native GUI programs.

I'm not sure that I'm clear on even how to generate statistics for the performance of a GUI--do you measure the time taken to perform a specific task, use automated testing, or is this one of those situations where you sort of eyeball it and say "it's slower/faster than program X". I'm no expert on native GUI programs, but I do know that GUIs in Java often appear slow not because Java itself is necessarily slow, but because the GUI writers failed to construct a responsive GUI--putting long-running tasks on the Event Dispatch Thread, to name one example. In the absence of well-constructed comparison tests, it's hard to tell what's the fault of Java and what's the fault of bad GUI programming.

To answer your question, though, Bill K mentioned the Computer Programming Shootout Game as a source of numerous compiler benchmarks. Another interesting source of statistics for Win32 platforms can be found here.

Scott Fines
"Another interesting source of statistics for Win32 platforms can be found here" - No, it hasn't been updated since 2003 and on that machine Java 1.4.1 might even default to -client.
igouy
A: 

The point here is that you are comparing apples and oranges. Java compiles at runtime (have you heard of JIT?), so it's not exactly comparable to C which compiles offline. Most of the overhead comes from the time which you don't count in C (the compilation process) and not from the "method call" as you suppose

Davide
Why downvote? This is simple and accurate (and more up-to-date than other answers which were written before the question was modified)
Davide