Flutterby™! : Java vs C

Next unread comment / Catchup all unread comments User Account Info | Logout | XML/Pilot/etc versions | Long version (with comments) | Weblog archives | Site Map | | Browse Topics

Java vs C

2014-04-14 16:01:33.540411+00 by Dan Lyke 3 comments

A performance comparison between Java and C on the Nexus 5. Java vs C++ on the Android platform, with some relative comparisons on an x86 architecture laptop.

Spoiler: C still wins. Just a little over 2x with a lot of manual Java optimization (at the expense of readability), but clearly the Java optimizers have a long way to go.

[ related topics: Software Engineering Theater & Plays Architecture ]

comments in ascending chronological order (reverse):

#Comment Re: made: 2014-04-14 16:54:06.638182+00 by: battjt

Right. Array access has bounds checking in Java. This is a good thing.


#Comment Re: made: 2014-04-14 17:41:40.166442+00 by: Dan Lyke

If bounds checking is taking 8x of the execution time, it's doing things wrong.

And most of the optimizations he describes to get from 8x to just over 2x are things the optimizer should definitely be doing.

#Comment Re: made: 2014-04-15 09:40:24.927925+00 by: battjt

I agree, but bounds checking is probably most of the remaining 2X. There is a lot of array access in that code.

I don't understand why the optimizations he made resulted in 8X increase. My experience has shown that inlining has no impact on performance.

The change to getOffset is a lot more than just inlining. Masking with a constant is going to be a lot faster than the two mods and an add that he was doing, but it still doesn't account for an overall 8X speedup.

His clamp function doesn't make sense. A short can not be outside of the range of Short.MIN_VALUE and Short.MAX_VALUE. He converts a short to an int, does the clamp, then casts it back to a short.

This isn't apples to apples. 1. The java code has bounds checking. 2. The java code has an extra level of indirection to get to the arrays.

Given that, I'm surprised that it's only 2x slower. Joe