问题
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?
回答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.
来源:https://stackoverflow.com/questions/25629921/java-vs-c-run-time-edit-added-code