3 ms·
> How exactly would you avoid the overhead of individual instances using a ctypes structure? This optimization only works if you have an upper bound for the to
by acdha 13y ago
> How exactly would you avoid the overhead of individual instances using a ctypes structure?
This optimization only works if you have an upper bound for the total number of items but if so I'd at least try something like declaring point_x and point_y arrays so you'd have two Python objects accessed by indexes rather than many Point references. This technique can be particularly useful if you don't access all of the elements at the same time and you can get more efficient memory access patterns because you can access a subset of the elements in nice linear reads.
> If you really want to be serious about large, performance-intensive arrays in Python, it's time to pull out numpy. It actually would fit well here.
Very strong agreement - numpy is a treasure.
> As a result, I find ctypes a lot easier to use for simple optimizations. YMMV.
Ditto - I've found cffi to be faster in some cases but it's definitely more overhead to use.
- jnbiche 13y agoAh ok, so you're proposing a nested array, not an array of structure pointers. I thought you were saying there was some way to use ctypes structures and yet avoid the overhead of the class instances. And yes, I agree that if you're going to create this type of data structure, then it's worth the trouble to pull out numpy if you can.