C ++ Code ist: < /p>
Code: Select all
#include
#include
unsigned long long s(unsigned long long n)
{
unsigned long long s = 0;
for (unsigned long long i = 0; i < n; i++)
s += i;
return s;
}
int main()
{
LARGE_INTEGER freq, start, end;
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&start);
printf("%llu\n", s(1000000000));
QueryPerformanceCounter(&end);
double d = (double) (end.QuadPart - start.QuadPart) / freq.QuadPart * 1000.0;
printf("Delta: %f\n", d);
return 0;
}
< /code>
Java -Code ist: < /p>
public class JavaApplication5 {
public static long s(long n) {
long s = 0;
for (long i = 0; i < n; i++) {
s += i;
}
return s;
}
public static void main(String[] args) {
long start = System.nanoTime();
System.out.println(s(1000000000));
long end = System.nanoTime();
System.out.println((end - start)/1000000);
}
}
Code: Select all
Java: 2795 ms
Code: Select all
C++ : 0.013517 ms
Es heißt, C ++ ist 206777 -mal schneller als Java! Auf keinen Fall! Was ist in meinem Test falsch? Der Unterschied ist riesig.