python - What are the pros and cons of using a structured array in Numpy instead of several arrays? -


i use numpy gather objects same properties in efficient way. don't know choose between using 1 structured array or several arrays.

for example, let's consider object item , properties id (4 byte unsigned integer), name (20 unicode characters), price (4 byte float).

using structured array:

import numpy np item_dtype = np.dtype([('id', 'u4'), ('name', 'u20'), ('price', 'f4')])  # populate: raw_items = [(12, 'bike', 180.54), (33, 'helmet', 46.99)] my_items_a = np.array(raw_items, dtype=item_dtype)  # access: my_items_a[0] # first item my_items_a['price'][1] # price of second item 

using several arrays, wrapped in class convience:

class items:     def __init__(self, raw_items):        n = len(raw_items)         id, name, price = zip(*raw_items)         self.id = np.array(id, dtype='u4')        self.name = np.array(name, dtype='u20')        self.price = np.array(price, dtype='f4')  # populate: my_items_b = items(raw_items)  # access: (my_items_b.id[0], my_items_b.name[0], my_items_b.price[0]) # first item my_items_b.price[1] # price of second item 

what pros , cons of these 2 approaches? when using 1 instead of other? thanks

at least 1 consideration of locality of reference.

in general, it's idea structure memory layout when access memory location, there's chance you'll access locations right near by. increase cache performance.

consequently, regardless of logical meaning of data:

  • if have many operations calculate on fields of 1 record, fields of next record, etc., might consider records.

  • if have many operations calculate on single field entries, else different field entries, etc., might consider several arrays.

asides this, there's question of code clarity , ease of maintenance, it's not hard , fast rule. also, in general, ymmv, should profile , instrument different options.


Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -