简体   繁体   English

将大数据写入.csv文件时,经常打开和关闭文件更好吗?

[英]When writing large data into .csv file, is it better to open and close file often?

I am writing a program with a while loop, which would write giant amount of data into a csv file. 我正在编写带有while循环的程序,该程序会将大量数据写入csv文件。 There maybe more than 1 million rows. 可能有超过一百万行。

Considering running time, memory usage, debugging and so on, what is the better option between the two: 考虑到运行时间,内存使用情况,调试等,两者之间的更好选择是什么:

  1. open a CSV file, keep it open and write line by line, until the 1 million all written 打开一个CSV文件,保持打开状态并逐行写入,直到写入100万

  2. Open a file, write about 100 lines, close(), open again, write about 100 lines, ...... 打开一个文件,写大约100行,close(),再次打开,写大约100行,......

I guess I just want to know would it take more memories if we're to keep the file open all the time? 我想我只是想知道,如果要一直保持打开文件的状态,会需要更多的记忆吗? And which one will take longer? 哪一个需要更长的时间?

I can't run the code to compare because I'm using a VPN for the code, and testing through testing would cost too much $$ for me. 我无法运行代码进行比较,因为我正在使用VPN作为代码,而通过测试进行测试对我来说会花费太多。 So just some rules of thumb would be enough for this thing. 因此,仅凭一些经验法则就足够了。

I believe the write will immediately write to the disk, so there isn't any benefit that I can see from closing and reopening the file. 我相信写入操作会立即写入磁盘,因此关闭和重新打开文件没有任何好处。 The file isn't stored in memory when it's opened, you just get essentially a pointer to the file, and then load or write a portion of it at a time. 该文件在打开时并未存储在内存中,实际上您只是获得了一个指向该文件的指针,然后一次加载或写入该文件的一部分。

Edit 编辑

To be more explicit, no, opening a large file will not use a large amount of memory. 更明确地说,不,打开大文件不会占用大量内存。 Similarly writing a large amount of data will not use a large amount of memory as long as you don't hold the data in memory after it has been written to the file. 同样,只要在将数据写入文件后不将其保留在内存中,则写入大量数据也不会占用大量内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM