<br><br><div><span class="gmail_quote">On 8/8/07, <b class="gmail_sendername">Anne Archibald</b> <<a href="mailto:email@example.com">firstname.lastname@example.org</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
On 08/08/2007, Stefan van der Walt <<a href="mailto:email@example.com">firstname.lastname@example.org</a>> wrote:<br>> On Tue, Aug 07, 2007 at 01:33:24AM -0400, Anne Archibald wrote:<br>> > Well, it can be done in Python: just allocate a too-big ndarray and
<br>> > take a slice that's the right shape and has the right alignment. But<br>> > this sucks.<br>><br>> Could you explain to me why is this such a bad idea?<br><br>Oh. Well, it's not *terrible*; it gets you an aligned array. But you
<br>have to allocate the original array as a 1D byte array (to allow for<br>arbitrary realignments) and then align it, reshape it, and reinterpret<br>it as a new type. Plus you're allocating an extra ndarray structure,
<br>which will live as long as the new array does; this not only wastes<br>even more memory than the portable alignment solutions, it clogs up<br>python's garbage collector.</blockquote><div><br>The ndarray structure doesn't take up much memory, it is the data that is large and the data is shared between the original array and the slice. Nor does the data type of the slice need changing, one simply uses the desired type to begin with, or at least a type of the right size so that a view will do the job without copies. Nor do I see how the garbage collector will get clogged up, slices are a common feature of using numpy. The slice method also has the advantage of being compiler and operating system independent, there is a reason Intel used that approach.
<br><br>Aligning multidimensional arrays might indeed be complicated, but I suspect those complications will be easier to handle in Python than in C.<br><br>Chuck<br></div></div><br>