Last active
May 25, 2022 07:57
-
-
Save magnium/cf96160d248a79f9463439695a7748e8 to your computer and use it in GitHub Desktop.
Compare Numpy and Cupy performance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# based on https://gist.github.com/fukatani/4702aa05aed255cd25f42e77d0a22e37 | |
import time | |
import cupy | |
import numpy | |
cnt = 100 | |
N = 10 | |
def meas_cupy(func, operand): | |
stream = cupy.cuda.Stream.null | |
start = stream.record() | |
for i in range(cnt): | |
func(operand, xp=cupy) | |
end = stream.record() | |
end.synchronize() | |
elapsed = cupy.cuda.get_elapsed_time(start, end) / cnt | |
return elapsed | |
def meas_numpy(func, operand): | |
start = time.time() | |
for i in range(cnt): | |
func(operand, xp=numpy) | |
end = time.time() | |
elapsed = (end - start) * 1000 / cnt | |
return elapsed | |
def meas_numpy_cupy(title, func, shapes): | |
meas_results = [] | |
print() | |
print("===", title, "===") | |
print("Array size\tNumpy\tCupy") | |
for shape in shapes: | |
A = cupy.random.rand(*shape) | |
a = A.get() | |
cupy_elapsed = meas_cupy(func, A) | |
numpy_elapsed = meas_numpy(func, a) | |
meas_results.append((a.size, numpy_elapsed, cupy_elapsed)) | |
for result in meas_results: | |
print("{0:10d}\t{1:.3f}\t{2:.3f}".format(*result)) | |
# ---------------------------------------- | |
shapes_ = ((N, N), (N, N, N, N), (N, N, N, N, N, N), (N, N, N, N, N, N, N)) | |
for title, func in (("Array add", "A+A"), | |
("Array sub", "A-A"), | |
("Array sum", "A.sum()"), | |
("Array argmax", "A.argmax()"), | |
("Array sort", "xp.sort(A)")): | |
meas_numpy_cupy(title, lambda A, xp: eval(func), shapes_) | |
shapes_ = ((N, N), (N, N, N, N), (N, N, N, N, N, N)) | |
for title, func in (("Array tensordot", "xp.tensordot(A, A)"), | |
("Array matmul", "xp.matmul(A, A)"), | |
("Array einsum", "xp.einsum('...i, ...j->...ij', A, A)"), | |
("Array transpose", "xp.moveaxis(A, 0, -1)"), | |
("Array sin", "xp.sin(A)")): | |
meas_numpy_cupy(title, lambda A, xp: eval(func), shapes_) | |
shapes_ = ((N, N), (N * N, N * N), (N * N * N, N * N * N)) | |
for title, func in (("Array eigenvalue", "xp.linalg.eigh(A)"), | |
("Array inv", "xp.linalg.inv(A)")): | |
meas_numpy_cupy(title, lambda A, xp: eval(func), shapes_) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# results for Dell XPS 2020 | |
# CPU: Intel(R) Core(TM) i7-10750H CPU @ 2.60GHz | |
# RAM: 16.0 GB (15.8 GB usable) | |
# GPU: NVIDIA GeForce GTX 1650 Ti | |
# Windows 11 Pro 21H2 | |
# CUDA 11.6 | |
# numpy==1.18.5 | |
# Python 3.7.7 | |
=== Array add === | |
Array size Numpy Cupy | |
100 0.010 0.085 | |
10000 0.020 0.045 | |
1000000 1.860 0.102 | |
10000000 18.062 0.951 | |
=== Array sub === | |
Array size Numpy Cupy | |
100 0.010 0.129 | |
10000 0.020 0.045 | |
1000000 1.780 0.097 | |
10000000 18.374 0.918 | |
=== Array sum === | |
Array size Numpy Cupy | |
100 0.010 0.145 | |
10000 0.020 0.051 | |
1000000 0.430 0.666 | |
10000000 12.210 5.490 | |
=== Array argmax === | |
Array size Numpy Cupy | |
100 0.010 0.112 | |
10000 0.020 0.042 | |
1000000 0.540 1.239 | |
10000000 6.334 12.349 | |
=== Array sort === | |
Array size Numpy Cupy | |
100 0.010 0.203 | |
10000 0.130 0.180 | |
1000000 10.643 3.280 | |
10000000 108.295 39.788 | |
=== Array tensordot === | |
Array size Numpy Cupy | |
100 0.040 0.163 | |
10000 0.210 24.793 | |
1000000 559.860 177.300 | |
=== Array matmul === | |
Array size Numpy Cupy | |
100 0.020 0.204 | |
10000 0.050 0.224 | |
1000000 4.913 4.124 | |
=== Array einsum === | |
Array size Numpy Cupy | |
100 0.020 0.235 | |
10000 0.130 0.160 | |
1000000 23.745 0.566 | |
=== Array transpose === | |
Array size Numpy Cupy | |
100 0.020 0.012 | |
10000 0.020 0.019 | |
1000000 0.020 0.014 | |
=== Array sin === | |
Array size Numpy Cupy | |
100 0.010 0.138 | |
10000 0.080 0.046 | |
1000000 6.185 1.396 | |
=== Array eigenvalue === | |
Array size Numpy Cupy | |
100 0.200 10.206 | |
10000 8.662 5.830 | |
1000000 400.888 102.044 | |
=== Array inv === | |
Array size Numpy Cupy | |
100 0.220 0.699 | |
10000 5.722 1.975 | |
1000000 92.929 46.838 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment