File: bench_structarr.py

package info (click to toggle)
python-scipy 0.10.1%2Bdfsg2-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 42,232 kB
  • sloc: cpp: 224,773; ansic: 103,496; python: 85,210; fortran: 79,130; makefile: 272; sh: 43
file content (43 lines) | stat: -rw-r--r-- 1,118 bytes parent folder | download
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
from __future__ import division
from numpy.testing import *

from cStringIO import StringIO

import numpy as np
import scipy.io as sio


def make_structarr(n_vars, n_fields, n_structs):
    var_dict = {}
    for vno in range(n_vars):
        vname = 'var%00d' % vno
        end_dtype = [('f%d' % d, 'i4', 10) for d in range(n_fields)]
        s_arrs = np.zeros((n_structs,), dtype=end_dtype)
        var_dict[vname] = s_arrs
    return var_dict


def bench_run():
    str_io = StringIO()
    print
    print 'Read / writing matlab structs'
    print '='*60
    print ' write |  read |   vars | fields | structs '
    print '-'*60
    print
    for n_vars, n_fields, n_structs in (
        (10, 10, 20),):
        var_dict = make_structarr(n_vars, n_fields, n_structs)
        str_io = StringIO()
        write_time = measure('sio.savemat(str_io, var_dict)')
        read_time = measure('sio.loadmat(str_io)')
        print '%.5f | %.5f | %5d | %5d | %5d ' % (
            write_time,
            read_time,
            n_vars,
            n_fields,
            n_structs)


if __name__ == '__main__' :
    bench_run()