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 Updated to NodeBB v4.3 + New Features

Android, Java faster than C++ [Solved]

Scheduled Pinned Locked Moved Mobile and Embedded
5 Posts 3 Posters 1.3k Views 2 Watching
  • 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 5 Aug 2015, 15:54 last edited by luca 8 Jun 2015, 09:40
    #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
    • C Offline
      C Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on 5 Aug 2015, 21:16 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 C 2 Replies Last reply 6 Aug 2015, 06:50
      0
      • C Chris Kawa
        5 Aug 2015, 21:16

        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 6 Aug 2015, 06:50 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
        • C Chris Kawa
          5 Aug 2015, 21:16

          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.

          C Offline
          C Offline
          cookie-jar
          Banned
          wrote on 6 Aug 2015, 08:13 last edited by cookie-jar 8 Jun 2015, 08:14
          #4
          This post is deleted!
          1 Reply Last reply
          0
          • L Offline
            L Offline
            luca
            wrote on 6 Aug 2015, 09:17 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

            1/5

            5 Aug 2015, 15:54

            • Login

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