大家都知道Python最大的缺點就是效能差,到底多差網路上沒有具體數據,今天做了一個測試 。
首先要聲明一下, 這個簡單測試只是透過對陣列的建立、統計的耗時比較, 本以為陣列操作是python的強項,應該差別不大,但結果讓人大跌眼鏡:建立陣列Python耗時是java的14倍多,統計耗時Python是java的24倍多。
硬體環境:
Win8.1 64位元、i5 4670K 超到4.2GHZ,記憶體8G
軟體:
python 3.4, JDK 1.6
Phton的:
import time c_rows=10000 c_cols=10000 lvStart=time.time() #print(lvStart) # 建立 [10000][10000]的整数阵列 lvArr=[[] for row in range(1,c_rows+1,1)] lvRows=len(lvArr) for row in range(0,lvRows,1): lvArr[row]=[col for col in range(1,c_cols+1,1)] lvEnd=time.time() print("Create lvArray Use:%f" %((lvEnd-lvStart)*1000)) lvStart=time.time() lvSum=sum(list(map(sum,lvArr))) lvEnd=time.time() print("lvArray summary is :%d" %(lvSum)) print("Calculate lvArray Use:%f" %((lvEnd-lvStart)*1000))
結果:
Create lvArray Use:3654.559135 lvArray summary is :500050000000 Calculate lvArray Use:962.199926 [Finished in 6.3s]
========
JAVA的:
public class Test { public static void main(String[] args) { long lvStart = System.currentTimeMillis(); int[][] lvArr = new int[10000][10000]; for (int row = 0; row < lvArr.length; row++) { for (int col = 0; col < lvArr[row].length; col++) { lvArr[row][col] = col + 1; } } long lvEnd = System.currentTimeMillis(); System.out.println(String.format("Create lvArray Use:%d", lvEnd - lvStart)); lvStart = System.currentTimeMillis(); long lvSum = 0; for (int row = 0; row < lvArr.length; row++) { for (int col = 0; col < lvArr[row].length; col++) { lvSum += lvArr[row][col]; } } lvEnd = System.currentTimeMillis(); System.out.println(String.format("lvArray summary is :%d", lvSum)); System.out.println(String.format("Calculate lvArray Use:%d", lvEnd-lvStart)); } }
結果:
Create lvArray Use:257 lvArray summary is :500050000000 Calculate lvArray Use:39
原本以為Python會在列表、陣列方法的運算用到並行計算,但沒有,感覺很可惜。
C/Delphi這些硬傢伙就不比較了,絕對秒得沒影。
以上是python比java慢多少的詳細內容。更多資訊請關注PHP中文網其他相關文章!

ForhandlinglargedatasetsinPython,useNumPyarraysforbetterperformance.1)NumPyarraysarememory-efficientandfasterfornumericaloperations.2)Avoidunnecessarytypeconversions.3)Leveragevectorizationforreducedtimecomplexity.4)Managememoryusagewithefficientdata

Inpython,ListSusedynamicMemoryAllocationWithOver-Asalose,而alenumpyArraySallaySallocateFixedMemory.1)listssallocatemoremoremoremorythanneededinentientary上,respizeTized.2)numpyarsallaysallaysallocateAllocateAllocateAlcocateExactMemoryForements,OfferingPrediCtableSageButlessemageButlesseflextlessibility。

Inpython,YouCansspecthedatatAtatatPeyFelemereModeRernSpant.1)Usenpynernrump.1)Usenpynyp.dloatp.dloatp.ploatm64,formor professisconsiscontrolatatypes。

NumPyisessentialfornumericalcomputinginPythonduetoitsspeed,memoryefficiency,andcomprehensivemathematicalfunctions.1)It'sfastbecauseitperformsoperationsinC.2)NumPyarraysaremorememory-efficientthanPythonlists.3)Itoffersawiderangeofmathematicaloperation

Contiguousmemoryallocationiscrucialforarraysbecauseitallowsforefficientandfastelementaccess.1)Itenablesconstanttimeaccess,O(1),duetodirectaddresscalculation.2)Itimprovescacheefficiencybyallowingmultipleelementfetchespercacheline.3)Itsimplifiesmemorym

SlicingaPythonlistisdoneusingthesyntaxlist[start:stop:step].Here'showitworks:1)Startistheindexofthefirstelementtoinclude.2)Stopistheindexofthefirstelementtoexclude.3)Stepistheincrementbetweenelements.It'susefulforextractingportionsoflistsandcanuseneg

numpyallowsforvariousoperationsonArrays:1)basicarithmeticlikeaddition,減法,乘法和division; 2)evationAperationssuchasmatrixmultiplication; 3)element-wiseOperations wiseOperationswithOutexpliitloops; 4)

Arresinpython,尤其是Throughnumpyandpandas,weessentialFordataAnalysis,offeringSpeedAndeffied.1)NumpyArseNable efflaysenable efficefliceHandlingAtaSetSetSetSetSetSetSetSetSetSetSetsetSetSetSetSetsopplexoperationslikemovingaverages.2)


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Dreamweaver Mac版
視覺化網頁開發工具

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。