|
1 | | -# Imports |
| 1 | +####################################################################### |
| 2 | +# Copyright (c) 2019-present, Blosc Development Team <[email protected]> |
| 3 | +# All rights reserved. |
| 4 | +# |
| 5 | +# This source code is licensed under a BSD-style license (found in the |
| 6 | +# LICENSE file in the root directory of this source tree) |
| 7 | +####################################################################### |
| 8 | + |
| 9 | +# Benchmark for computing a slice of a expression in a 4D array. |
| 10 | + |
2 | 11 | import numpy as np |
3 | 12 | import blosc2 |
4 | 13 | import time |
5 | | -from memory_profiler import memory_usage |
| 14 | +from memory_profiler import memory_usage, profile |
6 | 15 | import matplotlib.pyplot as plt |
7 | 16 |
|
8 | 17 | file = "dset-ones.b2nd" |
|
26 | 35 | print(f"Time to create expression: {time.time() - t:.6f} seconds") |
27 | 36 |
|
28 | 37 | # dim0 |
| 38 | +@profile |
29 | 39 | def slice_dim0(): |
30 | 40 | t = time.time() |
31 | 41 | res = expr[1] |
32 | 42 | t0 = time.time() - t |
33 | 43 | print(f"Time to access dim0: {t0:.6f} seconds") |
| 44 | + print(f"dim0 slice size: {np.prod(res.shape) * res.dtype.itemsize / 2**30:.6f} GB") |
34 | 45 |
|
35 | 46 | # dim1 |
| 47 | +@profile |
36 | 48 | def slice_dim1(): |
37 | 49 | t = time.time() |
38 | 50 | res = expr[:,1] |
39 | 51 | t1 = time.time() - t |
40 | 52 | print(f"Time to access dim1: {t1:.6f} seconds") |
| 53 | + print(f"dim1 slice size: {np.prod(res.shape) * res.dtype.itemsize / 2**30:.6f} GB") |
41 | 54 |
|
42 | 55 | # dim2 |
| 56 | +@profile |
43 | 57 | def slice_dim2(): |
44 | 58 | t = time.time() |
45 | 59 | res = expr[:,:,1] |
46 | 60 | t2 = time.time() - t |
47 | 61 | print(f"Time to access dim2: {t2:.6f} seconds") |
| 62 | + print(f"dim2 slice size: {np.prod(res.shape) * res.dtype.itemsize / 2**30:.6f} GB") |
48 | 63 |
|
49 | 64 | # dim3 |
| 65 | +@profile |
50 | 66 | def slice_dim3(): |
51 | 67 | t = time.time() |
52 | 68 | res = expr[:,:,:,1] |
53 | | - #res = expr[1] |
54 | 69 | t3 = time.time() - t |
55 | | - |
56 | 70 | print(f"Time to access dim3: {t3:.6f} seconds") |
| 71 | + print(f"dim3 slice size: {np.prod(res.shape) * res.dtype.itemsize / 2**30:.6f} GB") |
57 | 72 |
|
58 | | -fig = plt.figure() |
59 | | -interval = 0.001 |
60 | | -offset = 0 |
61 | | -for f in [slice_dim0, slice_dim1, slice_dim2, slice_dim3]: |
62 | | - mem = memory_usage((f,), interval=interval) |
63 | | - times = offset + interval * np.arange(len(mem)) |
64 | | - offset = times[-1] |
65 | | - plt.plot(times, mem) |
| 73 | +if __name__ == '__main__': |
| 74 | + interval = 0.001 |
| 75 | + offset = 0 |
| 76 | + for f in [slice_dim0, slice_dim1, slice_dim2, slice_dim3]: |
| 77 | + mem = memory_usage((f,), interval=interval) |
| 78 | + times = offset + interval * np.arange(len(mem)) |
| 79 | + offset = times[-1] |
| 80 | + plt.plot(times, mem) |
66 | 81 |
|
67 | | -plt.xlabel('Time (s)') |
68 | | -plt.ylabel('Memory usage (MiB)') |
69 | | -plt.title('Memory usage over time for slicing operations, slice-expr.py') |
70 | | -plt.legend(['dim0', 'dim1', 'dim2', 'dim3']) |
71 | | -plt.savefig('plots/slice-expr.png', format="png") |
| 82 | + plt.xlabel('Time (s)') |
| 83 | + plt.ylabel('Memory usage (MiB)') |
| 84 | + plt.title('Memory usage lazyexpr slice (fast path), Linux Blosc2 3.5.1') |
| 85 | + plt.legend(['expr[1]', 'expr[:,1]', 'expr[:,:,1]', 'expr[:,:,:,1]']) |
| 86 | + plt.savefig('Linux_Blosc3_5_1_fast.png', format="png") |
0 commit comments