File: read_array_out_arg.py

package info (click to toggle)
pytables 3.10.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,228 kB
  • sloc: ansic: 82,212; python: 65,296; cpp: 753; sh: 394; makefile: 100
file content (57 lines) | stat: -rw-r--r-- 1,674 bytes parent folder | download | duplicates (2)
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
# This script compares reading from an array in a loop using the
# tables.Array.read method.  In the first case, read is used without supplying
# an 'out' argument, which causes a new output buffer to be pre-allocated
# with each call.  In the second case, the buffer is created once, and then
# reused.

from time import perf_counter as clock

import numpy as np

import tables as tb


def create_file(array_size):
    array = np.ones(array_size, dtype="i8")
    with tb.open_file("test.h5", "w") as fobj:
        array = fobj.create_array("/", "test", array)
        print(f"file created, size: {array.size_on_disk / 1e6} MB")


def standard_read(array_size):
    n = 10
    with tb.open_file("test.h5", "r") as fobj:
        array = fobj.get_node("/", "test")
        start = clock()
        for i in range(n):
            output = array.read(0, array_size, 1)
        end = clock()
        assert np.all(output == 1)
        print(f"standard read   \t {(end - start) / n:5.5f}")


def pre_allocated_read(array_size):
    n = 10
    with tb.open_file("test.h5", "r") as fobj:
        array = fobj.get_node("/", "test")
        start = clock()
        output = np.empty(array_size, "i8")
        for i in range(n):
            array.read(0, array_size, 1, out=output)
        end = clock()
        assert np.all(output == 1)
        print(f"pre-allocated read\t {(end - start) / n:5.5f}")


if __name__ == "__main__":

    array_num_bytes = [10**5, 10**6, 10**7, 10**8]

    for array_bytes in array_num_bytes:

        array_size = array_bytes // 8

        create_file(array_size)
        standard_read(array_size)
        pre_allocated_read(array_size)
        print()