【发布时间】:2021-11-30 13:23:04
【问题描述】:
我在 PC 和智能手机上做了一些 Python 性能比较,结果令人困惑。
PC:i7-8750H / 32GB RAM / 1TB SSD / Windows 10
智能手机:Android 11 上带有 Termux Linux 模拟器的 Galaxy S10
第一个是简单的蒙特卡罗模拟,代码如下。
import random
import time
def monte_carlo_pi(n_samples: int):
acc = 0
for i in range(n_samples):
x = random.random()
y = random.random()
if (x**2 + y**2) < 1.0:
acc += 1
return 4.0 * acc / n_samples
start_time = time.time()
print(monte_carlo_pi(10000000))
print(time.time()-start_time)
令人惊讶的是,PC 大约需要 5.2 秒,智能手机大约需要 2.7 秒。
其次是使用带有一些数据框操作的 pandas。
import pandas as pd
import time
start_time = time.time()
df = pd.DataFrame(
[ [21, 72, -67],
[23, 78, 62],
[32, 74, 54],
[52, 54, 76],
[0, 23, 66],
[2, 1, 2] ],
index = [11, 22, 33, 44, 55, 66],
columns = ['a', 'b', 'c'])
df2 = pd.DataFrame()
df2 = pd.concat([df2, df['a']], axis=1)
df2 = pd.concat([df2, df['c']], axis=1)
print(df2)
print(time.time()-start_time)
这一次,PC 大约是 0.007 秒,智能手机大约是 0.009 秒,但实际时间来自 exec。完成智能手机大约需要 2 秒。我的猜测是智能手机加载冗长的熊猫包需要更长的时间,但不确定。
- ARM 处理器在简单的重复计算上是否更快?或者其中一个处理器是否使用多核功能?
- 如上所述,智能手机读取冗长包裹的速度是否相对较慢?
- 是否有更好的方法来衡量 PC 和智能手机之间的整体 Python 性能?
【问题讨论】:
标签: python performance comparison