11

Will apps targeting Android devices written with C++ on Qt perform better than apps written with Java using Android SDK?

Dinal24
  • 3,162
  • 2
  • 18
  • 32

4 Answers4

29

It seems all other answers are disputes about C++ and Java, so I will try to share my experience and knowledge about Java and Qt programming in Android.

First of all, Android SDK implemented in Java and thus developer should use Java as default.

.

More Performance and more control

But there is need to more performance in some cases. Games is an example. Image processing is another use case example.
In some situations you need to use C++ for better control over HW. (e.g. use Accelerometer with higher frequency)

Android uses an specific virtual machine named Dalvik (until ver.5). Any application which needs UI, needs Dalvik. So C++ applications (games and Qt e.g.) needs to involve Dalvik.

Also there is no C++ library to utilising services and OS features directly from C++ (such as contact book). Therefore you need to use Java alongside C++. For this purpose you need to write some Java codes in .java file and you need to use JNI for communicate C++ and Java. JNI is another problematic thing which you need to learn.
So developing Android application using C++ is very more difficult than regular Java apps.
.

My Qt experience on Android

I have written some Android applications by Qt\QtQuick without QtQuick Controls. QtQuick is AWESOME for UI development but you need to doing some stuff such as managing load and unload UI manually. It is not a problem, since there is some features in QtQuick for this purpose like Loader.
Implementing general section of the application (non-specific to Android section) in Desktop is very productive and fast and interesting (both C++ and QtQuick).
But implementing Android specific section in JNI needs more time and care.

.

Qt on Android - Cons and Pros

  • APK package is large (~8mb most cases overhead)
  • Need to publish multiple APK package (ARM, x86) (Google play lets you to doing it very nice without end user notice)
  • App launch time is higher than normal apps
  • Need to develop using combination of C++ and Java through JNI
  • More difficulties to debugging
  • Fewer documentation
  • Extremely fast UI
  • If you plan to publish your app in other platforms like iOS, you need just to reimplement OS-specific section. Thus your time/cost will goes low.
  • High performance back end processing and overall app performance
  • Better memory management
  • Same quality in Android 2.2 (e.g.) till 7.0! (Some features of Android SDK is just in higher version and thus you can not uses that features if you plan to support lower versions of Android. like AnimationFramework. Using Qt you don't have this problem)
  • Utilising HW accelerated UI in earlier Android versions as well as higher ones
  • Source code protection against un-ubfuscation
  • Good separation about View and Model/Controller (helping to professional coding)
  • Easy and fast UI development
  • Easy and nice UI effects (OpenGLES & GLSL) (also naturally integrated to other components of UI)

.

Suggestion

IF (you are not a Qt experienced developer) => Use Java

IF (you need just some more performance for some processing) => Java + C++(NDK)

IF (you are a Qt experienced developer) AND ((you are beginner in Java) OR (you planned to porting your app into other platforms)) => Use Qt with C++

Community
  • 1
  • 1
S.M.Mousavi
  • 5,013
  • 7
  • 44
  • 59
4

Lets break this into two parts:

1)Will C++ perform better than Java?

Yes. Of course it will. It always does, so long as you don't make to many JNI calls back into Java. That's why people (including Google) use C++ where performance is of the essence.

2)Will QT perform better than using JNI to go back to Java or than using the raw calls available to C++ in the NDK?

Assuming you write both kinds of code optimally, the order is probably raw NDK calls > Java calls > QT calls. The QT library is just going to be making the exact same function calls you would, it just puts a different abstraction layer on it. That layer will add overhead. Chances are that the overhead is minimal most of the time. If you're an experienced QT programmer, you will likely get something coded faster using QT. If you're an experienced Android programmer, you'll get it done faster without it. QT will increase the odds of bugs, because any bugs in the QT layer itself will show through in your code.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • C++ does not always perform better than Java. In general, yes, but in absolute, no. http://en.wikipedia.org/wiki/Java_performance#Comparison_to_other_languages I'm just being picky, of course. The point is, with few exceptions, moving standard UI components around the screen is going to see no real improvement with C++ vs Java. Image/video processing, games, etc, are a different discussion. As for QT, I'd bet the standard Android UI has way more optimization in it than QT for Android, so it would "lose", but again, all depends what you're doing. – Kevin Galligan Jul 13 '14 at 06:17
  • 9
    @KevinGalligan Sorry, the only benchmarks I've ever seen showing Java to be faster have been debunked dozens of times. C++ is always faster. Not always multiple times faster, but always faster. And a wikipedia link is hardly proof of anything. Peer reviewed in an scientific journal or its garbage. – Gabe Sechan Jul 13 '14 at 06:21
  • Yeah, I was looking for your link to a peer reviewed article, or anything, but I guess I missed it, so that's not garbage? In any case, even though I was clear in saying you're *usually* in the right, but not *always*, which is absolutely true, you seem to have missed the point. – Kevin Galligan Jul 13 '14 at 06:25
  • @GabeSechan: I have to say I've read the same, just recently actually. NDK is used for intense applications, but the docs also state that *most* apps would never benefit from it. In certain circumstances it is *possible* for some java code to be faster than native code. The code may very well be trivial. – ChiefTwoPencils Jul 13 '14 at 07:28
4

This answer was intended as a comment, but its length quickly exceeded the limit. Sorry :)

C++ is not magically faster than managed languages. Just ignore everybody who claims otherwise.

All C++ does is give you a lot more knobs to play with. If you are not an expert in C++, you are probably going to write terribly non-idiomatic C++ code that performs way worse than simple Java code in real world applications. Just to give you two concrete examples: beginners tend to overuse new (which has terrible performance in C++ by default), and they don't understand when objects are implicitly copied.

