搜尋
首頁後端開發Python教學python比java慢多少

python比java慢多少

Jun 12, 2019 am 11:39 AM
python

大家都知道Python最大的缺點就是效能差,到底多差網路上沒有具體數據,今天做了一個測試 。

python比java慢多少

首先要聲明一下, 這個簡單測試只是透過對陣列的建立、統計的耗時比較, 本以為陣列操作是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中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
列表和陣列之間的選擇如何影響涉及大型數據集的Python應用程序的整體性能?列表和陣列之間的選擇如何影響涉及大型數據集的Python應用程序的整體性能?May 03, 2025 am 12:11 AM

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

說明如何將內存分配給Python中的列表與數組。說明如何將內存分配給Python中的列表與數組。May 03, 2025 am 12:10 AM

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

您如何在Python數組中指定元素的數據類型?您如何在Python數組中指定元素的數據類型?May 03, 2025 am 12:06 AM

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

什麼是Numpy,為什麼對於Python中的數值計算很重要?什麼是Numpy,為什麼對於Python中的數值計算很重要?May 03, 2025 am 12:03 AM

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

討論'連續內存分配”的概念及其對數組的重要性。討論'連續內存分配”的概念及其對數組的重要性。May 03, 2025 am 12:01 AM

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

您如何切成python列表?您如何切成python列表?May 02, 2025 am 12:14 AM

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

在Numpy陣列上可以執行哪些常見操作?在Numpy陣列上可以執行哪些常見操作?May 02, 2025 am 12:09 AM

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

Python的數據分析中如何使用陣列?Python的數據分析中如何使用陣列?May 02, 2025 am 12:09 AM

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

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

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

熱工具

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

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

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

SublimeText3 英文版

SublimeText3 英文版

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

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

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