python如何内嵌c语言,python中嵌入C语言脚本

《python中嵌入C语言脚本》由会员分享,可在线阅读,更多相关《python中嵌入C语言脚本(16页珍藏版)》请在人人文库网上搜索。

1、WORD格式-可编辑-python中嵌入C语言脚本借助Cinpy和C语言解释器TinyCC ,可以在python程序里面直接 嵌入C语言片断、不经编译直接使用C编写的函数了。wi n2k平台上,简单的测试对比数据如下(递归方法计算第四十项兔子数列 fib ( 40)语言实现时间(单位:秒)评pytho n官方 pyth on 2.4.3纯python fib函数568.718天啊使用psyco加速的python fib函数 17.922比较接近,还行使用swig直接转换的C语言编写的模块13.453使用Ci npy嵌入fib函数11.532CVC6速度优化编译的可执行文件5.562TinyCC。

2、 0.9.23编译的可执行文件6.719解释执行6.813FreeBASICfbc 0.16b-arch 486)-arch 686)编译的可执行文件(8.022编译的可执行文件(7.619forth4th 3.5a24th ex fib.4th277这个表现太失望了4th esv fib.4th fib.hx4th lx fib.hx1964th lg fib.hx fib.cmingw -O2 fib.c -o fib.exe110 gforth-0.6.2Gforth-fast fib.gfth14.719不错,不过不是说和C的速度可以比嘛?怎么也就是优化的python的速度啊注:其余源。

3、程序freebasic fun cti on fib(x as in teger) as in teger if x&l t;=1the nreturn 1elsereturn fib(x-1) + fib(x-2)end ifend fun cti on dim starttime, en dtime as doubledim res as in tegerstarttime=timerres=fib(40)en dtime=timerprin t fib(40)= ;res prin t time elapsed: ; (en dtime-starttime); s4th : fib ( 。

4、x - y )dup 2 & gt; ifdup 1 - recurseswap 2 - recurse +exitthe ndrop 1 ;time41 fib . crtimeswap -.time elapsed . . s crgforth-0.6.2 : fib ( x - y )dup 2 & gt; ifdup 1 - recurseswap 2 - recurse +exitthe ndrop 1 ;utime41 fib . crutime2swap d-time elapsed d. . us cr如果在windows下使用 mingw编译当前的 TinyCC,嵌入 C脚本。

5、会报错:tcc: file 7c/Program Files/tcc/libtcc1.a not found Doug Currie 在tcc的邮件列表里面提供了一个补丁:Here is what I did (andreported to the maili ng list) last February,so the patch may not be accurate for more rece nt vers ions, but the issues are the same.I have been able to create a libtcc.dll for WinXP usingMi。

6、 nGW/MSYS;the cha nges that were n ecessary were very minor. Perhaps this descripti on will help others use libtcc on Win dows. First, a small bug to report:In tcc.c the fun cti on tcc_base name() follows the line:#if !defi ned(LIBTCC)but the fun cti on tcc_base name()is used in pe_build_exports() i。

7、n tccpe.c -moving the #if line below the function tcc_basename() eliminates a link error buildi ng libtcc.dll in PE target mode.Second, con figuri ng and making tcc with MSYS places a path name incon fig.h in MSYS format. For example, I passed the argume nt - prefix=/c/Dev/tccto con figure; this pat。

8、h was good for buildi ng tcc.exe and did nt contain any spaces, unlike the default path. This creates the line #define CONFIG_TCCDIR /c/Dev/tccin con fig.h. Manu ally cha nging this line to #defi neCONFIG_TCCDIR C:/Dev/tccen ables libtcc.dll to find the in clude files and libraries once the library 。

9、is built. The application tcc.exe avoids this problem by setting tcc_lib_path from the applications directory at startup. A similar solution could be used for the library usi ng DllMai n. So, after con figure, fix CONFIG_TCCDIR in con fig.h and make.Fin ally, the library libtcc.dll can be built with。

10、 the MSYS comma nd: gcc -O2 -shared -Wall -Wl,-export-all-symbols / - mpreferred-stack-bo un dary=2 /-march=i386 -falig n-fun ctio ns=0 -fno-strict-aliasi ng / -DTCC_TARGET_PE -DLIBTCC -o libtcc.dll tcc.cBelow is a diff of tcc-0.9.23 tcc.c and the cha nges for LIBTCC with TCC_TARGET_PE and DLL locat。

11、ion of library files.Regards,e$ diff -u ./tcc-0.9.23-o/tcc.c tcc.c-./tcc-0.9.23-o/tcc.c Fri Jun 17 18:09:15 2005 + tcc.c Tue Feb28 17:03:44 2006 -10157,8 +10157,6 flag_ name, value);-#if !defi ned(LIBTCC)/* extract the base name of a file */static const char *tcc_base name(c onst char *n ame) -10175。

12、,6 +10 仃3,35 return p;+#if defi ned(LIBTCC)+#ifdef WIN32+int __stdcall DllMai n(void * hin stDLL, un sig ned long fdwReas on, void *IpvReserved)+if (fdwReas on = 1/*DLL_PROCESS_ATTACH*/)+/* on win 32, as impleme nted in mai n()+we suppose the lib and includes are at the location ofthis library+*/+static char path1024;+char *p, *d;+GetModuleFileNameA(h in stDLL, path, sizeof path);+p = d = strlwr(path);+while (*d)+if (*d = /) *d = /, p = d;+d;+*p = 70:+tcc_lib_path = path;+return 1 /*TRUE*/;+#e ndif+#else /* !LIBTCC */static in t64_t getclock_us(void)#ifdef WIN32。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值