Looks like Apple should “switch”

Looks like Apple should “switch”

And I am not talking about from PPC to Intel. They should switch from Objective-C and its runtime to a fast, clear, mainstream language like Java and the Java VM. BTW, when I say Obj-C, I mean Obj-C + the Apple Cocoa libraries (or even the NextStep ones I presume).

Maybe you were a little surprised when I threw in fast as one of the attributes of Java. Maybe you’ve not been paying attention, but Java is becoming a real screamer with all the JIT and garbage collection technology that is being poured into it. The other day, after I had ranted to someone for the 10th time that Apple should switch to Java from Obj-C, someone said “but wouldn’t that slow the whole system down?”. I thought about it for a minute and decided that it was unlikely, seeing as most invocations in Obj-C are reflective and many in Java can actually be inlined at runtime. I decided to take some bets with friends, virtually all decided that Objective-C must be faster because it is most like C and most predictions, once I explained the benchmark, were that Objective-C would beat Java by 2x. I also talked to some people on an Objective-C IRC channel and they assured me that Obj-C would smash Java because of all kinds of esoteric optimizations the runtime can do. They really couldn’t explain them so I really didn’t believe them.

I didn’t have nearly as much faith in Objective-C. I figured that Java would beat it by at least 2x. So much thought has been put into Java VMs compared with the Objective-C runtime it would actually be a shame if Objective-C won this competition. The benchmark I grabbed was a benchmark we use to test JRockit against HotSpot. Its does string comparisons that result in the different cases. Universally people thought this would give Obj-C an edge because strings are so low level in C. However, that is misleading, because a string in Obj-C isn’t low level at all. Its a class, NSString, just like it is in Java. Well, here is the result of the benchmark on a 1.42ghz PowerMac G4 (similar results on my 1.5ghz Powerbook):

That’s a nice 6.4x speedup for applications that use a lot of strings! Here is the binary you can run right off this site:

Here is the Xcode project so you can see the source and build it yourself:

I would normally now talk about how this whole thing took like 20 minutes to develop, including the user interface, but the only people that would care are people that already know how productive development is when using Xcode + IB + Cocoa. You might also take note that you can put both Obj-C and Java in the same executable without any trouble, just make sure you start with a Cocoa-Java application. Oh, one more thing, I also tried compiling it optimizing for speed instead of space — it ended up with the same benchmark result except the executable was smaller!?


After that experiment I talked to a Cocoa person and he said that many times people will go back and optimize all their code for manipulating strings and things like that using pure C code. So I rewrote the benchmark again, this time using standard ANSI C. Its a straight console application and here are the results:

/Users/sam/Projects/Xcode/Benchmark:> gcc -o benchmark benchmark.c; time ./benchmark

real 0m18.640s
user 0m17.480s
sys 0m0.200s

Without optimizations, the C application is actually a lot slower than the Java application. Lets add some optimizations:

/Users/sam/Projects/Xcode/Benchmark:> gcc -O3 -o benchmark benchmark.c; time ./benchmark

real 0m14.502s
user 0m14.230s
sys 0m0.050s

Still lags slightly. I’m sure there is probably some command line thing that could get the performance to exceed the Java version, however, I think my point is made. Java is really, really quite fast. So please Apple, stop using an inferior development laguage. Here is the C file: