Mercurial > hg > pycsalgos
comparison tests/testnumpy.py @ 7:9079a9f7c4cf
More tests
author | nikcleju |
---|---|
date | Sat, 05 Nov 2011 11:41:38 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
6:cc7239d5e972 | 7:9079a9f7c4cf |
---|---|
1 """ | |
2 Benchmark script to be used to evaluate the performance improvement of the MKL | |
3 """ | |
4 | |
5 import os | |
6 import sys | |
7 import timeit | |
8 | |
9 import numpy | |
10 from numpy.random import random | |
11 | |
12 def test_eigenvalue(): | |
13 """ | |
14 Test eigen value computation of a matrix | |
15 """ | |
16 i = 500 | |
17 data = random((i,i)) | |
18 result = numpy.linalg.eig(data) | |
19 | |
20 def test_svd(): | |
21 """ | |
22 Test single value decomposition of a matrix | |
23 """ | |
24 i = 1000 | |
25 data = random((i,i)) | |
26 result = numpy.linalg.svd(data) | |
27 result = numpy.linalg.svd(data, full_matrices=False) | |
28 | |
29 def test_inv(): | |
30 """ | |
31 Test matrix inversion | |
32 """ | |
33 i = 1000 | |
34 data = random((i,i)) | |
35 result = numpy.linalg.inv(data) | |
36 | |
37 def test_det(): | |
38 """ | |
39 Test the computation of the matrix determinant | |
40 """ | |
41 i = 1000 | |
42 data = random((i,i)) | |
43 result = numpy.linalg.det(data) | |
44 | |
45 def test_dot(): | |
46 """ | |
47 Test the dot product | |
48 """ | |
49 i = 1000 | |
50 a = random((i, i)) | |
51 b = numpy.linalg.inv(a) | |
52 result = numpy.dot(a, b) - numpy.eye(i) | |
53 | |
54 # Test to start. The dict is the value I had with the MKL using EPD 6.0 and without MKL using EPD 5.1 | |
55 tests = {test_eigenvalue : (752., 3376.), | |
56 test_svd : (4608., 15990.), | |
57 test_inv : (418., 1457.), | |
58 test_det : (186.0, 400.), | |
59 test_dot : (666., 2444.) } | |
60 | |
61 # Setting the following environment variable in the shell executing the script allows | |
62 # you limit the maximal number threads used for computation | |
63 THREADS_LIMIT_ENV = 'OMP_NUM_THREADS' | |
64 | |
65 def start_benchmark(): | |
66 print """Benchmark is made against EPD 6.0,NumPy 1.4 with MKL and EPD 5.1, NumPy 1.3 with ATLAS | |
67 on a Thinkpad T60 with Intel CoreDuo 2Ghz CPU on Windows Vista 32 bit | |
68 """ | |
69 if os.environ.has_key(THREADS_LIMIT_ENV): | |
70 print "Maximum number of threads used for computation is : %s" % os.environ[THREADS_LIMIT_ENV] | |
71 print ("-" * 80) | |
72 print "Starting timing with numpy %s\nVersion: %s" % (numpy.__version__, sys.version) | |
73 print "%20s : %10s - %5s / %5s" % ("Function", "Timing [ms]", "MKL", "No MKL") | |
74 for fun, bench in tests.items(): | |
75 t = timeit.Timer(stmt="%s()" % fun.__name__, setup="from __main__ import %s" % fun.__name__) | |
76 res = t.repeat(repeat=3, number=1) | |
77 timing = 1000.0 * sum(res)/len(res) | |
78 print "%20s : %7.1f ms - %3.2f / %3.2f" % (fun.__name__, timing, bench[0]/timing, bench[1]/timing) | |
79 | |
80 if __name__ == '__main__': | |
81 start_benchmark() |