Python-文件处理-内存错误-加快性能

标签 python performance python-3.4 file-processing

我正在处理大量数据。我必须将它们写入 .txt 文件。现在我必须将 1000000,10000000(1M-1B) 之间的所有数字写入 .txt 文件。因为如果我在单个列表中执行它,它会引发内存错误,所以我将它们切片(我不喜欢这个解决方案,但找不到任何其他解决方案)。

问题是,即使有前 50M 个数字(1M-50M),我什至无法打开 .txt 文件。它有 458MB,大约需要 15 分钟,所以我猜如果我写下所有数字,它会是一个 9GB 的 .txt 文件,需要 4 个小时左右。

当我尝试打开包含 1M-50M 之间数字的 .txt 文件时

myfile.txt has stopped working

所以现在文件包含1M-50M之间的数字,我什至无法打开它,我想如果我写下所有数字,就无法打开。

我现在必须在 1M-1B 之间打乱数字并将这些数字存储到 .txt 文件中。基本上这是一个自由职业,我必须处理更大的数字,比如100B等。即使是前50M也有这个问题,当数字更大时我不知道如何完成。

这里是1M-50M的代码

import random

x = 1000000
y = 10000000


while x < 50000001:
    nums = [a for a in range(x,x+y)]
    random.shuffle(nums)
    with open ("nums.txt","a+") as f:
        for z in nums:
            f.write(str(z)+"\n")
        x += 10000000

How can I speed up this process?

How can I open this .txt file, should I create new file every time? If I choose this option I have to slice the numbers more since even 50M numbers has problem.

Is there any module can you suggest may be useful for this process?

最佳答案

Is there any module can you suggest may be useful for this process?

使用Numpy对于处理大型数组确实很有帮助。

How can I speed up this process?

使用 Numpy 的函数 arangetofile大大加快了这个过程(见下面的代码)。初始数组的生成速度大约快 50 倍,将数组写入文件大约快 7 倍。

该代码仅执行每个操作一次(将 number=1 更改为更高的值以获得更好的精度),并且仅生成最多 1M 到 2M 之间的数字,但您可以看到总体情况。

import random
import timeit
import numpy

x = 10**6
y = 2 * 10**6

def list_rand():
    nums = [a for a in range(x, y)]
    random.shuffle(nums)
    return nums

def numpy_rand():
    nums = numpy.arange(x, y)
    numpy.random.shuffle(nums)
    return nums

def std_write(nums):
    with open ('nums_std.txt', 'w') as f:
        for z in nums:
            f.write(str(z) + '\n')

def numpy_write(nums):
    with open('nums_numpy.txt', 'w') as f:
        nums.tofile(f, '\n')

print('list generation, random [secs]')
print('{:10.4f}'.format(timeit.timeit(stmt='list_rand()', setup='from __main__ import list_rand', number=1)))

print('numpy array generation, random [secs]')
print('{:10.4f}'.format(timeit.timeit(stmt='numpy_rand()', setup='from __main__ import numpy_rand', number=1)))

print('standard write [secs]')
nums = list_rand()
print('{:10.4f}'.format(timeit.timeit(stmt='std_write(nums)', setup='from __main__ import std_write, nums', number=1)))

print('numpy write [secs]')
nums = numpy_rand()
print('{:10.4f}'.format(timeit.timeit(stmt='numpy_write(nums)', setup='from __main__ import numpy_write, nums', number=1)))



list generation, random [secs]
    1.3995
numpy array generation, random [secs]
    0.0319
standard write [secs]
    2.5745
numpy write [secs]
    0.3622

How can I open this .txt file, should I create new file every time? If I choose this option I have to slice the numbers more since even 50M numbers has problem.

这实际上取决于您想要如何处理这些数字。找到他们的相对位置?从列表中删除一个?恢复阵列?

关于Python-文件处理-内存错误-加快性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37379187/

相关文章:

python - Keras 中的 LSTM 始终返回相同的输出

python - 如果在 python 脚本中没有指定,默认的 shebang 是什么?

android - 在 Android 中绘制(过滤)100k+ 点到 MapView

c# - 在 ASP.Net 中,了解和分析性能的最佳方法是什么?

android - 无法使用改造 android 库在编译时出错

python - Django channel 中的多个 websocket.receive

python - 如何在boto3中为AWS EC2实例设置标签

python - 如何使用 pyglet 提高批处理的性能?

Python open() 需要完整路径

python - 为什么 asyncio 的事件循环会抑制 Windows 上的 KeyboardInterrupt?