python中嵌入C语言脚本

2023-05-16

借助Cinpy和C语言解释器TinyCC,可以在python程序里面直接嵌入C语言片断、不经编译直接使用C编写的函数了。

win2k平台上,简单的测试对比数据如下(递归方法计算第四十项兔子数列fib(40))

语言

实现

时间
(单位:秒)

python官方python 2.4.3python fib函数568.718天啊
使用psyco加速的python fib函数17.922比较接近,还行
使用swig直接转换的C语言编写的模块13.453
使用Cinpy嵌入fib函数11.532
     
CVC6速度优化编译的可执行文件5.562 
TinyCC 0.9.23编译的可执行文件6.719 
解释执行6.813 
     
FreeBASICfbc 0.16b编译的可执行文件(-arch 486)8.022 
编译的可执行文件(-arch 686)7.619 
     
forth4th 3.5a24th cx fib.4th277 这个表现太失望了
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.2Gforth-fast fib.gfth14.719不错,不过不是说和C的速度可以比嘛?
怎么也就是优化的python的速度啊

 

注:

  1. 其余源程序
    • freebasic
      
      function fib(x as integer) as integer
      	if x<=1 then
      		return 1
      	else
      		return fib(x-1) + fib(x-2)
      	end if
      end function
      
      dim starttime, endtime as double
      dim res as integer
      
      starttime=timer
      res=fib(40)
      endtime=timer
      print "fib(40)= ";res
      print "time elapsed: "; (endtime-starttime); " s"
              
    • 4th 
      
      : fib ( x -- y )
      	dup 2 > if
      		dup  1 - recurse
      		swap 2 - recurse +
      		exit
      	then
      		drop 1 ;
      
      time
      
      41 fib . cr
      
      time
      swap -
      ." time elapsed " . ." s" cr  
    • gforth-0.6.2
      
      : fib ( x -- y ) 
      	dup 2 > if 
      		dup  1 - recurse 
      		swap 2 - recurse +  
      		exit 
      	then 
      		drop 1 ;
      		
      utime
      
      41 fib . cr
      
      utime
      2swap d- 
      ." time elapsed " d. ." us" cr  

     

  2. 如果在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 line below the function tcc_basename()
    eliminates a link error building libtcc.dll in PE target mode.

    Second, configuring and making tcc with MSYS places a pathname in
    config.h in MSYS format. For example, I passed the argument

    --prefix=/c/Dev/tcc

    to configure; this path was good for building tcc.exe and didn't
    contain any spaces, unlike the default path. This creates 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 and libraries once the
    library is built. [The application tcc.exe avoids this problem by
    setting tcc_lib_path from the application's directory at startup. A

    similar solution could be used for the library using DllMain.]

    So, after configure, fix CONFIG_TCCDIR in config.h and make.


    Finally, the library libtcc.dll can be built with the MSYS command:

    gcc -O2 -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.c

    Below is a diff of tcc-0.9.23 tcc.c and the changes for LIBTCC with
    TCC_TARGET_PE and DLL location 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 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 DllMain(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 the location of this library
    +    */
    +        static char path[1024];
    +        char *p, *d;
    +
    +        GetModuleFileNameA(hinstDLL, path, sizeof path);
    +        p = d = strlwr(path);
    +        while (*d)
    +        {
    +            if (*d == '//') *d = '/', p = d;
    +            ++d;
    +        }
    +        *p = '/0';
    +        tcc_lib_path = path;
    +    }
    +    return 1 /*TRUE*/;
    +}
    +#endif
    +
    +#else /* !LIBTCC */
     static int64_t getclock_us(void)
     {
     #ifdef WIN32

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

python中嵌入C语言脚本 的相关文章

随机推荐

  • Linux - 搭建LDAP统一认证服务

    目的 通过以下步骤最终可使用ldap server中的用户登录一台ldap client xff0c 并允许有sudo权限 平常公司中所用的域账号以及服务器账号也许就是使用如下方式 xff0c 但是应该没有这么简陋 xff0c 只是借机了解
  • 每日练习------有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数

    题目 有n个整数 xff0c 使其前面各数顺序向后移m个位置 xff0c 最后m个数变成最前面的m个数 解题关键 需要新建个数组使得原数组可以做到整体往后移动M位 思路 1 创建个有n个整数的数组 2 输出n个数字 存储到数组中 3 遍历原
  • FLTK-Rs

    终于还是到这一步了 xff0c 可视化 xff0c 我的超人 xff01 FLTK是一个跨平台的轻量级 gui 库 该库本身是用 C 43 43 98 编写的 xff0c 具有很高的可移植性 fltk crate 是用 Rust 编写的 x
  • STM32寄存器点灯失败

    include 34 stm32f10x h 34 int main void unsigned int 0x40021018 61 1 lt lt 3 打开时钟GPIOB unsigned int 0x40010C00 61 1 lt l
  • nested exception is java.sql.SQLException: com.mysql.cj.jdbc.Driver

    解决方案 在 pom xml 中 xff0c 加入 mysql 的 maven 引用 span class token tag span class token tag span class token punctuation lt spa
  • iOS-开辟子线程(NSThread、NSOperationQueue、GCD)

    本节主要总结一些开辟子线程的常用的几种方法 一 通过NSThread类开辟子线程 1 NSThread手动开启子线程 span class hljs comment 创建线程对象 span span class hljs built in
  • iOS -UICollectionView添加区头区尾

    项目中需求时三个区 xff0c 没个区展示不同的数据 xff0c 一格一格的 xff0c 所以tableView是不是适用的 xff0c 需要采用collectionView才能更好的展示 xff0c 那么怎么给它添加区头区尾呢 xff1f
  • miui 安装app闪退问题

    android版本 xff1a 7 0 MIUI版本 xff1a 8 2 手机 xff1a 小米5 之前老版本可以运行 xff0c 今天用AS的run xff0c 在安装apk时报application installation faile
  • 在x64上构建智能家居(home assistant) (一) Supervised版本安装

    我的上一篇文章 在嵌入式x86上构建我的智能家居 home assistant 中本来希望在一个低功耗的x86嵌入式上安装home assistant xff0c 但是因为一些限制没有成功 找到一个低功耗的笔记本 xff08 东芝的dyna
  • 安装YMFE/yapi API管理服务器(Ubuntu20)

    GitHub YMFE yapi YApi 是一个可本地部署的 打通前后端及QA的 可视化的接口管理平台 YApi 是一个可本地部署的 打通前后端及QA的 可视化的接口管理平台 Contribute to YMFE yapi develop
  • 安装nodejs18 + yapi(Debian11)

    安装nodejs Node js Node js is a JavaScript runtime built on Chrome 39 s V8 JavaScript engine https nodejs org zh cn 官方手顺 通
  • Postgresql count 慢的处理方法

    performance Postgresql extremely slow count with index simple query Database Administrators Stack Exchange https dba sta
  • 解决Referenced file contains errors(struts-2.0.dtd)

    解决方法 两种 1 这个可能是你的DTD文件找不到 或者解析有错 才发生的错误 你可以在地址栏里输入http struts apache org dtds struts 2 0 dtd 这个看能查看不 如果不能 应该是网络的问题或XML解析
  • 使用POI向Excel中插入多张图片

    最近在大量使用poi对Excel进行操作 xff0c 可以说是越用越气愤 xff0c 很多功能支持得不完善 xff0c 一个在VB里很简单的操作 xff0c 你用poi实现可能就要多几倍甚至是数10倍的代码 但是我们搞JAVA的总不能丢掉J
  • 将“存储卡”改名为Storage Card的方法

    HKEY LOCAL MACHINE System StorageManager Profiles SDMemory 34 Name 34 61 34 SD Memory Card 34 34 Folder 34 61 34 Storage
  • ubuntu 驱动更新后导致无法进入界面

    问题描述 xff1a 安装新ubuntu系统后未禁止驱动更新导致无法进入登录界面 解决办法 xff1a 首先在进入BIOS中 xff0c 修改设置以进行命令行操作 xff0c 然后卸载已有的系统驱动 xff0c 最后安装新的驱动即可 开机按
  • PPC WM6.1智能手机上使用日语辞典浅谈

    在PPC手机上用日语辞典 xff08 広辞苑 xff0c 三省堂等 xff09 http bulo hjenglish com group topic 144804 PPC上的日文输入法 http bulo hjenglish com gr
  • PPC音量太小和听筒音太小的解决方法

    1下载注册表修改器 2复制修改器到PPC xff08 最好是卡上啦 xff09 3在PPC上运行修改器 我用的是华硕P525 以下是我小P的设置 xff1a 找到HKEY CURRENT USER ControlPanel Phone 项下
  • Delphi中的集成VBS脚本语言应用

    罗焱 从薇 王正浩 摘 要 xff1a 使用ActiveX Scripting技术 xff0c 可以在应用程序中集成使用脚本语言 本文介绍如何应用这一技术在Delphi应用程序中添加VBScript支持 关键词 xff1a ActiveX脚
  • python中嵌入C语言脚本

    借助Cinpy 和C语言解释器TinyCC xff0c 可以在python 程序里面直接嵌入C语言片断 不经编译直接使用C编写的函数了 win2k平台上 xff0c 简单的测试对比数据如下 xff08 递归方法计算第四十项兔子数列fib x