NumPyはやっぱすごい
Python3.4以降にはstatisticsというモジュールが標準ライブラリに追加された。標準ライブラリなので使う人が多くなる可能性があるが、NumPyが使えるなら当然のようにそちらのほうが早い。
適当にベンチマークしてみた結果を示す。
ベンチマークコード
#!/usr/bin/python import statistics import numpy as np from benchmarker import Benchmarker loop = 100 batch = 512 with Benchmarker(loop, cycle=10, extra=2) as bench: test_data = np.random.randint(0, 256, loop * batch) @bench(None) def _(bm): for i in bm: perm = np.random.permutation(loop * batch) @bench("NumPy") def _(bm): for i in bm: perm = np.random.permutation(loop * batch) a = np.asarray(test_data[perm[i:i + batch]]) mean = np.mean(a) median = np.median(a) std = np.std(a) @bench("normal") def _(bm): for i in bm: perm = np.random.permutation(loop * batch) a = test_data[perm[i:i + batch]].tolist() mean = statistics.mean(a) median = statistics.median(a) std = statistics.pstdev(a)
結果
## benchmarker: release 4.0.1 (for python) ## python version: 3.5.1 ## python compiler: MSC v.1900 64 bit (AMD64) ## python platform: Windows-7-6.1.7601-SP1 ## python executable: C:\Anaconda3\python.exe ## cpu model: AMD64 Family 21 Model 2 Stepping 0, AuthenticAMD ## parameters: loop=100, cycle=10, extra=2 ## (#1) real (total = user + sys) (Empty) 0.7140 0.6864 0.6864 0.0000 NumPy 0.0230 0.0468 0.0468 0.0000 normal 0.4340 0.4524 0.4524 0.0000 ## (#2) real (total = user + sys) (Empty) 0.6730 0.6708 0.6708 0.0000 NumPy 0.0120 0.0000 0.0000 0.0000 normal 0.4230 0.4368 0.4368 0.0000 ## (#3) real (total = user + sys) (Empty) 0.6700 0.6708 0.6708 0.0000 NumPy 0.0340 0.0312 0.0312 0.0000 normal 0.4480 0.4368 0.4368 0.0000 ## (#4) real (total = user + sys) (Empty) 0.6790 0.6864 0.6864 0.0000 NumPy 0.0130 -0.0000 -0.0000 0.0000 normal 0.4330 0.4368 0.4368 0.0000 ## (#5) real (total = user + sys) (Empty) 0.6660 0.6552 0.6552 0.0000 NumPy 0.0290 0.0156 0.0156 0.0000 normal 0.4310 0.4368 0.4368 0.0000 ## (#6) real (total = user + sys) (Empty) 0.6690 0.6708 0.6708 0.0000 NumPy 0.0340 0.0312 0.0312 0.0000 normal 0.4420 0.4212 0.4212 0.0000 ## (#7) real (total = user + sys) (Empty) 0.6810 0.6708 0.6708 0.0000 NumPy 0.0180 0.0312 0.0312 0.0000 normal 0.4420 0.4212 0.4056 0.0156 ## (#8) real (total = user + sys) (Empty) 0.6820 0.6708 0.6708 0.0000 NumPy 0.0770 0.0780 0.0780 0.0000 normal 0.4740 0.4680 0.4680 0.0000 ## (#9) real (total = user + sys) (Empty) 0.7030 0.6864 0.6864 0.0000 NumPy 0.0020 0.0156 0.0156 0.0000 normal 0.5320 0.4368 0.4368 0.0000 ## (#10) real (total = user + sys) (Empty) 0.7380 0.7020 0.7020 0.0000 NumPy -0.0350 -0.0156 -0.0156 0.0000 normal 0.3830 0.4056 0.4056 0.0000 ## (#11) real (total = user + sys) (Empty) 0.6780 0.6864 0.6864 0.0000 NumPy 0.0220 0.0156 0.0156 0.0000 normal 0.4500 0.4368 0.4368 0.0000 ## (#12) real (total = user + sys) (Empty) 0.6680 0.6708 0.6708 0.0000 NumPy 0.0280 0.0156 0.0156 0.0000 normal 0.4430 0.4524 0.4524 0.0000 ## (#13) real (total = user + sys) (Empty) 0.6750 0.6708 0.6708 0.0000 NumPy 0.0120 0.0156 0.0156 0.0000 normal 0.4460 0.4524 0.4368 0.0156 ## (#14) real (total = user + sys) (Empty) 0.7000 0.6708 0.6708 0.0000 NumPy -0.0030 0.0156 0.0156 0.0000 normal 0.4320 0.4524 0.4524 0.0000 ## Ignore min & max min cycle max cycle NumPy -0.0350 (#10) 0.0770 (#8) -0.0030 (#14) 0.0340 (#6) normal 0.3830 (#10) 0.5320 (#9) 0.4230 (#2) 0.4740 (#8) ## Average of 10 (=14-2*2) real (total = user + sys) NumPy 0.0193 0.0187 0.0187 0.0000 normal 0.4401 0.4399 0.4368 0.0031 ## Ranking real NumPy 0.0193 (100.0) ******************** normal 0.4401 ( 4.4) * ## Matrix real [01] [02] [01] NumPy 0.0193 100.0 2280.3 [02] normal 0.4401 4.4 100.0