Type Sizes
Once you know how to size the 4 basic types, you can size any type in Numstore. If you haven't yet, read the type system first - it walks through each rule.
The size rules
| Type | Size |
|---|---|
Primitive |
bits / 8 |
Struct |
sum of member sizes |
Union |
max of member sizes |
Strict Array |
product of dimensions x element size |
Practice problems
ns.to_dtype(...).itemsize will check your
answers.
- What is the element size of
struct { a u16, b i64, c f32 }? - What is the element size of
union { a u16, b i64, c f32 }? - What is the element size of
[10][20] u32? - What is the element size of
[100] struct { x f32, y f32, z f32 }? - What is the element size of
struct { id u64, samples [8] cf64 }? - What is the element size of
[12][256][256] struct { r u8, g u8, b u8 }? - In a
[4][4] f32, what byte offset is element[2][3]? - What is the element size of
struct { tag u8, payload union { a f64, b [2] u32 } }?
Answers
2 + 8 + 4 = 14bytes.max(2, 8, 4) = 8bytes.10 x 20 x 4 = 800bytes.- struct is
12, so100 x 12 = 1,200bytes. cf64is 8 bytes ->samplesis8 x 8 = 64, plusidat 8 ->72bytes.- struct is
3, array is12 x 256 x 256 = 786,432->2,359,296bytes. (2 x 4 + 3) x 4 = 11 x 4 = 44bytes.unionismax(8, 2 x 4) = 8, plustagat 1 ->9bytes.
Check the answers
>>> import pynumstore as ns
>>> ns.to_dtype("struct { a u16, b i64, c f32 }").itemsize
14
>>> ns.to_dtype("union { a u16, b i64, c f32 }").itemsize
8
>>> ns.to_dtype("[10][20] u32").itemsize
800
>>> ns.to_dtype("[100] struct { x f32, y f32, z f32 }").itemsize
1200
>>> ns.to_dtype("struct { id u64, samples [8] cf64 }").itemsize
72
>>> ns.to_dtype("[12][256][256] struct { r u8, g u8, b u8 }").itemsize
2359296
>>> ns.to_dtype("struct { tag u8, payload union { a f64, b [2] u32 } }").itemsize
9