-
Notifications
You must be signed in to change notification settings - Fork 52
/
speed_check.py
83 lines (63 loc) · 2.43 KB
/
speed_check.py
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# -*- coding: utf-8 -*-
# Copyright (c) oct2py developers.
# Distributed under the terms of the MIT License.
from __future__ import print_function, absolute_import
import time
import timeit
import numpy as np
from . import Oct2Py
class SpeedCheck(object):
"""Checks the speed penalty of the Python to Octave bridge.
Uses timeit to test the raw execution of a Octave command,
Then tests progressively larger array passing.
"""
def __init__(self):
"""Create our Octave instance and initialize the data array
"""
self.octave = Oct2Py()
self.array = []
def raw_speed(self):
"""Run a fast Octave command and see how long it takes.
"""
self.octave.eval("x = 1")
def large_array_put(self):
"""Create a large matrix and load it into the octave session.
"""
self.octave.push('x', self.array)
def large_array_get(self):
"""Retrieve the large matrix from the octave session
"""
self.octave.pull('x')
def run(self):
"""Perform the Oct2Py speed analysis.
Uses timeit to test the raw execution of an Octave command,
Then tests progressively larger array passing.
"""
print('Oct2Py speed test')
print('*' * 20)
time.sleep(1)
print('Raw speed: ')
avg = timeit.timeit(self.raw_speed, number=10) / 10
print(' {0:0.01f} usec per loop'.format(avg * 1e6))
sides = [1, 10, 100, 1000]
runs = [10, 10, 10, 5]
for (side, nruns) in zip(sides, runs):
self.array = np.reshape(np.arange(side ** 2), (-1))
print('Put {0}x{1}: '.format(side, side))
avg = timeit.timeit(self.large_array_put, number=nruns) / nruns
print(' {0:0.01f} msec'.format(avg * 1e3))
print('Get {0}x{1}: '.format(side, side))
avg = timeit.timeit(self.large_array_get, number=nruns) / nruns
print(' {0:0.01f} msec'.format(avg * 1e3))
self.octave.exit()
print('*' * 20)
print('Test complete!')
def speed_check():
"""Checks the speed penalty of the Python to Octave bridge.
Uses timeit to test the raw execution of a Octave command,
Then tests progressively larger array passing.
"""
test = SpeedCheck()
test.run()
if __name__ == '__main__':
speed_check()