[PyOpenGL-Users] Using lists vs. arrays with PyOpenGL (was: Re: PyOpenGL glVertexPointer trouble)
Brought to you by:
mcfletch
From: Mike C. F. <mcf...@ro...> - 2003-05-10 02:32:53
|
The...@ao... wrote: ... >everything is working fine, but >brings up a related question: Experimentally, I didn't use Numeric arrays >when passing vertices/indices to GL, I used python lists; is there an >inherant speed boost when using arrays? > > (copied to the list in case others have the same question) There is a speed boost using arrays vs. lists. If you create a contiguous Numeric python array of the correct data type (i.e. f, d, i, whatever), then PyOpenGL is able to do a very simple struct-member access on the array object to get the internal contiguous array pointer and can pass that directly to the OpenGL function. In all other situations, PyOpenGL will have to copy the data from the source object (list or array) into a newly-allocated Numeric Python array, pass the internal pointer to the OpenGL function, and (at some point in time) de-allocate the newly allocated Numeric Python array. I haven't tested, but I would expect that an array of a different type would be faster than the list, given the ability to assume data type for the array items (doesn't require any interaction with the individual objects to convert the values). Contiguous arrays are generated on array creation, and generally arrays are contiguous unless you are growing the array piecewise by appending sections. The data type of an array must match exactly to avoid copying (i.e. an array of type "f" will have to be copied to be passed to an OpenGL "d" function). You will find a "contiguous" function in the OpenGL.GL module which can be used to ensure that a given array is a contiguous numeric python array of a specified type (you would normally cache a copy after calling contiguous on the array). BTW: Although I am the author of that function, however, I've never actually used it in production code that I can recall, it was provided for a user who was experiencing problems in this area. With all that said, for many situations, the issue is irrelevant. All the code described above is at the C level, so unless you are dealing with very large arrays/lists, it's quite likely the copying effects would be entirely swamped by the overhead of using Python in the first place. Not to say you should waste the cycles for no reason, but until you get into large arrays, it's not likely going to be a concern. Hope that helps, Mike _______________________________________ Mike C. Fletcher Designer, VR Plumber, Coder http://members.rogers.com/mcfletch/ |