Skip to content

Instantly share code, notes, and snippets.

@mattip
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattip/a7b22dc237fb09a46758 to your computer and use it in GitHub Desktop.
Save mattip/a7b22dc237fb09a46758 to your computer and use it in GitHub Desktop.
test inverse of square matrices of arbitrary size
# Benchmark linalg.inv for square matrices.
# Copyright Matti Picus, 2015
# This is free and unencumbered software released into the public domain.
#Anyone is free to copy, modify, publish, use, compile, sell, or
#distribute this software, either in source code form or as a compiled
#binary, for any purpose, commercial or non-commercial, and by any
#means.
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
#EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
#MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
#IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
#OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
#ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
#OTHER DEALINGS IN THE SOFTWARE.
import time, sys
import numpy as np
if len(sys.argv) < 2:
N = [10]
else:
N = [int(s) for s in sys.argv[1:]]
np.random.seed(10)
first = True
for n in N:
a = np.random.random([n, n])
if first:
#warmup
for i in xrange(5000):
b = np.linalg.inv(a)
first = False
tic = time.clock()
for i in xrange(100000000 / (n * n)):
b = np.linalg.inv(a)
toc = time.clock()
delta = toc - tic
print 'after %8d, for n=%3d, time=%.2f, %9.2f msec/1000 loops' % (i, n, delta, delta*1000*1000/i)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment