日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python越来越慢_为什么Python中的串联速度越来越慢?

發布時間:2023/12/2 python 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python越来越慢_为什么Python中的串联速度越来越慢? 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

為什么在某些情況下,Python 3中的連接似乎比Python 2中的連接慢?

影響最大的串聯方法似乎是字節對象的連續串聯,從O(n)到O(n?)操作.

我的分析代碼大部分在這里:

#!/usr/bin/env python

from operator import concat

from sys import version, version_info

from timeit import timeit # Compatibility: ver >= 2.6

# ver = version.partition('

')[0].rstrip()

ver = '.'.join(str(v) for v in version_info[:3])

print(ver)

if version_info[0] == 2:

from StringIO import StringIO

else:

from io import StringIO

from functools import reduce

xrange = range

def build_plus():

output = ''

for _ in xrange(input_len):

output += 'a'

return output

def build_join():

return ''.join('a' for _ in xrange(input_len))

def build_bytes_plus():

output = b''

for _ in xrange(input_len):

output += b'a'

return output

def build_stringio():

output = StringIO()

for _ in xrange(input_len):

output.write('a')

return output.getvalue()

def build_reduce():

return reduce(concat, ('a' for _ in xrange(input_len)))

builds = {'str+': build_plus,

'join': build_join,

'reduce': build_reduce,

'bytes+': build_bytes_plus,

'StringIO': build_stringio}

if version_info[0] == 2:

import cStringIO

def build_cstringio():

output = cStringIO.StringIO()

for _ in xrange(input_len):

output.write('a')

return output.getvalue()

builds['cStringIO'] = build_cstringio

else:

from io import BytesIO

def build_bytesio():

output = BytesIO()

for _ in xrange(input_len):

output.write(b'a')

return output.getvalue()

builds['BytesIO'] = build_bytesio

resfile = open('times.csv', 'a')

size_range = 50 # Number of points over the size axis

min_order = 1.0 # 10^x byte input min

max_order = 5.0 # 10^x byte input max

for allow_gc in (False, True):

setup = 'gc.enable()' if allow_gc else 'pass'

for build_name, build_fun in builds.items():

# For a roughly constant confidence interval, aim for uniform sample density across the

# (logarithmic) input size axis.

for size_index in range(size_range+1):

input_len = int(10**((max_order-min_order)*size_index/size_range + min_order))

# Rather than repeating many measurements at one input size, perform one measurement

# per input size for a continuous range of input sizes and apply smoothing later.

dur = timeit(build_fun, setup, number=1)

resfile.write('"%s",%s,"%s",%d,%.6g

' % (ver, str(allow_gc).upper(), build_name,

input_len, dur))

我的R腳本中的一些圖形如下所示:

總結

以上是生活随笔為你收集整理的python越来越慢_为什么Python中的串联速度越来越慢?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。