[英]Python: Fastest way to write large integer to file
I want to write a really large integer from Python to a textfile, about 10 to 1000 Megabyte.我想写一个非常大的 integer 从 Python 到一个文本文件,大约 10 到 1000 兆字节。
The following options have the same speed, unfortunately both are really slow:以下选项具有相同的速度,不幸的是两者都非常慢:
import time
import pickle
num = 17**(10**7)
t1=time.time()
pickle.dump( num , open( "save2.p", "wb" ) )
t2=time.time()
print(str(t2-t1))
t3=time.time()
file = open("testfile2.txt","w")
file.write(str(num))
file.close()
t4=time.time()
print(str(t4-t3))
(of course, the value of num
is just a placeholder for another big integer) (当然, num
的值只是另一个大整数的占位符)
My questions:我的问题:
Who can help?谁能帮忙?
Try this:尝试这个:
import random
f=open("filename.txt","w")
char=""
ints=["0","1","2","3","4","5","6","7","8","9"]
for i in range(10**10): #Ten to the power of ten
char+=random.choice(ints)
if char.startswith("0"):
char=char[1:]
f.write(char)
f.close()
If you want to read the integer, you write this:如果你想阅读 integer,你可以这样写:
myInt=int(open("filename.txt","r").read())
Hope this helps!希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.