为什么在某些情况下,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中的串联速度越来越慢?相关推荐

  1. python 时间序列预测_使用Python进行动手时间序列预测

    python 时间序列预测 Time series analysis is the endeavor of extracting meaningful summary and statistical ...

  2. python 概率分布模型_使用python的概率模型进行公司估值

    python 概率分布模型 Note from Towards Data Science's editors: While we allow independent authors to publis ...

  3. python selenium 下拉列表_从下拉列表中选择python selenium选项

    我试图使用下面的代码使用selenium(python)从网页上的下拉列表中选择一个选项.在from selenium import webdriver from selenium.webdriver ...

  4. python 语料标注_在python中怎么标记文本?

    要运行下面的python程序,必须在系统中安装(NLTK)自然语言工具包. NLTK模块是一个庞大的工具包,旨在帮助您使用整个自然语言处理(NLP)方法. 要安装NLTK,请在终端中运行以下命令.su ...

  5. python pipeline框架_介绍Python的Django框架中的静态资源管理器django-pipeline

    django-pipeline 是一个 Django 下非常方便的静态资源管理 app,尤其是 1.2 版本之后,利用 django-staticfiles 的collectstatic 命令,在开发 ...

  6. python中立方表示_在Python中表示一个对象的方法

    在 Python 中一切都是对象.如果要在 Python 中表示一个对象,除了定义 class 外还有哪些方式呢?我们今天就来盘点一下. 0x00 dict 字典或映射存储 KV 键值对,它对查找.插 ...

  7. python临床数据_从临床试验中获取数据

    我正在开发一个小Python函数来从clinicalTrials.gov中获取数据.从每个研究记录中,我想从中找出研究的目标条件.例如,对于this研究记录,我需要以下内容:conditions = ...

  8. python注入进程_向进程中注入Python代码

    我想把Python代码注入到一个进程中,当它注入时,它似乎会使我的进程崩溃.我没有在我自己的程序中得到任何错误,但目标进程停止工作.被调用的非托管api没有给我任何错误,并且似乎已经正确地执行了它们的 ...

  9. python创建矩阵_在Python中创建矩阵的Python程序

    python创建矩阵 There is no specific data type in Python to create a matrix, we can use list of list to c ...

最新文章

  1. bluecam连接步骤说明_磊科路由器登录设置及默认密码说明
  2. 迁移ADT/ANT构建的Android项目至Gradle,a walk through。
  3. 中国高校毕业生即将加速蓝领化
  4. 一次Rootkit实施失败记(图解)
  5. android多点触控自由对图片缩放
  6. 高等组合学笔记(十一):分拆与Gauss二项式系数,恒等式与展开式
  7. linux 同一个ip 绑定两个不同的域名 访问两个不同的项目
  8. 一篇文章告诉你[C++]数组初始化
  9. Flutter - 底部导航详解与案例示范
  10. Google 编程之夏:海量优质项目,丰厚报酬,你竟然还不知道?
  11. 综合布线工程GB50311-2016中的各大子系统示意图
  12. PTA 发布关于巴基斯坦境内允许的频段和输出功率限制新规
  13. 定义图书类Book,具有属性账号id,铭name.作者author和价格price,在创建图书对象时要求通过构造器进行创建,- -次性将四个属性全部赋值
  14. 学会有礼貌的拒绝应聘者
  15. CA认证过程及https实现方法
  16. 如何正确理解SEO优化与搜索引擎优化之间的关系
  17. Android自动化测试应用:uiautomatorviewer工具的安装与使用
  18. 思科认证介绍(各种证书)
  19. Linux下网络传输测速程序小记
  20. 用计算机如何弹安和桥,学子风采|计算机与软件工程学院全能大赛决赛预告

热门文章

  1. 【渝粤题库】国家开放大学2021春2681煤矿采掘技术题目
  2. 【渝粤题库】国家开放大学2021春2542行政组织学题目
  3. linux中用参数代替键盘输入,Linux终端中使用上一命令减少键盘输入
  4. oracle 邻接模型,【原创】MySQL 模拟Oracle邻接模型树形处理
  5. centos模拟post请求_java模拟post和get请求(2019/10/25)
  6. antd 3升级命令_是时候拥有一个你自己的命令行工具了
  7. python上传文件到linux服务器_python上传大文件到服务器报错
  8. 远程声控系统(MATLAB代码见CSDN资源)
  9. php设置超链接,怎么给一个PHP密码访问页面加超链接
  10. 数据库(3)——关系