Skip to content

Instantly share code, notes, and snippets.

@marw
Created September 7, 2017 19:34
Show Gist options
  • Save marw/bb7315533c6ab462edc38680f2bd506c to your computer and use it in GitHub Desktop.
Save marw/bb7315533c6ab462edc38680f2bd506c to your computer and use it in GitHub Desktop.
import time
import argparse
import multiprocessing
import random
import numpy as np
M = 250
# size ~ 0.5MB
X = np.random.randn(M, M)
# to allow CPU with 6 cores to end tasks at once
TASK_N = 180
# size ~ 90MB
MATRICES = 180
def small_mat_test(i):
for i in range(TASK_N):
X.dot(X)
def small_mat(args):
"""
Testing small matrices that should fit in CPU cache.
"""
pool = multiprocessing.Pool()
start_time = time.time()
pool.map(small_mat_test, list(range(args.n)))
elapsed_time = time.time() - start_time
return elapsed_time
def list_mat(args):
"""
Testing list of bigger matrices that probably not fit in CPU cache.
"""
arrays = []
for i in range(MATRICES):
arrays.append(np.random.randn(M, M))
def list_mat_test(i):
idx1 = list(range(MATRICES))
idx2 = list(range(MATRICES))
random.shuffle(idx1)
random.shuffle(idx2)
for i in range(MATRICES):
arrays[idx1[i]].dot(arrays[idx2[i]])
pool = multiprocessing.Pool()
start_time = time.time()
pool.map(small_mat_test, list(range(args.n)))
elapsed_time = time.time() - start_time
return elapsed_time
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--suite', choices=('small_mat', 'list_mat'), default='small_mat')
parser.add_argument('--n', help='Number of iterations', default=3*16)
args = parser.parse_args()
if args.suite == 'small_mat':
elapsed_time = small_mat(args)
print('[Small Matrix Test] Elapsed: {:0.3f} seconds Matrix: {}x{} Multiplications: {} MulPerSec: {:.2f}'.format(
elapsed_time, M, M, args.n * TASK_N, (args.n * TASK_N)/elapsed_time))
elif args.suite == 'list_mat':
elapsed_time = list_mat(args)
print('[List Matrix Test] Elapsed: {:0.3f} seconds Matrix: {}x{} x {} Multiplications: {} MulPerSec: {:.2f}'.format(
elapsed_time, M, M, MATRICES, args.n * MATRICES, (args.n * MATRICES) / elapsed_time))
else:
raise ValueError('Nobody expects invalid parameter!')
if __name__ == '__main__':
main()
@marw
Copy link
Author

marw commented Sep 7, 2017

My various results, more or less correlate with https://www.cpubenchmark.net/

AMD Phenom(tm) II X6 1090T (6/6 cores/threads @ 3.2GHz)
    Python 3.4.3 (default, Nov 17 2016, 01:08:31)
    [Small Matrix Test] Elapsed: 6.224 seconds Matrix: 250x250 Multiplications: 8640 MulPerSec: 1388.27
    [List Matrix Test] Elapsed: 6.537 seconds Matrix: 250x250 x 180 Multiplications: 8640 MulPerSec: 1321.78
        
    Python 3.6.1 |Anaconda 4.4.0 (64-bit)
    [Small Matrix Test] Elapsed: 7.070 seconds Matrix: 250x250 Multiplications: 8640 MulPerSec: 1222.03
    [List Matrix Test] Elapsed: 7.141 seconds Matrix: 250x250 x 180 Multiplications: 8640 MulPerSec: 1209.96
    
Intel(R) Celeron(R) CPU  J1900 (4/4 cores/threads @ 1.99GHz)
    Python 3.5.2 (default, Nov 17 2016, 17:05:23)
    [Small Matrix Test] Elapsed: 37.147 seconds Matrix: 250x250 Multiplications: 8640 MulPerSec: 232.59
    [List Matrix Test] Elapsed: 37.301 seconds Matrix: 250x250 x 180 Multiplications: 8640 MulPerSec: 231.63
    
Intel(R) Core(TM) i3-5005U CPU (2/4 cores/threads @ 2.00GHz)
    Python 3.5.2 (default, Nov 17 2016, 17:05:23)
    [Small Matrix Test] Elapsed: 21.233 seconds Matrix: 250x250 Multiplications: 8640 MulPerSec: 406.90
    [List Matrix Test] Elapsed: 21.590 seconds Matrix: 250x250 x 180 Multiplications: 8640 MulPerSec: 400.18

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment