File: bench_convolve2d.py

package info (click to toggle)
python-scipy 0.14.0-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 52,228 kB
  • ctags: 63,719
  • sloc: python: 112,726; fortran: 88,685; cpp: 86,979; ansic: 85,860; makefile: 530; sh: 236
file content (48 lines) | stat: -rw-r--r-- 1,357 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
44
45
46
47
48
"""
Check the speed of 2d convolution.

"""
from __future__ import division, print_function, absolute_import

from itertools import product
import time

import numpy as np
from numpy.testing import Tester, TestCase, assert_allclose

from scipy.signal import convolve2d, correlate2d


def bench_convolve2d():
    np.random.seed(1234)

    # sample a bunch of pairs of 2d arrays
    tm_start = time.clock()
    pairs = []
    for ma, na, mb, nb in product((1, 2, 8, 13, 30), repeat=4):
        a = np.random.randn(ma, na)
        b = np.random.randn(mb, nb)
        pairs.append((a, b))
    tm_end = time.clock()
    tm_total = tm_end - tm_start
    print('time to sample random pairs of 2d arrays:')
    print(tm_total)
    print()

    fns = (convolve2d, correlate2d)
    modes = ('full', 'valid', 'same')
    boundaries = ('fill', 'wrap', 'symm')

    # compute 2d convolutions and correlations for each 2d array pair
    tm_start = time.clock()
    for a, b in pairs:
        for fn, mode, boundary in product(fns, modes, boundaries):
            if mode == 'valid':
                if b.shape[0] > a.shape[0] or b.shape[1] > a.shape[1]:
                    continue
            fn(a, b, mode=mode, boundary=boundary)
    tm_end = time.clock()
    tm_total = tm_end - tm_start
    print('time to compute 2d convolutions:')
    print(tm_total)
    print()