Friday, 16 April 2010

Android, Java and C (Well Haskell)

I have been pondering creating a few Android apps that would require some pretty complicated calculations. Now I have not done any profiling of Java's speed on android phones  (I am waiting for a phone to be delivered) as I have so far used only used the emulator to test my code/released apps. No point doing speed tests on an emulator as it is not representative.

Java on android does have a bad rep. It is not a just in time compiler and apparently coding in C/C++ give you at least an order of magnitude speed increase. Now I am just regurgitating stuff I have read so if you arrive here expecting real facts, errr sorry?

I had been pondering doing the core of these apps (there are 2) using haskell and then using Jhc (a haskell compiler) to compile the code to C. As I understand it Jhc does not need a big runtime system to be ported and some people have used it on iphone apps. Well back when apple let you use your language of choice. Using Haskell I could hopefully get a speed increase over interpreted Java and gain a lot of type safety in the process.

The issues. Well I am not sure how robust Jhc is. If android added a just in time compiler would haskell still beat it. Now here is the big one. I like to keep things simple when it come to admin work and here I call generating builds admin work.

Compiling C code means my APK is only usuable on ARM processors. Android has been ported to MIPS and intel announce they had just ported android to x86 and they expect products to be available this year. I love diversity but coding in C means generating and testing on these platforms or not supporting them. Coding in Java means I get them for free.

Basically much as I like the idea of coding in Haskell or C within android it comes at a price. The obviously thing to do is not worry about it, code in Java and then if it needs the extra speed revisit C.

Update: I originally wrote Nhc when I should have written Jhc. Updated to fix.


  1. It might be useful to know that the C code nhc produces is actually bytecode with an interpreter written in C. So I doubt you will get very much speed out of it.

    But from what you're writing it seems you might be thinking of jhc which I think could be a nice option.

  2. This comment has been removed by the author.

  3. Hi Josef,

    Yep I was totally thinking about Jhc and not Nhc when I wrote this entry. I will update the post.