python中嵌入C語(yǔ)言腳本_第1頁(yè)
python中嵌入C語(yǔ)言腳本_第2頁(yè)
python中嵌入C語(yǔ)言腳本_第3頁(yè)
免費(fèi)預(yù)覽已結(jié)束,剩余8頁(yè)可下載查看

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、python中嵌入C語(yǔ)言腳本借助Cinpy和C語(yǔ)言解釋器TinyCC,可以在python程 序里面直接嵌入 C語(yǔ)言片斷、不經(jīng)編譯直接使用 C編寫的函 數(shù)了。win2k平臺(tái)上,簡(jiǎn)單的測(cè)試對(duì)比數(shù)據(jù)如下(遞歸方法計(jì)算第 四十項(xiàng)兔子數(shù)列fib (40)語(yǔ)言實(shí)現(xiàn)時(shí)間(單位:秒)評(píng)python官方 純python fib 函數(shù)568.718天啊使用psyco加速的python fib 函數(shù)17.922比較接近,還行使用swig直接轉(zhuǎn)換的C語(yǔ)言編寫的模塊13.453使用Ci叩y嵌入fib函數(shù)11.532CVC6速度優(yōu)化編譯的可執(zhí)行文件5.562編譯的可執(zhí)行文件6.719解釋執(zhí)行6.813FreeBASICf

2、bc 0.16b-arch 486)-arch 686)編譯的可執(zhí)行文件(8.022編譯的可執(zhí)行文件(7.619forth4th 3.5a24th ex fib.4th277這個(gè)表現(xiàn)太失望了4th esv fib.4th fib.hx4th lx fib.hx1964th Ig fib.hx fib.eGforth-fast fib.gfth14.719不錯(cuò),不過(guò)不是說(shuō)和 C的速度可以比嘛?怎么也就是優(yōu)化的 python的速度啊注:其余源程序freebasic function fib(x as integer) as integer if x&l t;=1 thenreturn 1e

3、lsereturn fib(x-1) + fib(x-2)end ifend functiondim starttime, endtime as doubledim res as integerstarttime=timerres=fib(40)endtime=timerprint "fib(40)= "resprint "time elapsed: " (endtime-starttime); " s"4th : fib ( x - y )dup 2 > ifdup 1 - recurseswap 2 - recurs

4、e +exitthendrop 1 ;time41 fib . crtimeswap -."time elapsed " . ." s" crgforth-0.6.2 : fib ( x - y )dup 2 > ifdup 1 - recurseswap 2 - recurse +exitthendrop 1 ;utime41 fib . crutime 2swap d-."time elapsed " d. ." us" cr如果在windows下使用mingw編譯當(dāng)前的TinyCC,嵌入C腳本會(huì)

5、報(bào)錯(cuò):tcc: file 7c/Program Files/tcc/libtcc1.a' not foundDoug Currie在tcc的郵件列表里面提供了一個(gè)補(bǔ)?。篐ere is what I did (and reported to the mailing list)last February,so the patch may not be accurate for more recentversions, but theissues are the same.I have been able to create a libtcc.dll for WinXP using MinG

6、W/MSYS;the changes that were necessary were very minor.Perhaps thisdescription 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() intccpe.c - movi

7、ng the #if line below the function tcc_basename()eliminates a link error building libtcc.dll in PEtarget mode.Second, configuring and making tcc with MSYS places a pathname inconfig.h in MSYS format. For example, I passed theargument-prefix=/c/Dev/tccto configure; this path was good for building tcc

8、.exeand didn'tcontain any spaces, unlike the default path. Thiscreates the line#define CONFIG_TCCDIR "/c/Dev/tcc"in config.h. Manually changing this line to#define CONFIG_TCCDIR "C:/Dev/tcc"enables libtcc.dll to find the include files andlibraries once thelibrary is built. Th

9、e application tcc.exe avoids this problem bysetting tcc_lib_path from the application's directoryat startup. Asimilar solution could be used for the library usingDllMain.So, after configure, fix CONFIG_TCCDIFh config.h and make.Finally, the library libtcc.dll can be built with theMSYS command:gc

10、c -02 -shared -Wall -Wl,-export-all-symbols /-mpreferred-stack-boundary=2 /-march=i386 -falign-functions=0 -fno-strict-aliasing/-DTCC_TARGET_PE - DLIBTCC -o libtcc.dll tcc.cBelow is a diff of tcc-0.9.23 tcc.c and the changes forLIBTCC withTCC_TARGET_PE and DLL location of library files.Regards,e-./t

11、cc-0.9.23-o/tcc.c Fri Jun 17 18:09:152005+ tcc.c Tue Feb 28 17:03:44 2006 -10157,8 +10157,6 flag_name, value);-#if !defined(LIBTCC)/* extract the basename of a file */static const char *tcc_basename(const char *name) -10175,6 +10173,35 return p;+#if defined(LIBTCC)+#ifdef WIN32+int _stdcall DIIMain(

12、void * hinstDLL, unsigned long fdwReason, void *lpvReserved)+ if (fdwReason = 1/*DLL_PROCESS_ATTACH*/)+ +/* on Win32, as implemented in main()+ we suppose the lib and includes are at thelocation of this library+ */+static char path1024;+char *p,*d;+GetModuleFileNameA(hinstDLL, path, sizeofpath);+p = d = strlwr(pat

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論