The Performance Gap Between Java and C on Android Devices
In recent years, there has been an increasing trend towards using high-performance languages like C for developing applications that require heavy lifting. However, when it comes to developing Android apps, Java is often the preferred choice due to its ease of use and access to the Android UI, API, and services. But how does Java compare to C in terms of performance? Recent tests have shown that while C remains a faster option for certain tasks, the gap between Java and C is narrowing rapidly.
One test involves comparing the performance of 64-bit Android versions running on different processors. The results show that next-generation 64-bit Android 6.0 is only 3% slower than 32-bit Android 5.0 on a 64-bit processor when using 64-bit integers, and this difference can be attributed to the new Android runtime's ahead-of-time compiling and optimizing compiler. Additionally, testing with a mathematical function that involves multiplications, additions, and divisions shows that Java running on an Android 6.0 machine on a 64-bit processor is even faster than C code in some cases.
When using 32-bit integers, however, the results are less favorable for Java. Tests show that 32-bit Android 4.4 is 384% slower than C, while 32-bit Android 5.0 is 29% slower and 32-bit Android 6.0 is 191% slower. But when using 64-bit integers, the performance gap between Java and C narrows significantly. For example, Android 5.0 on a 64-bit processor is only 52% slower than C.
The performance difference between Java and C can be attributed to several factors. One reason is that the optimizing compiler used in Android 6.0 may have found better optimizations than the C compiler did, resulting in faster code. Additionally, the lack of garbage collection in the test cases means that memory allocation is minimal, which reduces the impact of any performance difference between Java and C.
In conclusion, while C remains a faster option for certain tasks, the gap between Java and C is narrowing rapidly. The new Android runtime's optimizing compiler and the use of 64-bit integers are key factors in this shift towards equal performance. As a result, developers can now consider using Java for their Android app development needs without sacrificing performance.
What does all this mean for developers? First, it means that choosing between Android and C is not as black-and-white as it once was. While C remains a better choice for heavy lifting tasks like game engines or other computationally intensive workloads, Java may now be sufficient for many development needs due to its improved performance.
Moreover, the choice of language should also depend on the specific requirements of the project. If the app requires direct access to the Android UI, API, and services, then Java is still the better choice. However, if the task involves heavy lifting or computationally intensive workloads, C may be a more suitable option.
Finally, developers can take advantage of the improved performance of Java on modern Android devices. By leveraging the new Android runtime's optimizing compiler and using 64-bit integers, developers can write high-performance code that takes full advantage of the latest hardware capabilities.
In conclusion, the choice between Java and C on Android devices is no longer a straightforward one. While both languages have their strengths and weaknesses, recent tests have shown that the gap between them is narrowing rapidly. By understanding the factors that contribute to this shift in performance, developers can make informed decisions about which language to use for their next project.
**References:**
* Gary Sims from Android Authority
"WEBVTTKind: captionsLanguage: enhello my name is Gary Sims from Android authority now Java is the official programming language of Android and Java is used not only on mobile but also on desktops and servers and Java is quite a unique programming language especially when compared to other programming languages like C now there are some big differences that are easy to spot like C is not object orientated Java is object orientated however there are some key differences that affect performance of programs written in C and programs written in Java first of all Java programs are not compiled to machine code they are compiled to an intermediate code called bite code Java bite code and this Java bite code is then executed on a Java virtual machine so to fulfill the goal of write once Run Anywhere a Java virtual machine can be implemented on Linux on Windows on servers on Android and therefore the same bite code the same intermediate code can be run on that platform with without needing to be recompiled however that means that when the program runs it has to run through itself another program this virtual machine which of course uses CPU time uses memory and therefore that has an impact on the performance the other big difference is that Java uses automated memory management that means that if a programmer allocates a memory allocates an object the programmer doesn't need to worry about when that object is no longer needed I think of the garbage collector will run periodically and collect up all the bits of memory that are no longer needed and discard them the problem is the garbage collector is an expensive thing to run for example if you're writing a game and you need to update the frames every 16 milliseconds having the garbage collector run right at the time when you're trying to update the frame will affect the performance of your app now C is quite different C compiles directly down to machine code so if it's running on a desktop PC running an Intel chip it will compile directly to the Intel machine code if it's running on an arm chip on a smartphone it will compile directly down to the machine code for the arm chip and it doesn't have automated memory management if the programmer allocates memory it's up to the programmer to free that memory at a time that the programmer feels is the best so therefore it's generally considered that our Java is slightly slower than C when the two are run side by side I thought it would be interesting to see how that speed comparison looks on Android what is the speed difference between a c program and a Java program now normally Java programs are written using the SDK the sofware development kit and Google also release a thing called the ndk the native development kit which allows you to write C and C++ programs for Android so what I've done is I've written an app that uses both Java code and C code and implements the same functionality in both languages with an attempt to see how much longer it takes in one language compared to the other now the app does three things first of all it calculates the sha1 hash of a block of data then it calculates the first million prime numbers using trial by division vision and then finally it runs a mathematical function that I wrote and it runs that a million times to see how long it takes now there are a couple of things I want to mention before we look at the actual results what first one is about optimization now every software engineer knows that software should be optimized to run uh faster on each platform that it's being deployed to now while I was writing this code because I was looking at the how many nanc it took to run each particular function because that was the idea of The Benchmark I was quite surprised at how drastically I could change the results by implementing a function one way or another now I've tried my best to optimize the Java code and the C code so that they give a fair test now if you want more details about the optimizations that I did and a comparison between the C code and the Java code please go over to the Android authority.com website and read the written article that goes along with this video it's also worth mentioning the Android Java virtual machines up to and including Android 4. for KitKat android used a Java virtual machine code named dvic and it was basically a Java virtual machine that interpreted the bike code and ran the appropriate instructions there and then on the processor it had a thing called Just in Time compiling which means that some sections of the code would be precompiled into the native machine code and called upon when needed and that did give a boost to the performance of the virtual machine however from Android 5.0 the default virtual machine became artart the Android runtime now the Android runtime used ahead of time compiling which basically meant that when a program was installed it was compiled at that moment in the background uh onto the processor for the processor of your particular machine probably an arm processor though it could of course be an Intel processor and then with the Advent of Android 6.0 what happened is that arm had been working in the background with Google to improve that compiling stage in that ahead of time compiler so that the machine code that was generated was even more efficient and that was known as the optimizing compiler and we'll see in the results how those three different Java virtual machines affect the performance of the Java programs running on the phones now I'd also like to point out that run this Benchmark on both 32bit and 64-bit processors and why that's important is because a lot of the code that's in my test app uses long integers now traditionally in C and Java an integer a whole number is 2 bits wide however there are things called long integers which are 64 bits wide now of course if you're running a Java virtual machine on a 32bit processor but you have a 64-bit number it has to do twice the work to process that number now actually it turns out that doing division or specifically the modulus operator for working out the remainder in Java on a 64-bit number is actually quite slow when running on a 32 two bit machine and we'll see that again reflected in the code so what I did was I wrote this app and it runs these three tests in Java and in C and it reports the relative difference in the time now I've run this test over 21 different devices a big shout out to my colleagues at Android Authority for helping me run this on a wide range of devices some are 32bits some are 64bits some have Kit Kat some have marshmallow some have lollipop okay and basically what I'm interested in is the difference in the speed between Java and C I'm not interested in the absolute time of course a modern day Snapdragon 820 processor is going to be faster than a quad core 32-bit processor from 2 years ago but on the same processor the Java and the C are both running and it's the difference between that performan is interesting to us not the absolute uh speed now once I ran the test what I actually found was that the result grouped together quite nicely you find that 32 bit lollipop all the results were grouped into one area 64bit marshmallow those results were all grouped together so let's have a look at what I found out so the first test was to take a block of data and create a hash an sha1 hash for that block of data now here are the results now again this is the relative difference the percentage difference between Java and c and the first thing we see is that Java is slower now in the worst case scenario which is on an Android 5 32-bit device it was 300% 296 300% slow which is four times slower now as we go down through the different devices we find that next you find 64-bit Android 5 you find 32-bit Android 4.4 but the fastest of all of these was Android 6.0 running on a 64-bit processor so for a real world application like creating cryptographic hashes we find that Java is actually 60% slower than C on the most modern 64-bit processors and in fact up to four times slower compared to C on older devices now the next test works out the first million primes by using trial by division now as I pointed out earlier I'm using 64-bit integers for this and 64-bit division on a 32-bit Java program is actually quite slow and we'll see that now in the results and the results here are quite amazing if we look at 32-bit Android marshmallow we see that Java is 263 slower than C we also find that 32bit Android 5 and 32bit Android 4.4 are significantly slow 172% and 240% respectively however once we move over to 64bit because we're using those 64-bit integers we find the Android 5.0 on a 64-bit process was only 38% slower and look at this this Android 6.0 on a 64-bit processor is only 3% slower now that 3% difference is really quite amazing and that's a lot to do with the new uh Android runtime with its ahead of time compiling and the optimizing compiler that went into Android 6.0 marshmallow now my third test runs a mathematical function that I invented it was my own invention just does some multiplications some adding and some division both integer and uh floating point and it comes up with a result at the end and that's run a million times to see how fast it runs on Java and C now again like the prime number testing here I'm using 64-bit integers and we can see the difference between a Java virtual machine running 32bit and 64-bit integers so looking at the worst results first here we find that Android 4.4 on uh 32-bit machine is 384 slower Android 5.0 on a 32-bit machine is 29% slower and Android 6.0 on a 32-bit machine is 191% slower now once again we move over to 64 bits we find that Android 5.0 on 64bits is only 52% slower but here's the amazing thing Java running on an Android 6.0 machine on a 64-bit processor was actually faster than the C code now there could be a couple of reasons for this one is definitely the optimizing compon compiler maybe it found a better optimization than the C compiler did and therefore was able to create faster code and then when you run that a million times that difference uh is seen quite clearly and the other thing of course to remember is that now using this simple mathematical function the garbage collector doesn't run at all and it doesn't need to run because it's just doing math there isn't much memory allocation going on so what does all this mean well first of all the choice between Android and C actually isn't that black and white because when you're writing an Android app you actually want to have access to the Android UI to the Android API to the various Android services including Google Play and all of those are only available via Java C is really only useful if you want to write a game engine or you've got some real heavy lifting to do and it will be quicker to do that in C now previously if you had that heavy lifting to do it might have been worthwhile porting sections of your code last sections of your code to see to get the performance gain however what my quick testing has shown is that that performance Gap is reducing rapidly and in fact in some cases it looks now that Java is just as fast as or even faster than C well my name is Gary Sims from Android authority I hope you enjoyed this video if you did please do give it a thumbs up also please don't forget to subscribe to Android Authority's YouTube channel also you should download and install the Android authority app that will will give you access to all the latest news and things that we are publishing also please don't forget to use the comments below to tell me what you think about Java what you think about C what you think about Android development in general it would be great to hear from you also if you want to please head over to the Android authority Forum you can use this link here and there you can ask me any questions you want about this video including about the app that I wrote including about C including about Java and I'll try and help you if I can and last of all but not least don't forget to subscribe be to androidauthority.com because we are your source for all things Android\n"