Here is the code from a local discussion group
# foo.nim
import nimpy
proc foo(): seq[array[3,int]]{.exportpy.}=
var a:seq[array[3,int]]
for i in 1..10000000:
a.add([i,i*2,0])
return a
and
# a.py
import time, foo
n=time.time()
that=foo.foo()
print(time.time()-n)
old=time.time()
def test(num):
a=[]
for i in range(num):
temp=[i,i*2,0]
a.append(temp)
return a
oh=test(10000000)
print(time.time()-old)
when I compile the foo.nim with latest-cloned-and-built nim on Win7 64bits( Nim Compiler Version 0.20.99 [Windows: amd64] Compiled at 2019-06-10) in MSYS2+MINGW with nim c -d:release --threads:on --app:lib --out:foo.pyd foo then
R:\>e:\prg\py\Anaconda3_64\python.exe a.py
8.30347490310669
10.290588617324829
R:\>r:\python-2.7.16.amd64\python.exe a.py
13.0490000248
21.8800001144
shows that nimpy runs faster than pure python which is expected
however if I compile the python module with nim c -d:release --threads:on --opt:speed --app:lib --out:foo.pyd foo then
R:\>e:\prg\py\Anaconda3_64\python.exe a.py
17.50800108909607
17.381994247436523
R:\>r:\python-2.7.16.amd64\python.exe a.py
17.4839999676
15.9549999237
it is funny that --opt:speed slow down the code! What is the problem?
question three, as we can find that both python 2 and 3 can use the generated pyd files. So the pyd is not a normal python extension which depends on the python version. How does it come? Will this feature slow down the extension?
Thanks
Try newSeqOfCap for speed.
I dont think you need --threads:on.
You are not using set on the code.
Being pure math that can be a func instead of proc but thats really just style.
Nimpy wont care about Python version, as long as is C API compatible it will work.
AFAIK.