如何在Racket中将巨大的文件加载到字符串或列表中? [英] How to load a huge file into a String or list in Racket?
问题描述
我有一个巨大的文件,需要对其进行操作.巨大如约.五百万个单词.
I have a HUGE file that I need to do operations on. Huge as in approx. half a million words.
我只想将其读取到列表或字符串中,以便稍后使用它.
I just want to read it into a list or String so I can do things with it later.
我也知道我可以使用file-> string或使用file-> list,file-> lines将其加载到字符串中,但是这些似乎花费的时间太长了.
Also I know I could load it into a string using file->string OR use file->list, file->lines, but these seem to take waayy too long.
将其加载到列表中是否正确?
Is this the right way to load it into a list?:
(define my-list (with-input-from-file "myFile.txt" read))
每当我运行程序时,我都会打印出第一行.似乎适用于较小的文件.
Whenever I run my program I just get the first line printed out. Seems to work for smaller files though.
推荐答案
我强烈感觉到您的问题不是将字符串读入,而是将其打印出来.
I have a strong feeling that your problem isn't reading the string in, but rather printing it out.
具体来说,读取这种大小的文件似乎要花费我大约0.03秒的时间.
Specifically, reading a file of this size appears to take me approximately 0.03 seconds.
我使用此程序生成了一个文件:
I generated a file using this program:
#lang racket
(define str
"Beebe a reeble to one niner big druppy bonker watz. ")
(with-output-to-file "/tmp/foo.txt"
(λ ()
(for ([i (in-range (/ 500000 10))])
(displayln str))))
然后,我这样阅读:
#lang racket
(define a (time (file->string "/tmp/foo.txt")))
...并产生以下输出:
... and produced this output:
cpu time: 30 real time: 30 gc time: 17
....表示30毫秒.
.... Indicating 30 milliseconds.
请注意,因为我将file->string
包裹在define
中,所以不是将整个内容打印出来.这将花费很长时间.
Note that because I wrapped the file->string
in a define
, I was not printing the whole thing out. That would take a long long time.
这篇关于如何在Racket中将巨大的文件加载到字符串或列表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!