Android Dalvik VM performance is a threat to the iPhone

One of the peculiarities of Apple is that they have set themselves down a path where every Apple developer needs to learn Objective-C (and C/C++) to build applications for their platform.  The biggest characteristic of Objective-C vs Java is dynamic dispatch. At runtime Objective-C can send arbitrary messages to objects and they may or may not respond to them.  This has the nice property that you can write code that is very dynamic and loosely bound but it also has the property that method calls in Objective-C are very slow and the more code that you write in Objective-C instead of in C/C++ the slower your codebase becomes.  Up until Android 2.2 (Froyo) the JVM (really a Dalvik JVM for licensing reasons) on the Android platform was playing with one hand tied behind its back.  Different from desktop/server Java, the JVM was still an interpreter, like the original JVM back in the Java 1.0 days.  It was very efficient interpreter but an interpreter none-the-less and was not creating native code from the Dalvik bytecodes that it uses.  As of Android 2.2 they have added a JIT, a just-in-time compiler, to the stack that translates the Dalvik bytecode into much more efficient machine code much like a C/C++ compiler.  You can see the results of this in the benchmarks of Froyo which show a 2-5x improvement.  As they add more and more JIT and GC features that have appeared in HotSpot, JRockit, etc, you will likely see even more improvements over time — without having to change or recompile the 3rd party developed software.

This wouldn’t be that big a deal if Android software wasn’t already approaching the speed of the Apple iPhone even when running its applications through the interpreter (see the HTC EVO 4G running 2.1).  This is likely going to mean that 3rd party developer applications created for Android, running on the same hardware, is going to be faster than the same code written against the Objective-C libraries that Apple provides for the iPhone.  You’ll be able to get more done, have smoother user interfaces and all around build more powerful applications easier.  A better experience for the user and the developer on Android is a bad thing for Apple.

The final issue that Apple has is that far more people know and need  to know Java than Objective-C.  Except for their platform, there is no need to ever learn it.  The tool chain is much more robust, information is much easier to come by, garbage collection is available, and there are thousands more libraries written in Java than Objective-C and far more portable libraries than are written for C/C++.  You might argue that you can always drop down to C/C++ code to make up for the lost performance of using Objective-C (at a significant cost development-wise).  You can do that, except places where you need to make a call into Apple’s Objective-C runtime libraries or where you want to write callbacks from those libraries. Those are mostly Objective-C libraries and use the dynamic dispatch mechanism which I showed in 2004 was very much slower than Java.  I’d really love to see the whole computer language shootout written with Objective-C calling conventions just to show how much slower it is at that level.

Obviously performance is not the only consideration, but it is a big one.  Android has other issues like a fragmented operating system base, hardware feature base and a more complicated user experience.  All those things will conspire to hold it back but I think we can see the writing on the wall that Android is going to dominate iPhone market-share wise which will eventually make it a more attractive platform business-wise.

Update: Just so people don’t get the wrong idea, I am a rabid iPhone user and develop for the iPhone first.  This blog entry is to call out a threat that Apple should take seriously.

Update 2: Alright, you win. Don’t worry about competing with Google & Android on performance. It probably doesn’t matter that much for the user experience.  Especially don’t fix obj_msgSend() with a JIT/LLVM, that would be crazy.

