Skip to content

Commit 2450078

Browse files
committed
Add function to calculate size of array
1 parent b644bb9 commit 2450078

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

array_arithmetic.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
print("Configuration")
99
# Python float is 64 bit / 8 byte
1010
ARRAY_SIZE = 1000 * 1000 * 10
11-
print("- Size of data array: {} MB".format(ARRAY_SIZE * 8 / (1000 * 1000)))
11+
size_of_array = ARRAY_SIZE * 8 / (1024 * 1024)
12+
print("- Size of data array: {} MiB".format(size_of_array))
1213

1314
N = 10
1415
print("- Number of iterations to average: {}".format(N))
@@ -19,6 +20,20 @@ def SETUP():
1920
buffer_array = np.empty(ARRAY_SIZE)
2021
return array1, array2, buffer_array
2122

23+
def size_of(array: np.ndarray) -> float:
24+
"""
25+
Calculates the size in memory of a Numpy array
26+
in MiB.
27+
"""
28+
element_dtype = array.dtype.type
29+
30+
element_size = 0
31+
if "32" in str(element_dtype):
32+
element_size = 4 # byte
33+
elif "64" in str(element_dtype):
34+
element_size = 8 # byte
35+
36+
return float( np.prod(array.shape) * element_size / (1024 * 1024) )
2237

2338
if __name__=="__main__":
2439
# gc.disable()
@@ -39,6 +54,7 @@ def SETUP():
3954
average = elapsed_time / N
4055
print("{:45}{:<15.6f}{:<15.6f}".format("Non-contiguous access, vectorization", elapsed_time, average))
4156

57+
print( size_of(array1) + size_of(array2) + size_of(buffer_array) + size_of(index_map) )
4258

4359
array1, array2, buffer_array = SETUP()
4460
index_map = np.arange(0, ARRAY_SIZE)

0 commit comments

Comments
 (0)