If you invest the time necessary to learn C++ deeply (1 to 10 years), a well-written C++ program can be significantly faster than its "counterpart" (if such a thing even exists) in a managed language.

Yes, there are tiny micro-benchmarks that prove C++ is n times faster than Java. It's comparatively easy to learn a subset of C++ well enough to write these kinds of benchmarks, if that's what you're after.

It's also trivial to write a micro-benchmark that shows std::list<T> performing much worse then java.util.LinkedList<T>. That's because without providing a specialized memory allocator for the std::list<T> nodes, the linked list will spend most of its time in memory allocation. Does this mean C++ is slower than Java? No, it simply means that allocators are one of those knobs that you have to master if you want to become an effective C++ programmer. A Java programmer does not care where memory comes from, because memory allocation is not customizable in Java (for better or worse).

fredoverflow
  • 256,549
  • 94
  • 388
  • 662
  • 9
    Java is a higher level language than C++. A higher level language means lower performance ESSENTIALLY! Java uses a virtual machine (for cross-platform purpose) thus it has many overhead for virtualisation. C++ uses direct power of HW. So Java is more simple in cost of lower performance. @fredoverflow don't try to affect a decision, just share your knowledge – S.M.Mousavi Nov 06 '16 at 14:06
  • @S.M.Mousavi: Although written to compare C# to C++, [this answer](http://stackoverflow.com/a/5331574/179910) applies about as well to Java as C#. – Jerry Coffin Nov 06 '16 at 16:39
  • 1
    @JerryCoffin I already know that information. The question not discussed which language is faster. So this answer is irrelevant. 'fredoverflow' discussed about performance by him assumes. the questioner not mentioned to performance nor productivity nor other secondary factors. Furthermore in natural if a C#/Java application written best and a C++ application is written best, C++ is faster certainly. – S.M.Mousavi Nov 06 '16 at 17:38
  • the "significally faster" would mean 5x or 10x faster. Never listen to Java folks who are saying that Java is a fast as C++, it is as fast as C++ if they cache everything and use the double amount of RAM. – Nulik Nov 26 '17 at 00:59
  • You can replace the "1 to 10 years" of learning C++ if you directly go to writing your own operating system. Just dive in , full time, to writing your own OS from BIOS , and in 1 year, you are going to be an expert in C/C++ – Nulik Nov 26 '17 at 01:03
  • If by that, you mean, an expert in the worst C imaginable and nothing in C++ at all, then yes. – Puppy Nov 26 '17 at 11:06
  • If comparing languages with respect to their performance you should not use comprehensibility issues to conclude which one is faster. You can say C++ is better in performance and Java is more comprehensible for novice programmers. I don't know whenever there is this discussion people say well you will get into trouble if you don't know how to program in C++, But that is not the question. – AMCoded Dec 29 '18 at 06:18
  • C++ is faster than Java. Period. This is not a discussion worth having. Java has already established its utility in the industry. Nobody is doubting how useful it is. Given that the Java code observes all best practices, Java applications are cross platform from the get go. It's also packed to the brim with features that allow you to be a bit sloppy or lazy in your design. It has a lot of great features. But it's not a fast language. Or I should say, it's not a fast technology, since the language is only part of the equation that determines its speed. – Rene Jul 01 '20 at 18:33
0

Following are results of my Whetstone and Linpack benchmarks via C and Java. Compiled C code can be slower if you select the wrong compile option for a particular target or even quite similar with the best option [for these kinds of CPU benchmarks].

After results is an example of the code used where Java can be virtually identical to C. For my Android benchmarks, all use the same Java front end with buttons and to display results. If you need more information or the programs (FREE with no ads) Google for “Roy Android Benchmarks.

     ARM v7-A15 rated 2000 MHz but running at 1700 MHz

 Whetstone Benchmark

      MWIPS  ------MFLOPS-------   ------------MOPS--------------
               1      2      3     COS   EXP  FIXPT     IF  EQUAL

Java  533.9  131.4  209.4  102.5  20.4   6.7  475.8  174.8  105.7
C    1477.7  363.9  220.6  307.5  39.7  18.0 1690.5 2527.9 1127.9


Linpack Benchmark MFLOPS

Option     C v5      C v7        C v7       C v7       Java
             DP        DP          SP    NEON SP         DP

          28.82     459.17     803.04    1334.90     143.06


  Example function in C

            startTime = getTime();
            for (ix=0; ix<xtra; ix++)
            {
                for(i=0; i<n1*n1mult; i++)
                {
                    e1[0] = (e1[0] + e1[1] + e1[2] - e1[3]) * t;
                    e1[1] = (e1[0] + e1[1] - e1[2] + e1[3]) * t;
                    e1[2] = (e1[0] - e1[1] + e1[2] + e1[3]) * t;
                    e1[3] = (-e1[0] + e1[1] + e1[2] + e1[3]) * t;
                }
                t = 1.0f - t;
            }
            t =  t0;                    
            endTime = getTime();
            runTime = (endTime - startTime) / 1000.0;


  Example function in Java

            startTime = System.currentTimeMillis();
            for (ix=0; ix<xtra; ix++)
            {
                for(i=0; i<n1*n1mult; i++)
                {
                    e1[0] = (e1[0] + e1[1] + e1[2] - e1[3]) * t;
                    e1[1] = (e1[0] + e1[1] - e1[2] + e1[3]) * t;
                    e1[2] = (e1[0] - e1[1] + e1[2] + e1[3]) * t;
                    e1[3] = (-e1[0] + e1[1] + e1[2] + e1[3]) * t;
                }
                t = 1.0f - t;
            }
            t =  t0;                    
            endTime = System.currentTimeMillis();
            runTime = (endTime - startTime) / 1000.0;
Roy Longbottom
  • 1,192
  • 1
  • 6
  • 8