Writing huge strings in python

后端 未结 2 1123
离开以前
离开以前 2020-12-04 00:50

I have a very long string, almost a megabyte long, that I need to write to a text file. The regular

file = open(\"file.txt\",\"w\")
file.write(string)
file.c         


        
2条回答
  •  情歌与酒
    2020-12-04 01:29

    Your issue is that str(long) is very slow for large intergers (millions of digits) in Python. It is a quadratic operation (in number of digits) in Python i.e., for ~1e8 digits it may require ~1e16 operations to convert the integer to a decimal string.

    Writing to a file 500MB should not take hours e.g.:

    $ python3 -c 'open("file", "w").write("a"*500*1000000)'
    

    returns almost immediately. ls -l file confirms that the file is created and it has the expected size.

    Calculating math.factorial(67867957) (the result has ~500M digits) may take several hours but saving it using pickle is instantaneous:

    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)
    

    To load it back using n = pickle.load(open('file.pickle', 'rb')) takes less than a second.

    str(n) is still running (after 50 hours) on my machine.

    To get the decimal representation fast, you could use gmpy2:

    $ python -c'import gmpy2;open("file.gmpy2", "w").write(str(gmpy2.fac(67867957)))'
    

    It takes less than 10 minutes on my machine.

提交回复
热议问题