Re: [Pyobjc-dev] conversion of tuples to structs
Brought to you by:
ronaldoussoren
From: b.bum <bb...@ma...> - 2003-09-12 20:39:24
|
On Sep 11, 2003, at 5:36 PM, Tobias Sargeant wrote: > I appreciate that the symmetry of the pythonifying/depythonifying is > neat, but it would be extremely useful (for example) to be able to pass > Numeric arrays representing points to objc functions without the > overhead > (both in terms of execution speed and in terms of typing) of converting > them to tuples. (You'll need to do a cvs update to grab a version of PyObjC that can support what is described below) Over Christmas, I did some graphics performance tests and found that the fastest possible way to get points across the bridge and onto the screen was through the use of something like.... points = array.array('f', [0.0,0.0,1.0,1.0] * colorIter) That is, allocate points as an array of type float, initialized to contain the sequence 0.0,0.0,1.0,1.0 repeated colorIter times. Then, update the contents as needed and invoke.... NSRectFillList(points, colorIter) ... to draw. This is possible because NSRectFillList() is bridged in a fashion that accepts both tuples and the array.array() construct. The array.array() construct is very fast mostly because it can be passed directly to NSRectFillList() without any conversion. Except that the current version of the bridge breaks this with the comment: /* Upto PyObjC 1.0b2 compat_NSRectFillList was the wrapper for NSRectFillList * this implementation is not consistant w.r.t. the rest of the bridge and * therefore depricated. */ Which is really unfortunate because the new version has to allocate a chunk of memory, then loop through every tuple and turn it into an array of floats -- this really adds up when pumping through 100s of thousands of rectangles. Given that 1x1 rectangles are the most efficient way of pushing non-anti-aliased points or lines to the screen, this situation is common in graphic intensive Cocoa apps. Some numbers: I wrote a little app that implements the Hopalong algorithm to draw lots of pretty dots on the screen. Using the Tuple based conversion, I see.... 2003-09-12 13:26:22.303 Hop[2294] Iterated 104300 points in 8.54 seconds. 2003-09-12 13:26:30.767 Hop[2294] Iterated 104300 points in 8.45 seconds. 2003-09-12 13:26:39.306 Hop[2294] Iterated 105700 points in 8.52 seconds. 2003-09-12 13:26:49.118 Hop[2294] Iterated 112900 points in 9.80 seconds. 2003-09-12 13:26:57.351 Hop[2294] Iterated 108200 points in 8.22 seconds. ... whereas the array.array('f', ...) method described above yields.... 2003-09-12 13:32:07.441 Hop[2309] Iterated 263200 points in 4.28 seconds. 2003-09-12 13:32:11.422 Hop[2309] Iterated 283600 points in 3.96 seconds. 2003-09-12 13:32:15.227 Hop[2309] Iterated 270000 points in 3.74 seconds. 2003-09-12 13:32:19.042 Hop[2309] Iterated 275600 points in 3.80 seconds. 2003-09-12 13:32:23.577 Hop[2309] Iterated 273600 points in 4.52 seconds. ... which is about 6x faster. The same carries through to the rest of the APIs that take (NSPoint **) or (NSRect **) type arrays of points/rects/etc... b.bum |