小编sim*_*mha的帖子

何时使用StringIO,而不是加入字符串列表?

使用StringIO作为字符串缓冲区比使用list作为缓冲区慢.

何时使用StringIO?

from io import StringIO


def meth1(string):
    a = []
    for i in range(100):
        a.append(string)
    return ''.join(a)

def meth2(string):
    a = StringIO()
    for i in range(100):
        a.write(string)
    return a.getvalue()


if __name__ == '__main__':
    from timeit import Timer
    string = "This is test string"
    print(Timer("meth1(string)", "from __main__ import meth1, string").timeit())
    print(Timer("meth2(string)", "from __main__ import meth2, string").timeit())
Run Code Online (Sandbox Code Playgroud)

结果:

16.7872819901
18.7160351276
Run Code Online (Sandbox Code Playgroud)

python stringio

48
推荐指数
3
解决办法
6万
查看次数

python 2.7 vs python 3.1

一些python 3的功能和模块已被反向移植到python 2.7 python 3.1和python 2.7之间的显着差异是什么?

python compatibility versions

13
推荐指数
1
解决办法
6725
查看次数

是否有python2.7的内存分析器?

我需要检查我在python中使用的对象的内存统计信息.
我遇到了guppy和pysizer,但它们不适用于python2.7.
是否有可用于python 2.7的内存分析器?
如果没有,我有办法自己做吗?

python memory profiler

5
推荐指数
0
解决办法
3567
查看次数

为什么文件读取再次阅读速度更快?

SIZE = 1<<16
def justread(file):
    with open(file, 'rb') as f:
        while f.read(SIZE):
            pass

我第一次在700MB文件上运行此功能需要19秒.当我再次重复
阅读同一个文件时,它花费的时间减少到0.5秒.
我用很多文件重复了这个,结果很相似.
这里发生了什么?

python linux file

2
推荐指数
2
解决办法
373
查看次数

标签 统计

python ×4

compatibility ×1

file ×1

linux ×1

memory ×1

profiler ×1

stringio ×1

versions ×1