C++,Java,Python,Fortran到底哪个更快?
在《C++的速度比Java快2.1%:來自計算100萬以內(nèi)質(zhì)數(shù)的實驗數(shù)據(jù)對比》這篇文中比較了C++和Java的速度,現(xiàn)在接著用同樣的辦法通過算1000000以內(nèi)的質(zhì)數(shù)來比較python和fortran的速度。
Python代碼
import time
import datetime
t =time.time()??
#print(int(round(t * 1000)))
i=2
j=2
for i inrange(2,1000000):
?? for j in range(2,(int(i/j)+2)??? ):
???? #print(i,"*",j,(int(i/j)+1)????????)
?????
????? if(i%j==0):
???????? #print(i,j,"???????????? **")
???????? break
? # if(j >int (i/j)):
???????? #print(i)
t1 =time.time()?????
print(int(round(t1 * 1000))-int(round(t * 1000)) ,"********* " )
?
#########################################
!!!!!!!!!!!!!!!!!!!!!!!!!
fortran代碼
program main
?
??? implicit none
??? integer::i,j
?Real :: r1 = 0.0
?Real :: r2 = 0.0
?Real :: r3 = 0.0
?Real :: r4 = 0.0
?Real :: ra1 = 0.0
?Real :: ra2 = 0.0
?Real :: ra3 = 0.0
?Real :: ra4 = 0.0
?
character(8)::date
character(10)::time
character(5)::zone
integer,dimension(8)::values
?
character(8)::datea
character(10)::timea
character(5)::zonea
integer,dimension(8)::valuesa
?
!using keywordarguments
calldate_and_time(date,time,zone,values)
calldate_and_time(DATE=date,ZONE=zone)
calldate_and_time(TIME=time)
calldate_and_time(VALUES=values)
?
? print '(a,2x,a,2x,a)',date,time,zone
? print '(8i5))',values
?
??? outer: do i=2,1000000
???????? inner:do j=2, i/j?????
?
???????????????? ? !print "(3i5)",? i/j ,i,j
???????????????? ?if( j*(i/j)==i)then
??????????????????????????? exit inner???????????????
????????????????????????????????? end if
????????? end do? inner
??????????? if(j > (i/j)) then
????? ??????????print "(3i10)",i
?????????? ?????????? endif
??? end do outer
?
calldate_and_time(datea,timea,zonea,valuesa)
calldate_and_time(DATE=datea,ZONE=zonea)
calldate_and_time(TIME=timea)
calldate_and_time(VALUES=valuesa)
?
?????????? Read(time(1:2) , * ) r1
? ? ? ? ? ?Read( time(3:4) , * ) r2
?????????? Read( time(5:6) , * ) r3
?????????? Read( time(8:10) , * ) r4
????? ?Read( timea(1:2) , * ) ra1
????? ?Read( timea(3:4) , * ) ra2
????? ?Read( timea(5:6) , * ) ra3
????? ?Read( timea(8:10) , * ) ra4
?
? print '(a,2x,a,2x,a)',datea,timea,zonea
? !print '(8i5)',valuesa
?
? print '(f11.3)', (ra1*60*60*1000+ra2*60*1000+ra3*1000+ra4 )-(r1*60*60*1000+ r2*60*1000+r3*1000+r4)
! print'(f11.3)', ra1,ra2,ra3,ra4
?
end program
?
運算過程基本是一樣的,因為python的冒號后面必須有東西所以把# if(j >int (i/j)):
???????? #print(i) 這兩句話都注釋掉了,實際上python的程序比其他的程序還少了一步,fortran的時間計算因為不能直接的得到ms的時間還要轉(zhuǎn)化一下,這一步慢了一點。
| c++(不顯示中間過程ms) | 顯示過程ms | java(不顯示中間過程ms) | 顯示過程ms | python(不顯示中間過程ms) | fortran(不顯示中間過程ms) | 顯示過程ms |
| 967 | 58228 | 844 | 1624 | 4625593 | 100717 | 109126 |
| 738 | 57315 | 549 | 1716 | 4492283 | 102039 | 109546 |
| 736 | 56691 | 929 | 1833 | 5211503 | 101634 | 108775 |
| 784 | 57116 | 862 | 1884 | * | 100651 | 122592 |
| 768 | 56679 | 866 | 1801 | * | 101487 | 121870 |
| 888 | 81498 | 922 | 1801 | * | 101261 | 121646 |
| 713 | 79003 | 860 | 1792 | * | 101379 | 120226 |
| 881 | 79574 | 833 | 1891 | * | 100606 | 110631 |
| 962 | 79737 | 851 | 1806 | * | 106303 | 108145 |
| 875 | 78760 | 916 | 1894 | * | 94476 | 108237 |
| 729 | 78189 | 786 | 1807 | ? | ||
| 846 | 82891 | 735 | 1777 | ? | ||
| 860 | 79148 | 785 | 1745 | |||
| 870 | 76569 | 878 | 1737 | |||
| 788 | 79128 | 754 | 1770 | |||
| 779 | 72847 | 828 | 1895 | |||
| 731 | 74039 | 852 | 1766 | |||
| 656 | 72004 | 919 | 1863 | |||
| 835 | 76723 | 858 | 1979 | ? | ||
| 839 | 77570 | 857 | 1882 | |||
| 840 | 70929 | 924 | 1747 | ? | ||
| 833 | 73277 | 952 | 1861 | |||
| 900 | 73976 | 659 | 1765 | |||
| 747 | 73794 | 783 | 1830 | |||
| 795 | 73472 | 813 | 1764 | |||
| 849 | 70584 | 769 | 1784 | |||
| 832 | 72319 | 834 | 1740 | |||
| 809 | 71475 | 892 | 1810 | |||
| 834 | 72823 | 818 | 1808 | |||
| 766 | 71847 | 834 | 1684 | |||
| 平均 | ||||||
| 815 | 72606.833 | 832.06667 | 1801.8667 | 4776459.7 | 101055.3 | 114079.4 |
因為速度差距過于懸殊fortran只算了10次,python算了3次,python也沒有測量控制臺該顯示所有中間過程的時間
得到數(shù)據(jù)在控制臺不顯示中間過程的情況下
C++ ? ? java ? ? fortran ? ? ? ?python
815???? 832 ? ? 101055???? 4776459
1???????? 1.02???? 123????????? 5860
也就是C++的計算速度是java的1.02倍,是fortran的123倍,是python的5860倍
控制臺顯示中間過程的情況的速度比較
Java?? ???c++ ? ? ? ? ?fortran??????python
1801? ??72606???114079?????? 4776459
1 ? ? ? ? ? ? 40 ? ? ? ? ? ?63??????????????? 2652
Java控制臺顯示速度是c++的40倍,是fortran的63倍,至少是python的2652倍??????? ????
?
總結(jié)
以上是生活随笔為你收集整理的C++,Java,Python,Fortran到底哪个更快?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++的速度比Java快2.1%:来自计
- 下一篇: 用反向传导分子模型去计算基团的定位效应