<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>C语言 :: 标签 :: yafeng 的博客</title>
    <link>https://yafengabc.github.io/tags/c%E8%AF%AD%E8%A8%80/index.html</link>
    <description></description>
    <generator>Hugo</generator>
    <language>zh-cn</language>
    <lastBuildDate>Sat, 19 Sep 2026 16:30:07 +0800</lastBuildDate>
    <atom:link href="https://yafengabc.github.io/tags/c%E8%AF%AD%E8%A8%80/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>用Cython加速Python程序以及包装C程序简单测试</title>
      <link>https://yafengabc.github.io/cnblogs/p6130849/index.html</link>
      <pubDate>Sun, 04 Dec 2016 15:02:00 +0800</pubDate>
      <guid>https://yafengabc.github.io/cnblogs/p6130849/index.html</guid>
      <description>用Cython加速Python程序 我没有拼错，就是Cython，C+Python=Cython!&#xA;我们来看看Cython的威力,先运行下边的程序：&#xA;import time&#xA;def fib(n):&#xA;if n0:&#xA;return 0&#xA;if n1:&#xA;return 1&#xA;return fib(n-1)+fib(n-2)&#xA;t=time.time()&#xA;print(fib(40))&#xA;print(time.time()-t)&#xA;$ python fib.py&#xA;102334155&#xA;59.367255449295044&#xA;在我的渣渣笔记本上，用时59.3秒，差不多一分钟。当然，在你那可能比我快一点，这也很正常。&#xA;好了，我们再试试Cython：&#xA;$ cython fib.py –embed&#xA;$ gcc -O3 fib.c -I /usr/include/python3.5m/ -lpython3.5m&#xA;$ ./a.out&#xA;102334155&#xA;14.487313747406006&#xA;嗯，快了那么一点点，4倍左右;我解释一下前边的几句代码：&#xA;首先，用cython命令把python生成c文件，也就是cython fib.py会生成一个fib.c的文件&#xA;–embed参数就是自动生成一个main函数，以便让gcc生成可执行程序。&#xA;接下来就是用gcc把fib.c编译成了个a.out程序，运行之，结果快了4倍（从60秒减少到15秒以内）。&#xA;当然，这只是小试牛刀，区区4倍而已，这也太少了！&#xA;接下来我吧这个文件复制成fib.pyx，并修改了一句代码：&#xA;import time&#xA;cdef int fib(int n):&#xA;if n0:&#xA;return 0&#xA;if n1:&#xA;return 1&#xA;return fib(n-1)+fib(n-2)&#xA;t=time.time()&#xA;print(fib(40))&#xA;print(time.time()-t)</description>
    </item>
  </channel>
</rss>