您的位置:首页 > 编程语言 > C语言/C++

python,java,c,c++ 效率比较

2016-07-14 18:19 441 查看
$ cat test.py

sum=0

for i in range(100000*10000):

    sum += i

print sum    

[]$ time python test.py

499999999500000000

real    3m37.687s

user    3m22.448s

sys     0m15.127s

[]$ time pypy test.py

499999999500000000

real    0m10.573s

user    0m10.540s

sys     0m0.021s

[]$ cat test.java

public class test{

    public static void main(String[] args)

    {

        long i=0,sum=0;

        for(;i<100000*10000;i++)

            sum += i;

        System.out.printf("%d\n",sum);         

    }



[]$ java test

499999999500000000

[]$ time java test

499999999500000000

real    0m0.862s

user    0m0.854s

sys     0m0.013s

[]$ cat test.cpp

#include <stdio.h>

int main(int argc,char * args[])

{

    long sum = 0;

    long i = 0;

    for ( ;i< 100000*10000;i++)

        sum += i;

    printf("%ld\n",sum);    

}

[]$ time ./testcpp 

499999999500000000

real    0m3.467s

user    0m3.465s

sys     0m0.000s

[]$ g++ -O test.cpp -o testcpp

[]$ time ./testcpp 

499999999500000000

real    0m0.002s

user    0m0.001s

sys     0m0.000s

[]$ cat test.c

#include <stdio.h>

int main(int argc,char * args[])

{

    long sum = 0;

    long i = 0;

    for ( ;i< 100000*10000;i++)

        sum += i;

    printf("%ld\n",sum);    

}

[]$ gcc test.c -O testc

gcc: testc: No such file or directory

[]$ gcc test.c -o testc

[]$ time ./testc

499999999500000000

real    0m3.500s

user    0m3.495s

sys     0m0.004s

[]$ gcc -O test.c -o testc

[]$ time ./testc

499999999500000000

real    0m0.001s

user    0m0.000s

sys     0m0.000s

[]$ gcc -O2 test.c -o testc

[]$ time ./testc

499999999500000000

real    0m0.001s

user    0m0.001s

sys     0m0.000s

>>> (100000*10000)/(3*60+37.687)

4593751.5791021055

>>> 

>>> (3*60+37.687)/(100000*10000)

2.1768700000000001e-07

>>> (100000*10000)/(10.54)

94876660.341555983

>>> (100000*10000)/(0.850)

1176470588.2352941

>>> 0.850/(100000*10000)

8.4999999999999996e-10

>>> (100000*10000)/(3.495)

286123032.90414876

>>> 3.495/(100000*10000)

3.495e-09

>>> (100000*10000)/(0.001)

1000000000000.0

>>> 0.001/(100000*10000)

9.9999999999999998e-13

python  202.448s   202448  4593751 4.59*10(6)   20 

pypy    10.540s   10540   94876660 9.48*10(7)  3 

cpp     3.465s   3465     286123032 2.86*10(8)   4

java    0.850s   854     1176470588 1.176*10(9) 1

cpp-O   0m0.001s    1

c       3.495s  3495

c-O     0.000s  1

c-O2    0m0.001s 1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: