[PyOpenGL-Users] stackoverflow: fastest way to build a c array from a list of tuples of floats?
Brought to you by:
mcfletch
|
From: Jonathan H. <ta...@ta...> - 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
ta...@ta... +44 7737 062 225 twitter/skype: tartley
|