关于 PC 与智能手机混合 Python 性能结果的问题

Question on mixed Python performance results from PC vs smartphone

我在 PC 和智能手机上做了一些 Python 性能比较,结果令人困惑。

PC:i7-8750H / 32GB 内存 / 1TB SSD / Windows 10
智能手机:在 Android 11

上带有 Termux Linux 模拟器的 Galaxy S10

第一个很简单 Monte Carlo 使用以下代码进行模拟。

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 秒。我的猜测是智能手机需要更长的时间来加载冗长的 pandas 包,但不确定。

  1. ARM 处理器在简单的重复计算上是否更快?或者是或不是利用多核功能的处理器之一?
  2. 如上所述,智能手机在阅读冗长的包裹时是否相对较慢?
  3. 是否有更好的方法来衡量 PC 和智能手机之间的整体 Python 性能?

您的 Python 代码的执行时间受开销限制。实际上,调用 random.random() 需要大量时间来考虑迭代次数和一次迭代的时间。这来自缓慢的模块获取。您可以使用 rand = random.random 缓存函数。此外, x**2 + y**2 也没有优化。 x*x + y*y 可以用来加速计算。此外,您也不需要分支:您可以只使用 acc += (x*x + y*y) < 1.0。这是结果代码:

def monte_carlo_pi(n_samples: int):
    acc = 0
    rand = random.random
    for i in range(n_samples):
        x = rand()
        y = rand()
        acc += (x*x + y*y) < 1.0
    return 4.0 * acc / n_samples

这段代码在我的机器上大约快了 3 倍。与本机代码相比,它仍然很慢。例如,在这个简单的代码上使用 Numba JIT 甚至快 12 倍(与原始代码相比快 35 倍)。 这种缓慢的执行来自标准 CPython 解释器本身。您可以尝试 PyPy(基于 JIT 的解释器)是否希望这样的代码在不更改代码本身的情况下执行得更快。

最后,请注意 Numpy 包通常用于此类代码以获得高效代码。

Is ARM processor faster on simple repetitive calculations? Or is or isn't either one of the processor utilizing multi-core capability?

None两者。解释器的速度可能会因其在两个不同平台上的编译方式而发生变化。此外,操作系统可以对代码的性能(通常是分配速度)发挥重要作用。在您的情况下,一个代码是 Windows 上的 运行,而另一个代码是 Android 上的 运行(Linux 之类)。据我所知,Windows 上的对象分配速度明显较慢。

Is smartphone relatively slow on reading lengthy packages as observed above?

这在很大程度上取决于您的硬件,尤其是您的存储设备、RAM 和 CPU。文件系统和 OS 本身也起着重要作用。要知道为什么平台比另一个平台慢,最好的办法就是使用分析器

Is there a better way to measure overall Python performance between PC and smartphone?

有几个基准可以评估 Python 实施在多个平台上的性能。其中之一是 this one. Benchmarking is quite-hard (especially micro-benchmarking) because you can very-easily draw the wrong conclusions due to a missing factor. Think about what you really want to measure and then reduce every possible source of noise to nothing because comparing several factor at once is a major source of benchmarking mistakes. For example, use the same OS on the two devices (eg. Android or at least a Linux-based systems with the same kernel version and a similar configuration/flags). You should use the exact same version of CPython. You should also compile CPython yourself using the same compiler with the same flags (or possibly use equivalent pre-compiled binaries). The version of libraries/packages often matter too. This is especially true for the libc (standard C library) and GMP (library to efficiently compute big numbers). You should also use a partition with the same file-system if you want to measure things related to it. I am probably missing many important points. Sometimes a code can be much slower than another just because it is not aligned the same way in memory (and alignment policy changes regarding the target platform)! Even 很多。这就是为什么您应该使用 profiler 检查为什么相同的代码在不同的上下文中是 slower/faster(例如,Linux 上的 perf 或者 Linux 上的 VTune Windows).

以下是相关帖子:

这里有有趣的谈话: