File: benchmarks.py

package info (click to toggle)
h5py 3.16.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,828 kB
  • sloc: python: 12,212; ansic: 578; makefile: 433; sh: 33
file content (57 lines) | stat: -rw-r--r-- 1,669 bytes parent folder | download | duplicates (4)
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
# Write the benchmarking functions here.
# See "Writing benchmarks" in the asv docs for more information.
import os.path as osp
import numpy as np
from tempfile import TemporaryDirectory
import h5py

class TimeSuite:
    """
    An example benchmark that times the performance of various kinds
    of iterating over dictionaries in Python.
    """
    def setup(self):
        self._td = TemporaryDirectory()
        path = osp.join(self._td.name, 'test.h5')
        with h5py.File(path, 'w') as f:
            f['a'] = np.arange(100000)

        self.f = h5py.File(path, 'r')

    def teardown(self):
        self.f.close()
        self._td.cleanup()

    def time_many_small_reads(self):
        ds = self.f['a']
        for i in range(10000):
            arr = ds[i * 10:(i + 1) * 10]

class WritingTimeSuite:
    """Based on example in GitHub issue 492:
    https://github.com/h5py/h5py/issues/492
    """
    def setup(self):
        self._td = TemporaryDirectory()
        path = osp.join(self._td.name, 'test.h5')
        self.f = h5py.File(path, 'w')
        self.shape = shape = (128, 1024, 512)
        self.f.create_dataset(
            'a', shape=shape, dtype=np.float32, chunks=(1, shape[1], 64)
        )

    def teardown(self):
        self.f.close()
        self._td.cleanup()

    def time_write_index_last_axis(self):
        ds = self.f['a']
        data = np.zeros(self.shape[:2])
        for i in range(self.shape[2]):
            ds[..., i] = data

    def time_write_slice_last_axis(self):
        ds = self.f['a']
        data = np.zeros(self.shape[:2])
        for i in range(self.shape[2]):
            ds[..., i:i+1] = data[..., np.newaxis]