1 Jul 2005 21:11
Numeric.where vs. ctypes-and-C DLL vs PyInline vs weave.inline
Ray Schumacher <rays <at> blue-cove.com>
2005-07-01 19:11:42 GMT
2005-07-01 19:11:42 GMT
In addition to the previous post, I tried PyInline and weave for executing a C routine over a Numeric array.
PyInline has about the same calling overhead as ctypes, apparently, about 240us for a simple C statement.
It also requires some extra hoops to digest the pointer to short I need ( void b2int(short *f, int N) ), so I
didn't fully try it.
weave.inline, however, was very good, with different issues. I found I had to patch msvccompiler.py for MS
C 7.1 .NET, but that's online.
import weave
inline = weave.inline # for speed
N = 1000
code="int i; for(i = 0; i < N; i++){if (dataArray[i]>8191){dataArray[i]-=16383;}}"
inline(code, ['dataArray', 'N']) # just pass Python objects
This created sc_019a1cf36209cb2dfc688820080541ef0.pyd in C:\Documents and Settings\rays\Local Settings\Temp\rays\python24_compiled\
Using the above code is slow; ~32000 us, as the compiler checks or runs each time. However, after much
fiddling and dir()s, I copied the long-name.pyd to C:\Python24\DLLs and just did
import sc_019a1cf36209cb2dfc688820080541ef0
b2iw = sc_019a1cf36209cb2dfc688820080541ef0.compiled_func # note the exposed function!
N = 1
t1=clock()
b2iw({'dataArray':dataArray}, {'N':N}) # note the dicts!
t2 = clock()
print 'weave', round((t2-t1)*1000000, 1), 'us'
25us! (~300us for N=10000) I think that's as close to C speed as I can expect, although I'm looking at the
compiler options in msvccompiler.py for P4 optimization.
I still get "Missing compiler_cxx fix for MSVCCompiler" on the initial compile, but apparently to no harm.
(Continue reading)
RSS Feed