-4

I just wrote a program that finds all the prime numbers with an upper bound.

The algorithm: Sieve of Eratosthenes.

Wrote it both in C and Java. The upper bound is 666014.

For some reason C gives the result in more than 2.5 seconds, while Java does it in like half a second.

Details:

  • The array in C is of type char

  • The array in Java is of type boolean

C IDE: CodeBlocks

Java IDE: IntellijIdea Community Edition

C code:

#include <stdio.h>

int main() {

    int n = 666013;
    int i;
    int k;

    char a[n];

    for (i = 2; i <= n; i++)
        a[i] = 0;

    for (i = 2; i <= n; i++)
         if ( a[i] == 0 )
         {
            printf("%d\n", i);
            for (k = i + i; k <= n; k += i)
                 a[k] = 1;
         }

    return 0;

}

Java code:

package primes;

public class Prime {

    public static void main(String[] args) {
        long starttime = System.nanoTime();
        final int MAXN = 666013;
        boolean a[] = new boolean[MAXN];

        for (int i = 2; i < a.length; i++)
            a[i] = true;

        for (int i = 2; i < a.length; i++)
            if (a[i])
            {
                System.out.println(i);
                System.out.printf("");
                for (int j = i + i; j < a.length; j += i) {
                    a[j] = false;
                }
            }

        System.out.println(System.nanoTime() - starttime);

    }
}

Last Edit: used System.nanoTime() Java gives 0.35 seconds

The C algorithm cannot be any faster. What is the reason Java is faster here?

Li4ick
  • 293
  • 2
  • 9

1 Answers1

1

I'm going to bet that you're actually asking about run-time even though you listed compilation time in your question header. If you're trying to time compilation, its really apples to oranges, especially if you're doing it in IDEs instead of using command line.

If you're comparing a Java application to a C++ application, there's a lot of things you must consider.

  • You should use the same data types.
  • You MUST make sure your Java code isn't triggering extra boxing.
  • You should read for both languages and find how to use the most accurate low level system timer. System.currentTimeMillis() is not very accurate in Java, for example.
  • You should make sure your time is measured prior to putting it to output so that output differences in the two languages don't get measured in the test.
  • Are you running the same architecture in both languages? (32 or 64 bit).

Frankly, your assessment is very code dependent. Without showing code people can't help you. There are many, many things you could do to affect timings in even the simplest code.

John Humphreys
  • 34,412
  • 34
  • 137
  • 243
  • Just for the note, System.currentTimeMillis() is more accurate than nanoTime() – joey rohan Sep 02 '14 at 18:38
  • Why is that out of curiosity? I thought I remembered something from Joshua Bloch's effective Java saying that currentTimeMillis() was inaccurate, but I don't have the book at work to check at the minute. I probably remembered the text wrong. – John Humphreys Sep 02 '14 at 18:55
  • This link might be useful : http://stackoverflow.com/questions/351565/system-currenttimemillis-vs-system-nanotime – joey rohan Sep 02 '14 at 18:58
  • And that book is opened in another tab.Cheers:) – joey rohan Sep 02 '14 at 19:00
  • 1
    Ah, nice link :) The 28 vote comment on the answer helps a lot. And it's a great book, I bought it because it was modeled on Effective C++ which was awesome too. :D – John Humphreys Sep 02 '14 at 19:16