This entry was posted in Apple, Java, Technology. Bookmark the permalink.
  • Isaac Gouy

    > I’d really love to see the whole computer language shootout written with Objective-C calling conventions just to show how much slower it is at that level.

    Demonstrate that calls/callbacks to Apple's Objective-C libraries actually are the bottleneck in real apps.

    Deliberately writing slow Objective-C implementations for benchmarks game programs would demonstrate nothing relevant.

    Back in the day – http://shootout.alioth.debian.org/debian/compar...

  • Jeremy

    Is this based on comparable processor speeds?

  • http://stefan.arentz.ca/2010/05/26/how-fast-are-objective-c-method-invocations/ Stefan Arentz − How fast are Objective-C method invocations? (Spoiler: Very fast)

    [...] Some dude on the internets claims that “method calls in Objective-C are very slow“. [...]

  • ..

    Double-spacing after a period/full-stop? Was this written in 1980 on a type-writer or are you planning on a mono-spaced font being put in the CSS. No need for it – very distracting.

  • http://www.javarants.com spullara

    It does actually. It means that you can't use the productive language, Objective-C, for those parts of your program where speed matters and you have to fallback to native code. Android programs can all written in Java at near native speeds. The benchmarks you cite from back in the day compare the Objective-C compiler compiling the C benchmarks, not Objective-C code.

  • http://www.javarants.com spullara

    I'll take that under advisement.

  • http://www.javarants.com spullara

    The benchmarks that I have done are on the same machine. Unfortunately for Apple, the processor speeds of the high end Android devices will always be faster because of the difference in release rate of the two systems. One phone a year vs at least a half dozen all leap frogging one another.

  • mcFactor

    > Garbage collection for Objective-C is not yet available on iPhone, only for real Mac OS X
    Oh thanks God it’s NOT available on iPhone and I home will never be available.

  • Anon

    Seriously? Seriously?!

    I didn't even notice until I saw your comment. Maybe you should practice reading instead of masturbating so much…

  • Thought you might find this interesting since you mentioned Android fragmentation in your post( link at bottom). A lot of people (myself included) that shared your thoughts on the current state of android has changed their minds after reading it and I was curious as to your take on it.

    http://www.engadget.com/2010/05/22/entelligence...

  • http://tjoozey.com/?p=5461 Android Dalvik VM performance is a threat to the iPhone

    [...] full post on Hacker News If you enjoyed this article, please consider sharing it! Tagged with: Android [...]

  • alblack

    When in the history of computing has the performance of a particular language ever had any impact on how popular or successful its associated computing platform was?

    In contrast, when has a freeware, hobbyist operating system ever become more popular or successful than a commercially viable one with a functional business model?

  • Dan

    I had to write in – I hate double spacing after periods too. It's always made writing come across as snooty to me.

  • Chet

    Unfortunately for reality, the vast majority of the userbase will always be running on something other than the fastest available processor because users don't buy a new phone every time a new model is released so unless you are doing some HTPC or video encoding work on your phone the fact that there is a processor out there that is slightly faster is irrelevant to a typical user.

  • Larissa C.

    One makes me a lot more money than the other. End of discussion.

  • Larissa C.

    One makes me a lot more money than the other. End of discussion.

  • Chet

    Your argument is that Apple will be at a severe competitive disadvantage due to the performance penalties of Objective-C compared to the new JIT compiler. Since the JIT compiler has not yet been released and Android is operating with severe performance penalties compared to currently shipping iPhones (per your own metrics, 2-5x slower than with the JIT compiler) how has Android been able to grow so quickly? If I follow your argument than Android should be getting destroyed in the market right now because of its currently huge performance disadvantage in current models.

    Let's posit a different argument — small performance disadvantages in method calling ABI are completely irrelevant compared to the actual performance, features, and user experience of the platform in consumer electronics devices (like smartphones).

  • Chet

    Your argument is that Apple will be at a severe competitive disadvantage due to the performance penalties of Objective-C compared to the new JIT compiler. Since the JIT compiler has not yet been released and Android is operating with severe performance penalties compared to currently shipping iPhones (per your own metrics, 2-5x slower than with the JIT compiler) how has Android been able to grow so quickly? If I follow your argument than Android should be getting destroyed in the market right now because of its currently huge performance disadvantage in current models.

    Let's posit a different argument — small performance disadvantages in method calling ABI are completely irrelevant compared to the actual performance, features, and user experience of the platform in consumer electronics devices (like smartphones).

  • Chet

    Your argument is that Apple will be at a severe competitive disadvantage due to the performance penalties of Objective-C compared to the new JIT compiler. Since the JIT compiler has not yet been released and Android is operating with severe performance penalties compared to currently shipping iPhones (per your own metrics, 2-5x slower than with the JIT compiler) how has Android been able to grow so quickly? If I follow your argument than Android should be getting destroyed in the market right now because of its currently huge performance disadvantage in current models.

    Let's posit a different argument — small performance disadvantages in method calling ABI are completely irrelevant compared to the actual performance, features, and user experience of the platform in consumer electronics devices (like smartphones).

  • yet another steve

    It's really easy to criticize a platform you don't understand very well. Apple has been optimizing its runtime dispatch for a long long time. Reality is that messaging overhead is trivial in practice. And you have access to C/C++ when you need to maximize performance. (Apple's lower level apis are written in pure C.) Real old-school compiled (by the dev) code — you aren't going to beat that for performance. This is the equivalent of speaking of Java as an interpreted language.

    The refresh cycle is overstated too. Real people buy a new phone about every 2 years… not every 3 months.

    And every BIG Android feature seems to come at the expense of what Apple (as a maker of complete products not just technology) knows from years of experience is user issue #1: battery life.

    ====

    But I digress. You're basically saying here that Compiled C can't keep up with the performance of Java. Fail.

  • noname

    Make sure you force garbage collection in Java
    because otherwise it is not fair comparison. Also initialization of the VM.

    This kind of B.S. worked when Apple purchased NeXT until Carbon
    was deprecated. Now no Mac developer ever bad mouths Obj-C or Cooca.

    First of all most of Java features were taken from Obj-C. including former NeXT
    employees who wrote the abomination that base classes and AWT.

    real work performance is different thing then cooked up benchmarks.
    even Ruby can beat Obj-C.
    If you need real example. look for WebObjects when it was written in Obj-C
    and then when it went to Java. The amount of features that were lost.
    FIrst web browser was written by TBL (a physicist) in Obj-C.

    Android is your last hope. you poor poor Java developers. You better
    pray that Google doesn't fuck up like Sun. Good thing that Java people
    along with Flash people can cry all they want how big their private parts are.

  • http://stefan.arentz.ca Stefan Arentz

    “method calls in Objective-C are very slow”

    I wrote a little test. You can see it at http://stefan.arentz.ca/2010/05/26/how-fast-are...

    8.5 million method invocations PER SECOND.

    Show some equivalent Java code running on Dalvik please.

  • http://stefan.arentz.ca Stefan Arentz

    “and you have to fallback to native code” – I don't think you understand how the iPhone works … Objective-C *is* native code.

  • http://ajcates.com/ A.J. Cates

    The hardware in most Android phones is 2x what it is in the iPhone. This and AT&T explains why they are able to match sales.

  • http://ajcates.com/ A.J. Cates

    The hardware in most Android phones is 2x what it is in the iPhone. This and AT&T explains why they are able to match sales.

  • BrentG

    I ran your test through Shark. Unfortunately, the Java bridge was deprecated a long time ago. Xcode will not compile Java now.

    But you might be interested to know that objc_msgSend() — the dynamic dispatch function which you're disparaging — only accounts for 5.4% of the time it took to run the test. It's quite possible that Cocoa just needs some serious performance optimization.

    Fortunately, it isn't anything like as important as you make it out to be. Cocoa enables the development of better code, faster, with access to better features, and is simply a more versatile language than Java. The AppKit framework is far more powerful than any comparable Java GUI toolkit, and looks a lot better. Sure, Apple could sink hundreds of thousands of person-hours into porting all the new Cocoa libraries (not to mention the underlying Core libraries like Core Foundation, Core Audio, Core Video, Core Animation etc.) to Java, but do you really think that that would pay off for them?

  • BrentG

    Hmm, I apologize, but I was reading the results incorrectly. The total time in objc_msgSend() was actually 19.5%. Sorry. Still, clearly not the cause of the discrepancy you found between Objective-C code and Java.

  • Chet

    What hardware was that test run on?

  • http://stefan.arentz.ca Stefan Arentz

    iPad

  • Alastair

    Sam – you might be a bit out of date regarding Objective-C and garbage collection. Garbage collection is available in Objective C via the autorelease pool and has been since Leopard. However, in any language garbage collection is nice in theory but not necessarily great in practice. In Objective-C autorelease isn't a great idea if you wish to have strict control over your applications memory footprint. In mobile environments this is extremely important. Java garbage collection is also far from perfect. Tracking down and fixing memory leaks in Java can be extremely painful. The other problem is the lack of control over when it is run.

    Personally I've found some of my greatest performance boosts (in an iPhone OS ObjC app) to occur by simply optimizing code or memory utilization. Nothing beats doing the hard work of performance tuning yourself. I would also argue that dropping down to C/C++ is a significant cost development-wise. You can easily write a mixed applications and target the faster languages at the performance critical parts of your application.

    Of course if you're lazy you could simply leave it up to the OS to hopefully improve things (at some point, by how much unknown) instead of doing it yourself. :)

  • http://www.javarants.com spullara

    Garbage collection for Objective-C is not yet available on iPhone, only for real Mac OS X. I wish it was.

  • http://www.javarants.com spullara

    It is possible that the Cocoa libraries are also much slower than their Java equivalents. Far more time has been spent optimizing the Java libraries.

  • Brian

    I guess OS X is doomed too then huh?

  • http://www.javarants.com spullara

    Linux beat out every single other Unix operating system on the server.

  • http://www.javarants.com spullara

    Fortunately OS X doesn't force you to write in Objective-C only for one, for two performance wise OS X does lose in virtually every benchmark (vs Linux / Windows implementations) but laptop/desktop performance has gotten to the point where it doesn't matter. It still matters on phones.

  • http://www.javarants.com spullara

    Objective-C calls are not compiled to machine code, they are dynamically dispatched through a runtime library. That is sort of the definition of non-native code.

  • Bazaarsoft

    And beat how many client operating systems? Last time I checked phones were not server systems.

  • BrentG

    In this case “optimizing your code” would involve forgoing the CoreFoundation methods which do string management, which would mean losing support for localization, which would be very costly to you as a developer. About 50% of the time spent in this test is taken up by Core Foundation. 12% or so by the actual memcmp() BSD function, and 19 or so by objc_msgSend() as I mentioned elsewhere, the rest is various. But this test is very contrived, in any case. It's nevertheless a fair point that the Mac OS X/iPhone OS libraries deserve more love. But switching it all over to Java? Ridiculous.

  • http://www.javarants.com spullara

    BTW, running this even more micro benchmark:

    http://stefan.arentz.ca/2010/05/26/how-fast-are...

    Results in a difference of 105x (Java over Objective C) on a Mac. I don't actually develop for Android so I can't run it there.

  • Bazaarsoft

    Really? Apple (and NeXT) has been optimizing for, what, 20 years? Even if you just count the time at Apple that's still 12 years. I don't see any real imperial evidence to support your argument.

  • http://matrixpointer.com/karl Karl Adam

    After tweeting up a storm at the wrong user thought I'd just add my comments directly to the discussion here.

    The fact of the matter is that the dynamic dispatch of ObjC is nowhere near as slow as one would think merely because it's a dynamic lookup. That dynamic lookup only ever happens the first time you ever call the method from which point it's hashed and invocation only requires one hash lookup. Apple has been optimizing this since 10.0, they've had 8 years to get it to just below the speed of direct method invocations in C++ and C.
    ObjC has garbage collection, just not on iPhone yet, and if github is any indication the amount of ObjC libraries and code far eclipses java which makes sense since the language predates Java by nearly 10 years. The established expertise in the domain is far greater.

    I still feel that the only reason why Google chose Java was specifically for this argument despite the fact that in the end it's really only the quality of the APIs themselves that matter.

  • thekarladam

    After tweeting up a storm at the wrong user thought I'd just add my comments directly to the discussion here.

    The fact of the matter is that the dynamic dispatch of ObjC is nowhere near as slow as one would think merely because it's a dynamic lookup. That dynamic lookup only ever happens the first time you ever call the method from which point it's hashed and invocation only requires one hash lookup. Apple has been optimizing this since 10.0, they've had 8 years to get it to just below the speed of direct method invocations in C++ and C.
    ObjC has garbage collection, just not on iPhone yet, and if github is any indication, the amount of ObjC libraries and code far eclipses Java which makes sense since the language predates Java by nearly 10 years. The established expertise in the domain is far greater.

    I still feel that the only reason why Google chose Java was specifically for this argument despite the fact that in the end it's really only the quality of the APIs themselves that matter.

  • tsm11

    Oh totally. Java's days are numbered*. (* to be measured in decades)

  • Bazaarsoft

    Or empirical as the case may be… :)

  • Bazaarsoft

    Or empirical as the case may be… :)

  • mcFactor

    1. garbage collection using NSAutoreleasePool IS available on iPhone
    2. objc 2.0 garbage collector is NOT available on iPhone, THANKS GOD.

  • thekarladam

    ObjC *is* native code. You're essentially saying that C++'s vtable lookup is native code because it's compiled before compilation and method invocations are hardcoded, whereas moving essentially the same thing into code makes it “non-native” code. At no part in the Obj-C dispatch mechanism is it ever in a state where code isn't being directly executed on the processor targeted at the specific hardware it was compiled for. (this leaves aside the OS and processor working in concert to run code that wasn't compiled for it)

  • http://www.javarants.com spullara

    Hey Karl! Actually I disagree. Check out the pure dispatch benchmark below. Java (on the Mac) is over 100x faster to execute that code.

  • clee

    If only there had been some kind of progress in the past six years on the ObjC tools! Wouldn't it be awesome if ObjC were compiled into some kind of machine-agnostic bytecode, similar to the JVM, but lower-level so that you could also target C and C++ to it as well as ObjC… oh, wait, that sounds an awful lot like LLVM (which Apple has been investing in for years now, along with the new C/C++/ObjC compiler frontend for it, clang).

    Seriously, it's not like Apple's just sitting around twiddling their thumbs. LLVM may not be perfect yet, but it's very neat tech, rapidly advancing, and can offer many of the same compelling potential performance improvements that Dalvik promises.

  • http://www.javarants.com spullara

    I didn't suggest switching the iPhone to Java. I think Apple needs to focus on performance of their Objective-C runtime and their core libraries or face an unnecessary performance gap.

  • http://www.javarants.com spullara

    I didn't suggest switching the iPhone to Java. I think Apple needs to focus on performance of their Objective-C runtime and their core libraries or face an unnecessary performance gap.

  • http://www.javarants.com spullara

    I don't imagine anyone, even Apple, mistakes NSAutoreleasePool for garbage collection.

  • elasticthreads

    “This wouldn’t be that big a deal if Android software wasn’t already approaching the speed of the Apple iPhone even when running its applications through the interpreter (see the HTC EVO 4G running 2.1). “

    I like how, to compare the performance of two software platforms, you use different generation hardware: a phone that hasn't been released for sale yet (the EVO) to a year old phone (assuming you're talking about the 3GS) that is approx. a month from being replaced by faster hardware.

    I bet Obj-C runs faster on my 2009 Mac Pro than Java does on a Pentium…

  • http://www.javarants.com spullara

    Ok, then everything is native code.

  • Zabbot

    Some people think using malloc/free equates to “garbage collection”. They are wrong, but there you go. Technically, there's a distinction is between 'automatic' versus 'manual' garbage collection. Pragmatically, no one except Lisp graybeards uses garbage collection in that way.

  • Chris

    I'm not convinced about your argument about performance, but i'm 200% agreeing with you about the foolishness of apple to stick with an obscure language obj-c, versus android with java which pretty much every developer in the world has had exposure to. I'm currently learning obj-c and iphone development, it's very difficult to wrap my head around, coming from a c#/java/ruby background.

  • CoderCanuck

    Hate to break it to you all, but there's a pile of people that learned to type that way and it's in muscle memory.

  • name

    Good to see that, even in a world with big, successful companies like Apple badmouthing crude middleware layers and preaching the virtues of actually understanding the platform you are coding to, Java is still out there, making it possible for any uninformed idiot to create “software”.

  • name

    Good to see that, even in a world with big, successful companies like Apple badmouthing crude middleware layers and preaching the virtues of actually understanding the platform you are coding to, Java is still out there, making it possible for any uninformed idiot to create “software”.

  • http://reedmorse.com Reed Morse

    If only the Android development toolchain were half as nice as the iPhone's… The Android SDK tools integrate terribly with Eclipse.

  • http://reedmorse.com Reed Morse

    If only the Android development toolchain were half as nice as the iPhone's… The Android SDK tools integrate terribly with Eclipse.

  • CoderCanuck

    I think you need to examine the details on JIT optimizations. A dispatch that passes static analysis and can be inlined IS inlined. It can later be unwound (since about 1.4 or 1.5) if a dynamic mod is made via introspection or the like. Objective-C doesn't have that level of dynamic inlining currently available, but as noted before, the LLVM work and features in Objective-C 2.0 that came out (including Garbage Collection, but not on the iPhone yet) are pushing Objective-C forward quite well.

    I've coded a long while on Java, and am increasing my work in Objective-C. They are different languages, and some things in Obj-C are actually quite good and cleaner than Java, others are not. If you want speed though, you are working in C with object extensions, and you need not always do a method call if it's speed critical, you make it a function call that, as Java will do, will compile in-line. But you lose dynamic capability in that case. Java has an edge with a very mature and capable runtime, Obj-C is still C in the core, and if you aren't taking advantage of the runtime, you're able to go fully optimized C code.

    I don't see a clear winner in either of them at this point. There's advantages and issues with both. And excuse my double spaces after periods. It's an old habit and I'm not about to remove them at this point in the post. I also put extra whitespace in my code for readability. ;-)

  • CoderCanuck

    I think you need to examine the details on JIT optimizations. A dispatch that passes static analysis and can be inlined IS inlined. It can later be unwound (since about 1.4 or 1.5) if a dynamic mod is made via introspection or the like. Objective-C doesn't have that level of dynamic inlining currently available, but as noted before, the LLVM work and features in Objective-C 2.0 that came out (including Garbage Collection, but not on the iPhone yet) are pushing Objective-C forward quite well.

    I've coded a long while on Java, and am increasing my work in Objective-C. They are different languages, and some things in Obj-C are actually quite good and cleaner than Java, others are not. If you want speed though, you are working in C with object extensions, and you need not always do a method call if it's speed critical, you make it a function call that, as Java will do, will compile in-line. But you lose dynamic capability in that case. Java has an edge with a very mature and capable runtime, Obj-C is still C in the core, and if you aren't taking advantage of the runtime, you're able to go fully optimized C code.

    I don't see a clear winner in either of them at this point. There's advantages and issues with both. And excuse my double spaces after periods. It's an old habit and I'm not about to remove them at this point in the post. I also put extra whitespace in my code for readability. ;-)

  • http://www.javarants.com spullara

    I have high hopes for LLVM. I'm hoping that it will close this gap.

  • Willie Abrams

    FWIW, I think invocation speed is a terrible benchmark. Besides, I agree with another commenter that your benchmark is testing the low-level libraries as much as dispatch…

    I went back and read your original article (2004) about Java, Obj-C and C speed. You concluded that Java was faster than Obj-C. You also concluded that Java was faster than C. I built put your JavaBenchmark into a command line tool and ran it. I ran the C benchmark again with GCC, and I ran it through clang. Here are results 6 years on…

    210690 : gcc 4.2.1 -O3
    306842 : javac 1.6.0_20
    446515 : clang 1.0.2 -O3

  • CoderCanuck

    Native code I will posit is machine instructions directly executed by the CPU in question. Objective-C is native code in that way, and the dispatch is done via function calls through a lookup table, not unlike the way C++ lays out, but the table can be altered at runtime. Still, all this is done in machine-code that is native to the CPU.

    Contrast Java, where non-JITted code is in an intermediary representation (byte code) until the JIT analyzes and opportunistically or deliberately depending on circumstance converts it to native machine code. Byte code is running through an interpreter, that jumps to direct execution when it hits a JITted segment of instructions. Objective-C is all native machine code, and the dynamic aspects are done by shifting things in those memory structures and code paths in native machine code. There is no byte-code in the gcc compiled objective-C.

  • http://www.javarants.com spullara

    This is exactly what I would like to see. Is there any evidence that they are going to go to this level with Obj-C or will the continual refrain be “rewrite in C/C++ for speed”?

  • Ladd

    You're mostly measuring string comparison performance, not method dispatch. Shark will easily show you this for the Objective-C side.

    Certainly, it could be argued that the NSString compare: method is somewhat slow, but this hasn't ever been a problem for me in practice.

  • phutwo

    You're comparing objc's message passing versus Java's function calls? Why don't you compare objc's function calls (C's function calls) versus Java function calls? You don't have to use objc's message passing, just make a direct call if you need it, those objects are just pointers. Java may have a slight advantage under rare cases as it can JIT into inlined. In those cases you can inline C's function calls as well.

    But you're missing the forest in the trees. In most normal GUI programs, you make a few thousand messages and that amounts to 0% of the run time. On an iPhone or Android, most of your time spent would be in library calls, both of which are native.

    Neither objc or java really matter in GUI applications. In high performance gaming applications, objc's advantage is it can use native instructions of the hardware (and precise control of memory layout), for instance the neon arm extensions or any new hardware instructions Apple chooses to implement in the future (Now that they can move functionality between soft and hardware freely). With Java, that has to be abstracted out into a library or use the NDK.

  • Danny Woods

    Unless your application has distinctly non-repetitive usage patterns, the JVM (or CLR, for that matter) will be on-par with C for a good many tasks. I wrote a video decoder in Java for a proprietary codec last year, and surprised the developer of the codec by outperforming his reference C implementation and being easily as fast (maybe a little faster) than his drop-to-MMX optimised version. Why? Because a JVM with a JIT can analyse how your code is running, not just how you or your compiler envisioned it running when the pre-optimised blob was written to disk. The JIT can also decide to use architecture-specific acceleration features (MMX, SSE) without you ever even knowing assembler; if you want to compete with that in C you'll need to cut-off all users below a certain CPU capability threshold and have a *very* good compiler. Frequently called methods that are not overridden in subclasses generally have monomorphic rather than polymorphic dispatch, avoiding a vtable lookup that a virtual method in C++ hard-codes at compilation. Small methods are often inlined into their callers when the running system demonstrates that it is required.

    As for developer time: he spent a non-trivial amount of time writing and tweaking the assembler for the block transforms used by the codec. I just translated the vanilla C versions.

    The downside? Memory, of course. While my decoder was as fast as the optimised C version, it consumed far more memory, even after a several passes through the code minimising the number of allocations (all that did was give me a very shallow memory growth curve and fewer garbage collects). Also, not all JITs are equal. Just because Dalvik now has a JIT doesn't necessarily put it on a par with modern HotSpot implementations.

    There's also the lack of control. You can't shave cycles off of a tight loop by hand-crafting some assembler that takes advantage of some high-level knowledge that you, the programmer, have.

    As to your last point, on battery life: the JIT will help here. Code that tries to run as fast as possible will accomplish what it's trying to do sooner; code that's task driven will accomplish those tasks with less load on the CPU. Both mean less of a drain on battery.

  • http://techmiso.com/2736/android/ Android :: TechMiso

    [...] Sam Pullara on Android: …I think we can see the writing on the wall that Android is going to dominate iPhone market-share wise which will eventually make it a more attractive platform business-wise. [...]

  • http://reabilita.me/2967 Duncan Davidson on Android’s Virtual Machine Performance as a Competitive Factor Against the iPhone | reabilita.me

    [...] Davidson, who knows a thing or two about Java, responding to this argument by Java developer Sam Pullara that Android’s Dalvik VM puts the iPhone at a significant competitive disadvantage, for [...]

  • http://www.daringfireball.es/2010/05/27/maquina-virtual-android/ Duncan Davidson escribe sobre el rendimiento de la máquina virtual de Android como ventaja competitiva contra el iPhone | Daring Fireball en español

    [...] Davidson, que sabe un par de cosas sobre Java, responde a lo que asegura el programador de Java Sam Pullara sobre que la máquina virtual Dalvik de Android coloca al iPhone en una gran desventaja competitiva [...]

  • http://www.music-piracy.com Matt

    Dlavik is a threat to android, and has been since day one. Let's honestly look at the stupidity of this post. You are arguing that a native application dev environment that's more robust than androids sdk, faster, and quicker to program in is somehow in trouble from a slow ass busted and might I added perfectly retarded register based vm.

    Yeah ignoring the merits of dlavik as a VM, let's just right off the bat point out you just said that iphones ( generally faster processors than android designs ) are now facing a threat from a VM… on an embedded device.

    That doesn't make any sense. It's incomprehensibly stupid. It's like saying “Dlavik sucking less makes it AWESOME”. No it doesn't. It makes it less shit. But at the end of the day it's still shit.

    As long as android relies on dlavik they will suffer in the app war with apple. And that's obvious when you talk to developers, and when you look at numbers.

    Also, you realize that Sun's commitment to Java is lapsing as they lose all of their core devs right? The language that couldn't succeed since it's flawed inception in the 90s is in rapid decline. Largely thanks to google btw. So saying that using Java as a language is somehow superior to objective C, even from a preference point is lacking in some attachment to reality. Objective C is a cleaner language that's easier to read, and work with. It's got a lower learning curve, runs faster, and has libraries that don't suck. Apple's implementation of it is one of the best on the face of the earth. You look at android's SDK, and even there NDK and it makes you want to stab people in the face.

  • Strangway

    A *lot* of people learned to say “backslash” when talking about forward slashes. It's bad learning. That guy is right, stop using two spaces after a period. Unlearn.

  • http://alastairs-place.net alastair

    Your post from 2004 is wrong, since you aren't measuring method invocation overhead at all. In all of your tests, you're actually measuring the performance of the string comparison API. You can see that this dominates the results easily enough. Also, I'm suspicious about the validity of the test because I think the Java compiler is optimising away code that should be being timed.

  • http://www.mcubedsw.com Martin Pilkington

    There are 3 things you are missing:

    1. objc_msgSend is fast enough that this isn't really relevant. The bulk of performance issues in both languages is in their libraries.

    2. Java method calls are incredibly inflexible. The reason Apple goes with Obj-C is because dynamic dispatch, despite being a bit slower, is far more flexible and allows for design patterns that allow for developers to write less code to do the same thing. Obj-C is sacrificing a bit of runtime speed (which is largely irrelevant on even mobile phones) for the sake of faster development time and more elegant code. Plus you get things that you can't do with a basic method call. Unless I'm mistaking, you can only call a method on an object in the same process. With Obj-C you can send a message to an object on the other side of the planet.

    3. Ultimately this doesn't affect users. Java and Obj-C have been side by side on the Mac for years. They didn't care that Java could invoke a method faster than Obj-C could. What they cared about wast that the apps written in Obj-C had far better user experiences and integrated better with OS X. You can throw all the low level comparisons around you like, ultimately users care about having a fun, responsive and useful application and couldn't care less about how a developer created it.

  • Niklas

    “garbage collection is available”, as it is in Objective-C, not on the iPhone currently, but that;s not a Java advantage since Objective-C has it on the Mac.

  • Jon Hendry

    Autorelease pools are a form of reference counted memory management, not garbage collection.

    And it goes back to about 1995, not “since Leopard”

  • http://www.javarants.com spullara

    There is reason only one company uses Objective-C and it is not its awesomeness.

  • Pieter

    If performance matters, Dalvik is doomed. There will always be a penalty for byte code. Either due to to interpretation itself costing time or due to performing JIT, itself costing time, forcing bookkeeping and taking memory for the compiled code that could otherwise be used for caches or just keeping a larger working set in memory.

    Separate from that, I think you misunderstand the amount of function calls done using the objc_msgSend system in a real application.

  • Pieter

    I don't believe the 12G invocations per second you claim for java, I think you have been had by your JIT compiler.
    I do believe the 116M invocations per second for Objective C…
    See my comment on Stefan's site…

  • Steven

    Just out of curiosity? Do you like uneducated people as well? A single space after a period/full stop simply looks annoying.

  • http://blog.chrissaari.com/2010/05/27/the-traffic-cop-pattern-or-why-java-vs-obj-c-isnt-really-that-important/ Chris Saari
  • http://kenkinder.com/ Ken Kinder

    Performance aside, you mention that a lot of developers already know Java and that puts them in a position to develop for Android. Unfortunately, that's only sort of true. While Android uses the Java language, even the more basic Java libraries are missing and replaced with Google ones. All those Java developers who trained for Sun's idea of Java are going to have to relearn everything but the language itself.

  • http://www.mcubedsw.com Martin Pilkington

    It is wrong to suggest that you have to be a fanboy to like Objective-C. I can see several flaws in Objective-C and things that annoy me a lot. However, it is an incredibly powerful language. The dynamic dispatch isn't bolted onto C, it is a runtime build with C. It is no more bolted on than stdio is bolted onto C.

    Now for some, including me, Objective-C *is* a lot easier to read and a lot cleaner. You can more easily see nesting thanks to the square brackets, you have interspersed arguments which makes methods far more self documenting and you can do more in less code thanks to the dynamic dispatch.

    The issue is, you are complaining about Obj-C being “slow” because of dynamic dispatch. I can show you 100s of Obj-C apps that run perfectly fine despite dynamic dispatch and 100s of Java apps that run slow even with the static method calling. Hell, have you tried an Android phone and iPhone side by side? It is ridiculous how slow the Android is in comparison at times, yet it is selling reasonably well.

    The fact is that these things are largely irrelevant and the majority of performance problems come from APIs or just bad code. The largely insignificant runtime cost of dynamic dispatch is more than made up for by the very significant savings in development time that a dynamic language affords you. Users don't care about invisible speed issues, they care about real speed issues. Developers don't care about getting every last little bit out of their code, they care about writing less code, faster. This is why we write in Objective-C, C, Ruby, Python, C# and even Java instead of Assembly. About the only time you *really* need to drop down to C and where Obj-C is “slow” is when you need realtime processing, such as audio, in which case almost anything bar C is likely to be too slow and sometimes C itself can be.

  • http://blog.utest.com/android-vs-iphone-performance/2010/05/ Android vs. iPhone Performance | Software Testing Blog

    [...] So people are saying that Android is slow, right?  Actually, no.  In fact, Java has been optimized so extensively in the past 10 years that its performance is now incredibly fast.  Dalvik has been optimized even further for mobile devices, so Android is one fast platform. What people are actually saying is that the iPhone is slow. [...]

  • http://www.mobileapptesting.com/android-vs-iphone-performance/2010/05/ Android vs. iPhone Performance | Mobile App Testing

    [...] So people are saying that Android is slow, right? Actually, no. In fact, Java has been optimized so extensively in the past 10 years that its performance is now incredibly fast. Dalvik has been optimized even further for mobile devices, so Android is one fast platform. What people are actually saying is that the iPhone is slow. [...]

  • sam

    Yeah this “poor Java developer” makes a shitload of money writing Java and that won't change for a very long time. In fact, I'd bet I make more as a “poor Java developer” than just about any staff iPhone dev out there. All this and Java is going nowhere. It's still dominates the market and will for decades to come.

    Finally, Java may suck as a language but so does Obj-C. Looking at O-C code is like looking into a code museum. So spare me.

  • http://imackorea.com/?p=602 Copland 위기, 다시 올 수 있다

    [...] 중 하나가 이것이다. 그리고 구글의 최신 안드로이드 판을 보면, 구글은 실수를 저지르지 않았다. don’t-call-it-Java인 Dalvik 가상머신은 꽤잘하고 있다. (레지스터-기반의 [...]

blog comments powered by Disqus