小编pcd*_*mmy的帖子

tempfile.TemporaryFile与StringIO

我写了一个小基准,我比较了ZOCache的不同字符串连接方法.

所以它看起来像tempfile.TemporaryFile比其他任何东西都要快:

$ python src/ZOCache/tmp_benchmark.py 
3.00407409668e-05 TemporaryFile
0.385630846024 SpooledTemporaryFile
0.299962997437 BufferedRandom
0.0849719047546 io.StringIO
0.113346099854 concat
Run Code Online (Sandbox Code Playgroud)

我一直在使用的基准代码:

#!/usr/bin/python
from __future__ import print_function
import io
import timeit
import tempfile


class Error(Exception):
    pass


def bench_temporaryfile():
    with tempfile.TemporaryFile(bufsize=10*1024*1024) as out:
        for i in range(0, 100):
            out.write(b"Value = ")
            out.write(bytes(i))
            out.write(b" ")

        # Get string.
        out.seek(0)
        contents = out.read()
        out.close()
        # Test first letter.
        if contents[0:5] != b"Value":
            raise Error


def bench_spooledtemporaryfile():
    with tempfile.SpooledTemporaryFile(max_size=10*1024*1024) as out:
        for i in range(0, 100):
            out.write(b"Value = …
Run Code Online (Sandbox Code Playgroud)

python stringio cstringio

5
推荐指数
1
解决办法
3111
查看次数

标签 统计

cstringio ×1

python ×1

stringio ×1