- 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
- 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
- 4、该文档为VIP文档,如果想要下载,成为VIP会员后,下载免费。
- 5、成为VIP后,下载本文档将扣除1次下载权益。下载后,不支持退款、换文档。如有疑问请联系我们。
- 6、成为VIP后,您将拥有八大权益,权益包括:VIP文档下载权益、阅读免打扰、文档格式转换、高级专利检索、专属身份标志、高级客服、多端互通、版权登记。
- 7、VIP文档为合作方或网友上传,每下载1次, 网站将根据用户上传文档的质量评分、类型等,对文档贡献者给予高额补贴、流量扶持。如果你也想贡献VIP文档。上传文档
查看更多
Word格式
完美整理
python中嵌入C语言脚本
借助Cinpy和C语言解释器TinyCC,可以在python程序里面直接嵌入C语言片断、不经编译直接使用C编写的函数了。
win2k平台上,简单的测试对比数据如下(递归方法计算第四十项兔子数列fib(40))语言
实现
时间
(单位:秒)
评
python
官方python 2.4.3
纯python fib函数
568.718
天啊
使用psyco加速的python fib函数
17.922
比较接近,还行
使用swig直接转换的C语言编写的模块
13.453
使用Cinpy嵌入fib函数
11.532
C
VC6
速度优化编译的可执行文件
5.562
TinyCC 0.9.23
编译的可执行文件
6.719
解释执行
6.813
FreeBASIC
fbc 0.16b
编译的可执行文件(-arch 486)
8.022
编译的可执行文件(-arch 686)
7.619
forth
4th 3.5a2
4th cx fib.4th
277
这个表现太失望了
4th csv fib.4th fib.hx
4th lx fib.hx
196
4th lg fib.hx fib.c
mingw -O2 fib.c -o fib.exe
110
gforth-0.6.2
Gforth-fast fib.gfth
14.719
不错,不过不是说和C的速度可以比嘛?
怎么也就是优化的python的速度啊
注:
其余源程序
freebasic function fib(x as integer) as integer
if xlt;=1 then
return 1
else
return fib(x-1) + fib(x-2)
end if
end functiondim starttime, endtime as double
dim res as integerstarttime=timer
res=fib(40)
endtime=timer
print fib(40)= ;res
print time elapsed: ; (endtime-starttime); s
4th : fib ( x -- y )
dup 2 gt; if
dup 1 - recurse
swap 2 - recurse +
exit
then
drop 1 ;time41 fib . crtime
swap -
. time elapsed . . s cr
gforth-0.6.2 : fib ( x -- y )
dup 2 gt; if
dup 1 - recurse
swap 2 - recurse +
exit
then
drop 1 ;
utime41 fib . crutime
2swap d-
. time elapsed d. . us cr
如果在windows下使用mingw编译当前的TinyCC,嵌入C脚本会报错:
tcc: file /c/Program Files/tcc/libtcc1.a not found
Doug Currie在tcc的邮件列表里面提供了一个补丁:
Here is what I did (and reported to the mailing list) last February,
so the patch may not be accurate for more recent versions, but the
issues are the same...
I have been able to create a libtcc.dll for WinXP using MinGW/MSYS;
the changes that were necessary were very minor. Perhaps this
description will help others use libtcc on Windows.
First, a small bug to report:
In tcc.c the function tcc_basename() follows the line:
#if !defined(LIBTCC)
but the function tcc_basename() is used in pe_build_exports() in
tccpe.c -- moving the #if
原创力文档


文档评论(0)