~mogurijin/pyeigen/py3k

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import timeit

number = 10
repeat = 3

test_pyeigen = True
test_numpy = True

m1 = None
m2 = None

def test(stmt, setup):
    return timeit.repeat(stmt, setup, repeat=repeat, number=number)

def run_tests():
    global m1, m2

    # Pyeigen
    try:
        import pyeigen
    except ImportError:
        pyeigen = None
    
    if test_pyeigen and pyeigen is not None:
        print "PyEigen"
        m1 = pyeigen.MatrixXf.random(1000, 1000)
        m2 = pyeigen.MatrixXf.random(1000, 1000)
        setup = "from __main__ import m1, m2"
    
        print "Add:", test("m1 + m2", setup) 
        print "Matrix multiply:", test("m1 * m2", setup)
        print "Scalar multiply:", test("m1 * 2.0", setup)
        print

    # Numpy
    try:
        import numpy.matlib
    except ImportError:
        numpy = None
    
    if test_numpy and numpy is not None:
        print "NumPy"
        m1 = numpy.matlib.rand((1000, 1000)).astype(numpy.float32)
        m2 = numpy.matlib.rand((1000, 1000)).astype(numpy.float32)
        setup = "from __main__ import m1, m2"
    
        print "Add:", test("m1 + m2", setup)
        print "Matrix multiply:", test("m1 * m2", setup)
        print "Scalar multiply:", test("m1 * 2.0", setup)
        print

if __name__ == "__main__":
    run_tests()