图 1:Geekbench 比较(CPU 和 GPU)。
M1 芯片在 2019 Mac 中超越了 Intel 芯片。该基准测试仅衡量整体机器性能,与本文要进行的数据科学基准测试并不是百分百相关。 性能测试——纯 Python
以下是在该基准中执行的任务列表:
创建一个包含 100 至 999 之间的 100000000 随机整数的列表 l;
对列表 l 中的每个项目求平方;
取 l 中每一项的平方根;
将相应的平方和平方根相乘;
相应的平方和平方根相除;
对相应的平方和平方根进行整除运算。
该测试仅使用内置 Python 库,不含 Numpy。以下是测试的代码段
import random time_start = datetime.now ( ) l = [ random.randrange ( 100, 999 ) for i in range ( 100000000 ) ] squared = [ x**2 for x in l ] sqrt = [ x**0.5 for x in l ] mul = [ x * y for x, y in zip ( squared, sqrt ) ] div = [ x / y for x, y in zip ( squared, sqrt ) ] int_div = [ x // y for x, y in zip ( squared, sqrt ) ] time_end = datetime.now ( ) print ( f'TOTAL TIME = { ( time_end - time_start ) .seconds} seconds' )
结果如下:
[attach]37320[/attach]
图 2:Python 速度测试,越低为越好。
通过 Anaconda(和 Rosseta 2)在 M1 Mac 上运行的 Python 减少了 196 秒的运行时。最好是在本地运行 Python,因为这样就能将运行时进一步减少 43 秒。 性能测试—— Numpy
以下是在该基准中执行的任务列表:
矩阵乘法
向量乘法
奇异值分解
Cholesky 分解
特征分解
脚本如下
# SOURCE: http://gist.github.com/markus-beuckelmann/8bc25531b11158431a5b09a45abd6276import numpy as npfrom time import timefrom datetime import datetimestart_time = datetime.now ( ) # Let's take the randomness out of random numbers ( for reproducibility ) np.random.seed ( 0 ) size = 4096A, B = np.random.random ( ( size, size ) ) , np.random.random ( ( size, size ) ) C, D = np.random.random ( ( size * 128, ) ) , np.random.random ( ( size * 128, ) ) E = np.random.random ( ( int ( size / 2 ) , int ( size / 4 ) ) ) F = np.random.random ( ( int ( size / 2 ) , int ( size / 2 ) ) ) F = np.dot ( F, F.T ) G = np.random.random ( ( int ( size / 2 ) , int ( size / 2 ) ) ) # Matrix multiplicationN = 20t = time ( ) for i in range ( N ) :np.dot ( A, B ) delta = time ( ) - tprint ( 'Dotted two %dx%d matrices in %0.2f s.' % ( size, size, delta / N ) ) del A, B# Vector multiplicationN = 5000t = time ( ) for i in range ( N ) :np.dot ( C, D ) delta = time ( ) - tprint ( 'Dotted two vectors of length %d in %0.2f ms.' % ( size * 128, 1e3 * delta / N ) ) del C, D# Singular Value Decomposition ( SVD ) N = 3t = time ( ) for i in range ( N ) :np.linalg.svd ( E, full_matrices = False ) delta = time ( ) - tprint ( "SVD of a %dx%d matrix in %0.2f s." % ( size / 2, size / 4, delta / N ) ) del E# Cholesky DecompositionN = 3t = time ( ) for i in range ( N ) :np.linalg.cholesky ( F ) delta = time ( ) - tprint ( "Cholesky decomposition of a %dx%d matrix in %0.2f s." % ( size / 2, size / 2, delta / N ) ) # Eigendecompositiont = time ( ) for i in range ( N ) :np.linalg.eig ( G ) delta = time ( ) - tprint ( "Eigendecomposition of a %dx%d matrix in %0.2f s." % ( size / 2, size / 2, delta / N ) ) print ( '' ) end_time = datetime.now ( ) print ( f'TOTAL TIME = { ( end_time - start_time ) .seconds} seconds' )
测试结果如下:
[attach]37321[/attach]