【python性能提升的几种方法】教程文章相关的互联网学习教程文章

为什么Python和Cython中这两个代码之间存在巨大的性能差异?【代码】

我在Python中遇到了性能问题,我的一位朋友建议我使用Cython搜索更多后,我发现这个代码来自here Python:def test(value):for i in xrange(value):z = i**2if(i==1000000):print iif z < i:print "yes" test(10000001)用Cython:def test(long long value):cdef long long icdef long long zfor i in xrange(value):z = i**2if(i==1000000):print iif z < i:print "yes"test(10000001)在我执行两个代码之后,令人惊讶的是我通过Cytho...

Python:如何提高脚本的性能以获得一组点的边界框【代码】

我有一组点(x和y),我想知道X和Y的最大值和最小值(边界框).我编码这些行,我用列表理解读取所有点,然后在X和Y上使用max和min.最后我删除了点. 这个解决方案不是内存效率,因为我需要读取所有点points = [(p.x,p.y) for p in lasfile.File(inFile,None,'r')] # read in list comprehension X_Max = max(zip(*points)[0]) X_Min = min(zip(*points)[0]) Y_Max = max(zip(*points)[1]) Y_Min = min(zip(*points)[1]) del points我要求建议...

python – 在分析具有多个正则表达式的单个字符串时,线程或多处理能否提高性能?【代码】

