运行的Cython的3种方法


运行条件:python2.7

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

第一种:

fib.pyx

def fib(long n):
    '''Returns the nth Fibonacci number.'''
    cdef long a=0, b=1, i
    for i in range(n):
        a, b = a + b, a
    return a


test.py

import sys  
reload(sys)  
sys.setdefaultencoding('utf8')  
import pyximport; pyximport.install()

import fib
print fib.fib(15)

运行方法:

python test.py

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

第二种

fib.pyx

def fib(long n):
    '''Returns the nth Fibonacci number.'''
    cdef long a=0, b=1, i
    for i in range(n):
        a, b = a + b, a
    return a

setup.py

#!/usr/bin/python
#-*-coding:utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf8')
from distutils.core import setup
from Cython.Build import cythonize
#cythonize:编译源代码为C或C++,返回一个distutils Extension对象列表
setup(ext_modules=cythonize('fib.pyx'))

test.py

from fib import fib
print fib(9)

运行方法:

python setup.py build_ext --inplace
python test.py

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

第3种

c1.pyx

import math

def great_circle(float lon1,float lat1,float lon2,float lat2):
    cdef float radius = 3956.0
    cdef float pi = 3.14159265
    cdef float x = pi/180.0
    cdef float a,b,theta,c

    a = (90.0-lat1)*(x)
    b = (90.0-lat2)*(x)
    theta = (lon2-lon1)*(x)
    c = math.acos((math.cos(a)*math.cos(b)) + (math.sin(a)*math.sin(b)*math.cos(theta)))
    return radius*c


test.py

#!/usr/bin/env python  
#encoding: utf-8
import sys   #引用sys模块进来,并不是进行sys的第一次加载
reload(sys)  #重新加载sys
sys.setdefaultencoding('utf8')  ##调用setdefaultencoding函数
import timeit
from c1 import great_circle
num=10#表示运行多少次
# t = timeit.Timer("c1.great_circle(%f,%f,%f,%f)" % (lon1,lat1,lon2,lat2),"import c1")
t = timeit.Timer("c1.great_circle(%f,%f,%f,%f)" % (16,124,76,173),"import c1")
print "Cython function (still using python math)", t.timeit(num), "sec"

运行方法:

cython c1.pyx
gcc -c -fPIC -I/usr/include/python2.7/ c1.c
gcc -shared c1.o -o c1.so
python test.py



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在VSCode中运行Cython代码,您需要按照以下步骤进行设置和配置: 1. 首先,确保您已经安装了VSCode和Cython扩展。在VSCode的扩展市场中搜索并安装Cython扩展。 2. 打开VSCode,创建一个新的Python文件或打开一个包含Cython代码的Python文件。 3. 在VSCode的底部状态栏中选择您要使用的Python环境。如果您的Cython代码需要特定的Python环境,请确保选择了正确的环境。 4. 在VSCode的顶部菜单中选择“Terminal”>“New Terminal”来打开一个新的终端。 5. 在终端中使用以下命令安装Cython依赖: ``` pip install cython ``` 6. 编写您的Cython代码并将其保存到文件中。 7. 在VSCode的顶部菜单中选择“Terminal”>“Run Build Task...”来运行构建任务。 8. 在弹出的Quick Pick列表中选择“Cython: build”以开始构建Cython代码。 9. 构建完成后,您可以在终端中看到编译后的Cython代码。 10. 通过选择“Terminal”>“Run Task...”来运行任务,然后选择“Cython: run”来运行Cython代码。 请注意,这些步骤假定您的环境已正确配置并具有适当的Python和Cython安装。如果您遇到任何问题,请确保按照引用和引用中提到的配置和调试方法进行操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [VScode配置运行C/C++、python,及快捷键配置](https://blog.csdn.net/qq_25799253/article/details/127417315)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [一文掌握vscode远程调试python代码](https://blog.csdn.net/zong596568821xp/article/details/124344569)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值