首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用python编写巨大的字符串

用python编写巨大的字符串
EN

Stack Overflow用户
提问于 2015-02-10 06:20:22
回答 2查看 2.5K关注 0票数 2

我有一个非常长的字符串,几乎有一兆字节长,我需要写到一个文本文件中。常客

代码语言:javascript
复制
file = open("file.txt","w")
file.write(string)
file.close()

工作,但太慢了,有没有办法可以写得更快?

我正在尝试将一个几百万位的数字写入到一个文本文件中,该数字的量级是math.factorial(67867957)

这是性能分析中显示的内容:

代码语言:javascript
复制
    203 function calls (198 primitive calls) in 0.001 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.000    0.000 <string>:1(<module>)
        1    0.000    0.000    0.000    0.000 re.py:217(compile)
        1    0.000    0.000    0.000    0.000 re.py:273(_compile)
        1    0.000    0.000    0.000    0.000 sre_compile.py:172(_compile_charset)
        1    0.000    0.000    0.000    0.000 sre_compile.py:201(_optimize_charset)
        4    0.000    0.000    0.000    0.000 sre_compile.py:25(_identityfunction)
      3/1    0.000    0.000    0.000    0.000 sre_compile.py:33(_compile)
        1    0.000    0.000    0.000    0.000 sre_compile.py:341(_compile_info)
        2    0.000    0.000    0.000    0.000 sre_compile.py:442(isstring)
        1    0.000    0.000    0.000    0.000 sre_compile.py:445(_code)
        1    0.000    0.000    0.000    0.000 sre_compile.py:460(compile)
        5    0.000    0.000    0.000    0.000 sre_parse.py:126(__len__)
       12    0.000    0.000    0.000    0.000 sre_parse.py:130(__getitem__)
        7    0.000    0.000    0.000    0.000 sre_parse.py:138(append)
      3/1    0.000    0.000    0.000    0.000 sre_parse.py:140(getwidth)
        1    0.000    0.000    0.000    0.000 sre_parse.py:178(__init__)
       10    0.000    0.000    0.000    0.000 sre_parse.py:183(__next)
        2    0.000    0.000    0.000    0.000 sre_parse.py:202(match)
        8    0.000    0.000    0.000    0.000 sre_parse.py:208(get)
        1    0.000    0.000    0.000    0.000 sre_parse.py:351(_parse_sub)
        2    0.000    0.000    0.000    0.000 sre_parse.py:429(_parse)
        1    0.000    0.000    0.000    0.000 sre_parse.py:67(__init__)
        1    0.000    0.000    0.000    0.000 sre_parse.py:726(fix_flags)
        1    0.000    0.000    0.000    0.000 sre_parse.py:738(parse)
        3    0.000    0.000    0.000    0.000 sre_parse.py:90(__init__)
        1    0.000    0.000    0.000    0.000 {built-in method compile}
        1    0.001    0.001    0.001    0.001 {built-in method exec}
       17    0.000    0.000    0.000    0.000 {built-in method isinstance}
    39/38    0.000    0.000    0.000    0.000 {built-in method len}
        2    0.000    0.000    0.000    0.000 {built-in method max}
        8    0.000    0.000    0.000    0.000 {built-in method min}
        6    0.000    0.000    0.000    0.000 {built-in method ord}
       48    0.000    0.000    0.000    0.000 {method 'append' of 'list' objects}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
        5    0.000    0.000    0.000    0.000 {method 'find' of 'bytearray' objects}
        1    0.000    0.000    0.000    0.000 {method 'items' of 'dict' objects}
EN

回答 2

Stack Overflow用户

发布于 2015-02-12 22:37:42

您的问题是,str(long)对于Python语言中的大整数(数百万位)来说非常慢。It is a quadratic operation (in number of digits) in Python,即对于~1e8位数字,可能需要~1e16操作才能将整数转换为十进制字符串。

写入文件500MB不应花费数小时,例如:

代码语言:javascript
复制
$ python3 -c 'open("file", "w").write("a"*500*1000000)'

几乎立即返回。ls -l file确认文件已创建且大小符合预期。

计算math.factorial(67867957) (结果有大约500M位)可能需要几个小时,但使用pickle保存它是瞬间的:

代码语言:javascript
复制
import math
import pickle

n = math.factorial(67867957) # takes a long time
with open("file.pickle", "wb") as file:
    pickle.dump(n, file) # very fast (comparatively)

使用n = pickle.load(open('file.pickle', 'rb'))重新加载它只需要不到一秒钟的时间。

str(n)仍然在我的机器上运行( 50小时后)。

要快速获得小数表示法,您可以使用use gmpy2

代码语言:javascript
复制
$ python -c'import gmpy2;open("file.gmpy2", "w").write(str(gmpy2.fac(67867957)))'

在我的机器上只需要不到10分钟。

票数 3
EN

Stack Overflow用户

发布于 2015-02-10 06:52:59

好的,这真的不是一个答案,它更多的是为了证明你的理由是错误的

首先测试大字符串的写入速度

代码语言:javascript
复制
 import timeit
 def write_big_str(n_bytes=1000000):
     with open("test_file.txt","wb") as f:
          f.write("a"*n_bytes)
 print timeit.timeit("write_big_str()","from __main__ import write_big_str",number=100)

你应该看到一个相当不错的速度(也就是重复100次)

接下来,我们将看到将一个非常大的数字转换为字符串需要多长时间

代码语言:javascript
复制
import timeit,math
n = math.factorial(200000)
print timeit.timeit("str(n)","from __main__ import n",number=1)

它可能需要大约10秒(这是一个百万位数字),这当然是慢的……但不是很慢(好吧,转换成字符串相当慢:p…但仍然不应该花几个小时)(我想我的盒子花了243秒:P)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28420541

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档