首页 > 编程语言 >python高性能计算:cython入门代码

python高性能计算:cython入门代码

时间:2024-07-30 20:09:23浏览次数:8  
标签:prime cython return 入门 python setup num time import

三种实现的对比:

(1)纯python

x.py

def is_prime(num):
    for j in range(2, num):
        if (num%j)==0:
            return False
    return True




import time

a = time.time()
for i in range(10, 100000):
    is_prime(i)
b = time.time()

print(b-a)



(2) x2.pyx

def is_prime(num):
    for j in range(2, num):
        if (num%j)==0:
            return False
    return True




import time

a = time.time()
for i in range(10, 100000):
    is_prime(i)
b = time.time()

print(b-a)

编译文件:

x2_setup.py

from distutils.core import setup
from Cython.Build import cythonize

setup(
  name = 'Great Circle module v1',
  ext_modules = cythonize("x2.pyx", 
                          #compiler_directives={'language_level' : "3"}
                          # or "2" or "3str"
                          ),
)

编译:

python x2_setup.py build_ext --inplace



(3) x3.pyx

def is_prime(num):
    cdef int j
    for j in range(2, num):
        if (num%j)==0:
            return False
    return True




import time

a = time.time()
for i in range(10, 100000):
    is_prime(i)
b = time.time()

print(b-a)

编译文件:

x3_setup.py

from distutils.core import setup
from Cython.Build import cythonize

setup(
  name = 'Great Circle module v1',
  ext_modules = cythonize("x3.pyx", 
                          #compiler_directives={'language_level' : "3"}
                          # or "2" or "3str"
                          ),
)

编译:

python x3_setup.py build_ext --inplace



运行效果对比:

image





标签:prime,cython,return,入门,python,setup,num,time,import
From: https://www.cnblogs.com/devilmaycry812839668/p/18333246

相关文章

  • 【第二节】python编程基础语法
    目录一、运算符介绍1.1算术运算符1.2比较运算符1.3赋值运算符1.4位运算符1.5逻辑运算符1.6成员运算符1.7身份运算符二、python运算符优先级三、三大流程结构四、列表五、元组六、字典一、运算符介绍1.1算术运算符1.2比较运算符1.3赋值运算符......
  • 基于python的百度迁徙迁入、迁出数据分析(四)
    这篇文章是对上篇文章的可获取数据的时间区间的修正,依然通过开发者模式找寻相关数据源,我直接把数据url贴在这里,可以发现里面包含了相对明面上看不到的数据包括,行政区id、春运迁徙数据等:qianxi.cdn.bcebos.com/app/index.js?9bf6150c2c2807aeaddb上篇文章在这里,有兴趣的可以连......
  • python高性能计算:cython使用openmp并行 —— 报错:undefined symbol: omp_get_thread_n
    test.pyx文件:fromcython.parallelcimportparallelfromopenmpcimportomp_get_thread_numcpdefvoidlong_running_task1()noexceptnogil:whileTrue:passcpdefvoidlong_running_task2()noexceptnogil:whileTrue:passdefdo......
  • 使用Python Paramiko创建文件目录并上传文件的终极指南
    哈喽,大家好,我是木头左!前言:为何选择Paramiko?在网络运维和自动化领域,SSH(SecureShell)协议是连接和管理远程服务器的常用手段。而Paramiko是一个用于进行SSH2会话的Python库,它支持加密、认证和文件传输等功能。使用Paramiko,可以方便地实现远程命令执行、文件上传下载等操作。准......
  • python_爬虫基础
    python爬虫基础1、初识爬虫1.rebots协议网站通过Robots协议告诉搜索引擎哪些页面可以抓取,哪些页面不能抓取,但它仅仅是互联网中的约定而已,可以不用遵守。例如:https://www.taobao.com/robots.txt2.谷歌浏览器插件●XPathHelper●WebScraper●ToggleJavaScript●User-......
  • Python - Creating Managed Attributes using properties
    CreatingManagedAttributesusingpropertiesPropertiescanbeusedtocreatedataattributeswithspecialfunctionality.Ifyouwantsomeextrafunctionality(liketypechecking,datavalidationortransformation)whilegettingorsettingadataattribut......
  • NSSCTF web入门题鉴赏
    前言思来想去还是觉得web好玩一点哈哈,尝试过crypto更能懂那种痛楚[SWPUCTF2021新生赛]Do_you_know_httphttps://www.nssctf.cn/problem/385这道题就是简单的http协议刚开始是'WLLM'browser!这个题眼,于是我们上网络查询一下,发现useragency并不是它,那就需要我们去改一下us......
  • 初步入门C ++之类的概念
    文章目录0HelloWorld!1编译过程2类2.1类的概念2.2构造函数与析构函数0HelloWorld!#include<iostream> //相当于#include<stdio.h>intmain(intargc,charargv[]){char c;std::cout<<"HelloWorld!\n"<<std::endl......
  • Python - Static Methods
    Sometimeswehavetowritemethodsthatarerelatedtotheclassbutdonotneedanyaccesstoinstanceorclassdataforperformingtheirwork.Thesemethodscouldbesomehelperorutilitymethodsthatareusedinsidetheclassbuttheycanperformthei......
  • python中列表的学习
    列表目录列表列表的定义创建列表列表的索引列表的切片内置函数列表的遍历列表的常用方法(变量.方法名())列表的定义List(列表)是Python中使用最频繁的数据类型,在其他语言中通常叫做数组专门用于存储一串信息列表用[]定义,数据之间使用﹐分隔列表的索引从О开始索引就是数据在列......