RAMdisk比磁盘慢?

问题描述:

我创建的python程序受IO限制.大多数时间(超过90%)都花在一个循环中,该循环重复约10,000次.在此循环中,将生成约100KB的数据并将其写入一个临时文件.然后,它会被另一个程序和收集到的有关该数据的统计信息读回.这是将数据传递到第二个程序的唯一方法.

A python program I created is IO bounded. The majority of the time (over 90%) is spent in a single loop which repeats ~10,000 times. In this loop, ~100KB data is generated and written to a temporary file; it is then read back out by another program and statistics about that data collected. This is the only way to pass data into the second program.

由于这是主要瓶颈,所以我认为将临时文件的位置从我的主HDD移至(〜40MB)RAMdisk(超过2GB的可用RAM)会大大提高此文件的IO速度从而减少了运行时间.但是,我获得了以下结果(每个结果平均超过20次运行):

Due to this being the main bottleneck, I thought that moving the location of the temporary file from my main HDD to a (~40MB) RAMdisk (inside of over 2GB of free RAM) would greatly increase the IO speed for this file and so reduce the run-time. However, I obtained the following results (each averaged over 20 runs):

  • 测试数据1:不带RAMdisk-72.7s,带RAMdisk-78.6s
  • 测试数据2:不带RAMdisk-223.0s,带RAMdisk-235.1s

RAMdisk似乎比我的HDD慢.

It would appear that the RAMdisk is slower that my HDD.

这可能是什么原因造成的?

What could be causing this?

除了使用RAMdisk以获得更快的文件IO之外,还有其他选择吗?

Are there any other alternative to using a RAMdisk in order to get faster file IO?

您的操作系统几乎可以肯定已经在缓存/缓存磁盘写入.毫不奇怪,RAM磁盘的性能如此之高.

Your operating system is almost certainly buffering/caching disk writes already. It's not surprising the RAM disk is so close in performance.

在不完全知道您在写什么或如何写的情况下,我们只能提供一般性建议.一些想法:

Without knowing exactly what you're writing or how, we can only offer general suggestions. Some ideas:

  • 如果您有2 GB RAM,则可能有一个不错的处理器,因此您可以将此数据写入具有压缩功能的文件系统.假设您的数据适合此操作,那么这将以I/O操作换取CPU时间.

  • If you have 2 GB RAM you probably have a decent processor, so you could write this data to a filesystem that has compression. That would trade I/O operations for CPU time, assuming your data is amenable to that.

如果您要进行许多小写操作,请将它们组合在一起一次写更大的部分. (我们可以看到源代码吗?)

If you're doing many small writes, combine them to write larger pieces at once. (Can we see the source code?)

使用后是否要删除100 KB文件?如果不需要它,则将其删除.否则,操作系统可能被迫将其刷新到磁盘.

Are you removing the 100 KB file after use? If you don't need it, then delete it. Otherwise the OS may be forced to flush it to disk.