Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Android, Java faster than C++ [Solved]
Forum Update on Monday, May 27th 2025

Android, Java faster than C++ [Solved]

Scheduled Pinned Locked Moved Mobile and Embedded
5 Posts 3 Posters 1.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    luca
    wrote on last edited by luca
    #1

    hi,
    I have an Android application that simply calculate the first N prime numbers in both way: C++ and Java (using JNI).
    The C++ code is:

    
    ...
    long  m_startNum=0;
    long  m_endNum=100000;
    ...
    void Engine::primeNumbers() {
        int tot_prime=0;
        int count=0;
        for( long i=m_startNum;i<=m_endNum;i++)
        {
            for( long  j=2;j<=sqrt(i);j++)
            {
                if(i%j==0)
                    count++;
            }
            if(count==0 && i!=1)
            {
                tot_prime++;
                count=0;
    
            }
            count=0;
        }
        emit ended();
        qDebug() << "TOT TROVATI: " << tot_prime;
        emit newPrimeNumber(tot_prime);
    }
    

    The Java version is:

    void Engine::primeNumbersJava()
    {
        jint tot = QAndroidJniObject::callStaticMethod<jint>(
                    "com/test/calc_test_java/PrimeNumbers",
                    "primeNumbers");
    
        qDebug() << "TOT TROVATI: " << tot;
        emit newPrimeNumber(tot);
        emit ended();
    }
    

    and my Java class:

    package com.test.calc_test_java;
    
    public class PrimeNumbers {
        static long m_startNum = 0;
        static long m_endNum = 100000;
    
        public static int primeNumbers() {
            System.out.println("inizio primeNumbers");
            int prime_found=0;
            int count=0;
            for(long i=m_startNum;i<=m_endNum;i++)
            {
                for(long  j=2;j<=Math.sqrt(i);j++)
                {
                    if(i%j==0)
                        count++;
                }
                if(count==0 && i!=1)
                {
                    count=0;
                    prime_found++;
                }
                count=0;
            }
    
            System.out.println("fine primeNumbers");
            return prime_found;
        }
    }
    

    The strange thing is that when I execute on a tablet, the C++ version takes 4 secs to end while Java version takes only 2 secs.
    I didn't expect that result...

    What do you think about?

    Luca

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      I don't see any code measuring time. Have you actually checked it? Running Java and C++ have different runtimes spinning up and lots of other overhead - loading libraries and such. It doesn't mean that particular function runs faster or slower.

      L cookie-jarC 2 Replies Last reply
      0
      • Chris KawaC Chris Kawa

        I don't see any code measuring time. Have you actually checked it? Running Java and C++ have different runtimes spinning up and lots of other overhead - loading libraries and such. It doesn't mean that particular function runs faster or slower.

        L Offline
        L Offline
        luca
        wrote on last edited by
        #3

        @Chris-Kawa
        Yes, I checked time by using QElapsedTimer on the calling method of primeNumbersJava and primeNumbers.
        As you write, I suppose the reason is related to library loading or something else.
        But I grant you Java is very faster in my specific methods primeNumbersJava and primeNumbers...

        1 Reply Last reply
        0
        • Chris KawaC Chris Kawa

          I don't see any code measuring time. Have you actually checked it? Running Java and C++ have different runtimes spinning up and lots of other overhead - loading libraries and such. It doesn't mean that particular function runs faster or slower.

          cookie-jarC Offline
          cookie-jarC Offline
          cookie-jar
          Banned
          wrote on last edited by cookie-jar
          #4
          This post is deleted!
          1 Reply Last reply
          0
          • L Offline
            L Offline
            luca
            wrote on last edited by
            #5

            Looking on the compiling output I saw that QtCreator is using "-O2" optimization flag.

            I tried with -O3 by putting the following lines on .pro:

            QMAKE_CXXFLAGS_RELEASE -= -O
            QMAKE_CXXFLAGS_RELEASE -= -O1
            QMAKE_CXXFLAGS_RELEASE -= -O2 
            QMAKE_CXXFLAGS_RELEASE *= -O3
            

            And now the C++ version on primeNumbers takes only 620 msec while Java version takes 2 sec .
            That's what I expect!! ;-)

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved