## Re: [PyOpenGL-Users] stackoverflow: fastest way to build a c array from a list of tuples of floats?

 Re: [PyOpenGL-Users] stackoverflow: fastest way to build a c array from a list of tuples of floats? From: Jonathan Hartley - 2010-11-19 01:20:20 ```On 12/11/2010 16:29, Christopher Barker wrote: > On 11/11/10 11:48 PM, Jonathan Hartley wrote: >> What is the fastest way in python to build a c array from a list of >> tuples of floats? The context: my Python code pass arrays of 2D vertices >> to OpenGL. > > This seems like an obvious use for numpy: > >> points = [(random(), random()) for _ in xrange(1000)] > > def array_numpy(points): > return np.array(points, dtype=float32) > > And you can save the overhead of the function call, since it's a > one-liner anyway. Of course, if you're using numpy, you probably never > would have had all those points in tuples in the first place -- you'd > just use numpy arrays from the start. Thanks for this. I'm trying out the numpy approach now. Jonathan -- Jonathan Hartley Made of meat. http://tartley.com tartley@... +44 7737 062 225 twitter/skype: tartley ```

 [PyOpenGL-Users] stackoverflow: fastest way to build a c array from a list of tuples of floats? From: Jonathan Hartley - 2010-11-12 07:55:21 ```There's an interesting question on StackOverflow today that I suspect a lot of people would like to see an optimal answer to: ----- What is the fastest way in python to build a c array from a list of tuples of floats? The context: my Python code pass arrays of 2D vertices to OpenGL. I tested 2 approaches, one with ctypes, the other with struct, the latter being more than twice faster. from random import random points = [(random(), random()) for _ in xrange(1000)] from ctypes import c_float def array_ctypes(points): n = len(points) return n, (c_float*(2*n))(*[u for point in points for u in point]) from struct import pack def array_struct(points): n = len(points) return n, pack("f"*2*n, *[u for point in points for u in point]) Any other alternatives? ----- http://stackoverflow.com/q/4156872/10176 He's already chosen one answer, but I think that was premature, and some of you folks might have substantially better ideas. I'm thinking of a fully static-typed Cython function, but I apparently lack the smarts to get it working over breakfast, and now I have to go to work. Jonathan -- Jonathan Hartley Made of meat. http://tartley.com tartley@... +44 7737 062 225 twitter/skype: tartley ```
 Re: [PyOpenGL-Users] stackoverflow: fastest way to build a c array from a list of tuples of floats? From: Christopher Barker - 2010-11-12 16:29:30 ```On 11/11/10 11:48 PM, Jonathan Hartley wrote: > What is the fastest way in python to build a c array from a list of > tuples of floats? The context: my Python code pass arrays of 2D vertices > to OpenGL. This seems like an obvious use for numpy: > points = [(random(), random()) for _ in xrange(1000)] def array_numpy(points): return np.array(points, dtype=float32) And you can save the overhead of the function call, since it's a one-liner anyway. Of course, if you're using numpy, you probably never would have had all those points in tuples in the first place -- you'd just use numpy arrays from the start. > Any other alternatives? There's also array.array: def array_array(points): a = array.array('f') [a.extend(p) for p in points] return a Which brings up a question I have: is there way to write a "nothing comprehension"? -- I use the list comprehension syntax, as it's a nice clean way to process all the items in a list -- but I don't need the resulting list -- it seems there should be a way to write that without the overhead of creating a useless list. -Chris > ----- > http://stackoverflow.com/q/4156872/10176 > > > He's already chosen one answer, but I think that was premature, and some > of you folks might have substantially better ideas. I'm thinking of a > fully static-typed Cython function, but I apparently lack the smarts to > get it working over breakfast, and now I have to go to work. > > Jonathan > -- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chris.Barker@... ```
 Re: [PyOpenGL-Users] stackoverflow: fastest way to build a c array from a list of tuples of floats? From: Toby Dickenson - 2010-11-12 17:15:44 ```On Friday 12 Nov 2010, Christopher Barker wrote: > def array_array(points): > a = array.array('f') > [a.extend(p) for p in points] > return a > > Which brings up a question I have: is there way to write a "nothing > comprehension"? for p in points: a.extend(p) -- Toby Dickenson ```
 Re: [PyOpenGL-Users] stackoverflow: fastest way to build a c array from a list of tuples of floats? From: Jonathan Hartley - 2010-11-19 01:20:20 ```On 12/11/2010 16:29, Christopher Barker wrote: > On 11/11/10 11:48 PM, Jonathan Hartley wrote: >> What is the fastest way in python to build a c array from a list of >> tuples of floats? The context: my Python code pass arrays of 2D vertices >> to OpenGL. > > This seems like an obvious use for numpy: > >> points = [(random(), random()) for _ in xrange(1000)] > > def array_numpy(points): > return np.array(points, dtype=float32) > > And you can save the overhead of the function call, since it's a > one-liner anyway. Of course, if you're using numpy, you probably never > would have had all those points in tuples in the first place -- you'd > just use numpy arrays from the start. Thanks for this. I'm trying out the numpy approach now. Jonathan -- Jonathan Hartley Made of meat. http://tartley.com tartley@... +44 7737 062 225 twitter/skype: tartley ```