| 1 |
''' |
| 2 |
Some simple benchmarks to check how much the performance is impaired when using units (units are not used in the main |
| 3 |
simulation loop but also analysing/plotting should not be noticeably slow for the user) |
| 4 |
''' |
| 5 |
import time |
| 6 |
|
| 7 |
import numpy as np |
| 8 |
|
| 9 |
from brian2 import * |
| 10 |
|
| 11 |
sizes = [10, 100, 1000, 10000, 100000] |
| 12 |
|
| 13 |
access = lambda idx, ar1, ar2: ar1[idx] |
| 14 |
multiply = lambda idx, ar1, ar2: 42 * ar1 |
| 15 |
addition = lambda idx, ar1, ar2: ar1 + ar2 |
| 16 |
slicing = lambda idx, ar1, ar2: ar1[idx:] |
| 17 |
|
| 18 |
for func_name, func in [('access', access), |
| 19 |
('multiply', multiply), |
| 20 |
('add', addition), |
| 21 |
('slicing', slicing)]: |
| 22 |
times_no_unit = [] |
| 23 |
times_unit = [] |
| 24 |
for size in sizes: |
| 25 |
no_unit1 = np.random.randn(size) |
| 26 |
no_unit2 = np.random.randn(size) |
| 27 |
with_unit1 = no_unit1 * mV |
| 28 |
with_unit2 = no_unit2 * mV |
| 29 |
start = time.time() |
| 30 |
for x in range(size): |
| 31 |
func(x, no_unit1, no_unit2) |
| 32 |
times_no_unit.append(time.time() - start) |
| 33 |
start = time.time() |
| 34 |
for x in range(size): |
| 35 |
func(x, with_unit1, with_unit2) |
| 36 |
times_unit.append(time.time() - start) |
| 37 |
print('') |
| 38 |
print(func_name, ':') |
| 39 |
print('No unit ', times_no_unit) |
| 40 |
print(' unit ', times_unit) |
| 41 |
print('relative', np.array(times_unit) / np.array(times_no_unit)) |
| 42 |
|
| 43 |
# Dimensionless Quantities |
| 44 |
print('*' * 60) |
| 45 |
print('Dimensionless quantities') |
| 46 |
|
| 47 |
for func_name, func in [('access', access), |
| 48 |
('multiply', multiply), |
| 49 |
('add', addition), |
| 50 |
('slicing', slicing)]: |
| 51 |
times_no_unit = [] |
| 52 |
times_unit = [] |
| 53 |
for size in sizes: |
| 54 |
no_unit1 = np.random.randn(size) |
| 55 |
no_unit2 = np.random.randn(size) |
| 56 |
with_unit1 = no_unit1 * mV/mV |
| 57 |
with_unit2 = no_unit2 * mV/mV |
| 58 |
start = time.time() |
| 59 |
for x in range(size): |
| 60 |
func(x, no_unit1, no_unit2) |
| 61 |
times_no_unit.append(time.time() - start) |
| 62 |
start = time.time() |
| 63 |
for x in range(size): |
| 64 |
func(x, with_unit1, with_unit2) |
| 65 |
times_unit.append(time.time() - start) |
| 66 |
print('') |
| 67 |
print(func_name, ':') |
| 68 |
print('No unit ', times_no_unit) |
| 69 |
print(' unit ', times_unit) |
| 70 |
print('relative', np.array(times_unit) / np.array(times_no_unit)) |