基准C ++ vs Java,不切实际的结果 [英] Benchmark C++ vs Java, Unrealistic results

查看:149
本文介绍了基准C ++ vs Java,不切实际的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个简单的测试,我知道C ++速度更快,但是我的测试结果是不现实的。

C ++代码是:

  #include< stdio.h> 
#include< windows.h>

unsigned long long s(unsigned long long n)
{
unsigned long long s = 0; (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\\\
,s(1000000000));

QueryPerformanceCounter(& end);
double d =(double)(end.QuadPart - start.QuadPart)/ freq.QuadPart * 1000.0;

printf(Delta:%f\\\
,d);

返回0;
}

Java代码是:

  public class JavaApplication5 {

public static long s(long n){
long s = 0;

for(long i = 0; 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);




$ p $ C $编译器:gcc 4.4 .0和Java:jdk 1.6.0
$ b


Java:2795 ms



C ++:0.013517 ms

它说C ++比Java快206777倍!没门!在我的测试中出了什么问题?

解决方案

显示您使用的编译器选项。您的真实代码( #include 不是您真正的代码)。



您的C ++编译器比你的Java编译器聪明得多(平均情况是这样,你的情况是这样,但并不是每个C ++编译器都比每个Java编译器都聪明),并且它预先计算了结果。您唯一要做的就是调用 printf



在大多数Java用于执行的任务中,它的性能和C ++一样。



VM语言(Java,C#)与JIT编译相关的额外成本,但也受益于更高效的内存分配和跨共享内联库。而且C ++在访问OS系统调用时速度要快得多。除此之外,可以仔细调整C ++内存布局以实现缓存行为;您无法在托管语言中获得这种级别的控制权。



以上哪些因素具有更大的影响力完全取决于应用程序。任何人都会声称C ++比Java更快或Java比C ++更快是一个白痴。平均数无关紧要。您的应用程序的性能很重要。






这里是我的证明,gcc正在预先计算答案。 b
$ b

在此代码上:

  #include  
#include< windows.h>

unsigned long long s(unsigned long long n)
{
unsigned long long s = 0; (unsigned long long i = 0; i< n; i ++)




s + = i;

return s;


int main(int argc,char ** argv)
{
LARGE_INTEGER freq,start,end;
QueryPerformanceFrequency(& freq);
QueryPerformanceCounter(& start);

printf(%llu\\\
,s(1000000000));

QueryPerformanceCounter(& end);
double d =(double)(end.QuadPart - start.QuadPart)/ freq.QuadPart * 1000.0;

printf(Delta:%f\\\
,d);

QueryPerformanceCounter(& start);

printf(%llu\\\
,s(atol(argv [1])));

QueryPerformanceCounter(& end);
d =(double)(end.QuadPart - start.QuadPart)/ freq.QuadPart * 1000.0;

printf(Delta:%f\\\
,d);
返回0;
}

使用gcc-4.3.4,使用命令行 ./ g ++ - 4 -omasoud-gcc.exe -O3 masoud.cpp

  bash-3.2#./masoud-gcc 1000000000 
499999999500000000
Delta:0.845755
499999999500000000
Delta:1114.105866

通过比较,MSVC ++ 16.00.40219.01(用于x64(2010 SP1)),命令行 cl / Ox masoud.cpp

 > masoud 1000000000 
499999999500000000
Delta:229.684364
499999999500000000
Delta:354.275606

VC ++不预先计算答案,但64位代码的执行速度提高了三倍以上。这是Java应该达到的速度。






更有趣的事实:gcc比其生成的代码更快地计算出答案计算出来。为gcc编译时间:

  real 0m0.886s 
user 0m0.248s
sys 0m0.185s


I did a simple test, I know C++ is faster but the results of my test is unrealistic.

C++ code is:

#include <stdio.h>
#include <windows.h>

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;
}

Java code is:

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);
    }
}

C++ compiler: gcc 4.4.0 and Java: jdk 1.6.0

Java: 2795 ms

C++ : 0.013517 ms

It says C++ is 206777 times faster than Java! No way! What is wrong in my test?

解决方案

Show the compiler options you used. And your REAL code (#include <stdio> isn't your real code).

Your C++ compiler is much smarter than your Java compiler (this is true on average and in your case, but not every C++ compiler is smarter than every Java compiler), and it precomputed the result. The only thing you're timing is the printf call.

On most of the tasks Java is used for, it performs about as well as C++.

VM languages (Java, C#) have additional costs related to JIT compilation, but also benefit from more efficient memory allocation and inlining across shared libraries. And C++ is much much faster at accessing OS syscalls. Beyond that, C++ memory layouts can be carefully tuned for cache behavior; you don't get that level of control in managed languages.

Which of these factors has more influence is completely application-specific. Anyone making a blanket statement that "C++ is faster in general than Java" or "Java is faster in general than C++" is an idiot. Averages don't matter. Performance on YOUR application matters.


And here is my proof, that gcc is precomputing the answer.

On this code:

#include <stdio.h>
#include <windows.h>

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( int argc, char** argv )
{
    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);

    QueryPerformanceCounter(&start);

    printf("%llu\n", s(atol(argv[1])));

    QueryPerformanceCounter(&end);
    d = (double) (end.QuadPart - start.QuadPart) / freq.QuadPart * 1000.0;

    printf("Delta: %f\n", d);
    return 0;
}

With gcc-4.3.4, using the command-line ./g++-4 -omasoud-gcc.exe -O3 masoud.cpp:

bash-3.2# ./masoud-gcc 1000000000
499999999500000000
Delta: 0.845755
499999999500000000
Delta: 1114.105866

By comparison, MSVC++ 16.00.40219.01 for x64 (2010 SP1), command-line cl /Ox masoud.cpp:

> masoud 1000000000
499999999500000000
Delta: 229.684364
499999999500000000
Delta: 354.275606

VC++ isn't precomputing the answer, but 64-bit code does execute the loop more than three times faster. This is the speed that Java ought to approach.


More fun facts: gcc precomputes the answer faster than the code it generates to calculate it out. Compile time for gcc:

real    0m0.886s
user    0m0.248s
sys     0m0.185s

这篇关于基准C ++ vs Java,不切实际的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