如果我想使用几十个正则表达式来分析字符串,线程或多处理模块可以提高性能吗?换句话说,分析多个线程或进程上的字符串会比以下更快:match = re.search(regex1, string) if match:afunction(match) else:match = re.search(regex2, string)if match:bfunction(match)else:match = re.search(regex3, string)if match:cfunction(match) ...只有一个正则表达式匹配,所以这不是一个问题.如果答案是多处理,你会建议使用什么技术(队列,管...

python – 性能问题:大numpy数组和系统调用【代码】

在预分配大量内存(例如numpy数组)后使用系统调用时会出现性能问题.问题随着内存的增加而增加. test.py:import os import sys import time import numpystart = time.clock() test = int(sys.argv[1]) a = numpy.zeros((test,500,500)) for i in range(test) :os.system("echo true > /dev/null") elapsed = (time.clock() - start) print(elapsed)每次迭代时间急剧增加:edouard@thorin:~/now3/code$python test.py 100 0.64 edou...

提高python中函数的性能【代码】

我有这种格式的几GB GB文本文件0 274 593869.99 6734999.96 121.83 1, 0 273 593869.51 6734999.92 121.57 1, 0 273 593869.15 6734999.89 121.57 1, 0 273 593868.79 6734999.86 121.65 1, 0 273 593868.44 6734999.84 121.65 1, 0 273 593869.00 6734999.94 124.21 1, 0 273 593868.68 6734999.92 124.32 1, 0 273 593868.39 6734999.90 124.44 1, 0 273 593866.94 6734999.71 121.37 1, 0 273 593868.73 6734999.99 127.28 1,我...

python – 循环性能下降【代码】

我有以下代码:keywordindex = cPickle.load(open('keywordindex.p','rb'))#contains~340 thousand items masterIndex = {}indexes = [keywordindex] for partialIndex in indexes:start = time.time()for key in partialIndex.iterkeys():if key not in masterIndex.keys():masterIndex[key]= partialIndex[key]elif key in masterIndex.keys():masterIndex[key].extend(partialIndex[key])cPickle.dump(masterIndex,open('Master...

Python 2D列表性能,没有numpy【代码】

我试图在Python中创建一个2D列表.我找到了两种可能性.def cArray(size):c = [[0. for i in range(size)] for j in range(size)]return cdef npArray(size):np = numpy.zeros((size,size))return np现在这两个函数都给出了正确的答案.这里的问题在于性能.我使用timeit运行了这两个,这是我的结果:list size is 5000 number of times run is 5cArray average time: 3.73241295815 npArray average time: 0.210782241821很明显,我想避...

关键字参数性能(python)【代码】

我试图通过使用timeit测试(计时)各种函数来优化一些python代码. 我发现根据变量是关键字参数还是函数内部,我得到的速度不同. 那是:def test_function(A = value()):#rest of function....返回的结果与以下不同:def test_function():A = value()#rest of function ...我想他们会得到非常相似的结果 – 我猜我不理解/遗漏了一些东西…… (也为测试做了10,000次循环)解决方法:关键字参数在函数定义时评估一次.因此,在您的第一个示例...

Python数据结构开销/性能

在Python中使用字典而不是元组是否有任何性能优势? 如果我正在优化速度,是否有理由更喜欢一个而不是另一个?解决方法:丰富, 列表和dicts是适合不同需求的野兽.确保你不使用列表进行线性搜索,其中dicts哈希是完美的,因为它的速度较慢.此外,如果您只需要遍历的元素列表,请不要使用dicts,因为它将占用比列表更多的空间. 这可能听起来很明显,但是通过算法选择正确的数据结构可以获得更高的性能,因为更高效的编译代码布局等可以实现微优...

(Python)解析文件以避免性能问题的最佳方法【代码】

我有点担心哪种方式最好处理一个必须隔离信息的文件. 例如,想象一个日志文件,其中数据以块为单位划分,每个块都有一个子块列表. 日志文件示例:data data data data block 1 start-sub block 1 start--data x--data y-sub block 1 end-sub block 2 start--data x--data marked as good--data z-sub block 2 endblock 1 endblock 1 summaryblock 2 start-sub block 1 start.....-sub block 1 end.... data data data我正在寻找一种有...

python – 提高查询性能【代码】

我需要从PostgreSQL数据库中读取并加入很多行(~500k)并将它们写入MySQL数据库. 我天真的做法看起来像这样entrys = Entry.query.yield_per(500)for entry in entrys:for location in entry.locations:mysql_location = MySQLLocation(entry.url)mysql_location.id = location.idmysql_location.entry_id = entry.id[...]mysql_location.city = location.city.namemysql_location.county = location.county.namemysql_location.state...

python – 是什么导致这两种插入排序实现之间的性能差异?【代码】

我有两个插入排序的实现.第一个是我的java教科书中的示例的转录(尽管有一个while循环而不是java for循环)def insertion_sort(array):for i in range(1,len(array)):j = iwhile j > 0 and array[j] < array[j-1]:array[j],array[j-1] = array[j-1], array[j]j=j-1return array第二个似乎是更加pythonic实现.def insertion_sort2(array):for i in range(1,len(array)):for j in range(i-1,-1,-1):if array[j+1] < array[j]:array[j+1...

python – 分析和改进Django的ORM(SORT)生成的查询的性能【代码】

基于我的Django模型中的几个条件,我有一个不那么复杂的(imho)过滤逻辑.有一个特定的查询需要花费很多时间才能完成. 查询是基于这两个查询集构建的:queryset = self.serializer_class.Meta.model.valid_pricelist_objects.filter(Q(drug_prices__pricelist__price_destination__to_all_insurances=True) |# pylint: disable=line-too-longQ(drug_prices__pricelist__price_destination__to_organization_data__organization__uuid=...

使用python over java进行Android App开发有哪些可能的性能问题?

我是Android的新手,正在寻找一个有很好的过滤系统的应用程序(电子邮件客户端),但经过大量谷歌搜索和Android市场浏览后,似乎没有一个可用于Android的电子邮件客户端提供这样的功能. (至少免费的没有).所以,最后我决定自己开发一个.现在的问题是我希望用Python开发它,但我担心效率问题.因此,问题是: >除了在Android上使用Python的API有限外,在Java和Python之间进行选择时需要注意哪些因素?>此外,我想我们需要使用我们的Python应用程...

python – numpy中的性能【代码】

我在python中编写一个方法,通过OpenCV,它处理来自摄像机的一些图像,并将该图像逐像素地与一些参数进行比较,并根据这些参数将该像素置于零.执行该操作的代码如下:while True: ret, frame = cap.read() # retrieve image from the camera for row in range(rows):for col in range(cols):b1 = (frame[row][col][:] < Nsigma2[row][col][:]).all();b2 = (frame[row][col][:] > Psigma2[row][col][:]).all();if ...