Thanks, Paul, for explain why to calculate fib in this way is bad.

I try to use pycxx, because I thought it is a tool to write py extension. And as for extension, before this example, I always thought it is used to speed the py source.

 

I tried some tools, and got a table. In this table, the speed is calculated in second.

As we can see:

1. no matter cython, swig, pyinline or shedskin, they compiled C/C++ source into PYD. and all the PYDs give a great increase in speed 

2. pycxx compiled C++ source into PYD too, but this PYD is even slower than a pure python function heavily!

so from this example, I conclude that pycxx is only for the aim "to write python exteison in C++ easily", but not for the aim "to speed up the original pure python program". Am I right?

 

M  | pure py |cython | swig  | pycxx  | cinpy | pyinline | psyco | shedskin

---+----------+---------+------+---------+--------+---------+--------+-----------

40 | 116.64  |   3.70   | 5.40  | 789.81 |  4.00   |  4.66    |  6.22   |  3.50

30 |   0.93   |  0.03    | 0.03  |   6.35   |   0.03  |  0.03    |  0.05   |  0.03

 

I don't want to list all the source files I used here, because they are all recursion function, and have the almost same and simple source code structure as the following codes.

 

[pure py]

def fib(n):
    if n<=1:
        return 1
    else:
        return fib(n-1)+fib(n-2)

M=30
st=time.time()
for i in range(M):
    fib(i)
et=time.time()
print fib(M)
print 'time: %.2f secs' % (et-st)

[/pure py]

 

[test_pyinline]

import time
import PyInline, __main__

m = PyInline.build(code="""
    long fib(int a) {
        if (a<2)   return 1;
        return fib(a-2)+fib(a-1);
  }""",
  targetmodule=__main__, language="C")

M=30
st=time.time()
for i in range(M):
    fib(i)
et=time.time()
print fib(M)
print 'time: %.2f secs' % (et-st)

[/test_pyinline]