前文咱們已經說過了如何用freebasic編制dll被python調用,本節是經過示例來對比其運行效率。python
廢話不說,上代碼:ui
myfib.bascode
編譯後生成myfib.dllci
fbc -s gui -dll -export "myfib.bas"input
'mylib.bas function fib Cdecl Alias "fib"( x as integer) as Integer Export if x<=1 then return 1 else return fib(x-1) + fib(x-2) end if end function
編譯.dll,命名爲myfib.dll,拷貝到python.exe 目錄下it
2. python 代碼:io
# -*- coding: cp936 -*- from ctypes import * lib=CDLL("myfib.dll") # Fibonacci in Python def fibpy(x): if x<=1: return 1 return fibpy(x-1)+fibpy(x-2) # test fibpy(x) def sspy(x): import time t0=time.time() fibpy(x) t1=time.time() print "Python coast time:",(t1-t0) # test dll of freebasic: fib(40) def ssfb(x): import time t0=time.time() lib.fib(x) t1=time.time() print "coast time:",(t1-t0) print "1.=================" print "test for python function fibpy(40)" sspy(40) print "" print "2.=================" print "test for dll of freebasic fib(40)" ssfb(40) i=raw_input("input anything for Exit")
生成的結果你們都關心的哈,以下:編譯
對比一下,提升26倍哦。ast
轉帖請註明出處。function