Embed C language scripts in Python

Source: Internet
Author: User

With cinpy and C language interpreter tinycc, you can directly embed C language fragments in Python programs and directly use functions written in C without compilation.

On the Win2k platform, the simple test comparison data is as follows (Recursive Calculation40th Rabbit series fib (40 ))

Language

Implementation

Time
(Unit: seconds)

Rating
Python Official Python 2.4.3 Pure Python fib Function 568.718 God
Python fib function accelerated by psyco 17.922 Close, okay
Modules written in C language using swig Direct Conversion 13.453
Use cinpy to embed fib Functions 11.532
         
C Vc6 Speed-optimized compiled executable files 5.562  
Tinycc 0.9.23 Compiled executable files 6.719  
Explain execution 6.813  
         
Freebasic FBC 0.16b Compiled executable files (-arch 486) 8.022  
Compiled executable files (-arch 686) 7.619  
         
Forth 4th 3.5a2 4th CX fib.4th 277 This is so disappointing.
4th CSV maid. HX
4th lx fib. HX
196
4th LG fib. HX fib. c
Mingw-O2 fib. C-o fiber .exe
110
Gforth-0.6.2 Gforth-fast fib. gfth 14.719 Yes, but it doesn't mean the speed is comparable to that of C?
How is the speed of optimized python?

 

Note:

  1. Other source programs

    • Freebasic

      function fib(x as integer) as integerif x<=1 thenreturn 1elsereturn 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 - recurse +exitthendrop 1 ;time41 fib . crtimeswap -." time elapsed " . ." s" cr
    • Gforth-0.6.2
      : fib ( x -- y ) dup 2 > if dup  1 - recurse swap 2 - recurse +  exit then drop 1 ;utime41 fib . crutime2swap d- ." time elapsed " d. ." us" cr

     

  2. If you use mingw in Windows to compile the current tinycc, an error is returned when embedding the C script:
    TCC: File '/C/program files/TCC/libtcc1.a' not found

    Doug Currie provides a patch in the TCC email list:

    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
    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, inserting ing 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

    # Define config_tccdir "C:/dev/TCC"

    Enables libtcc. DLL to find the include files and libraries once
    Library is built. [the application tcc.exe avoids this problem
    Setting tcc_lib_path from the application's directory at startup.

    Similar solution cocould 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
    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 provided des 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.