我目前正在编写一段简单的IO解析,并且在如何编写它的问题上进退两难。
这就是web应用程序的情况,其中这个特定的解析函数可能会在一秒钟内被多个用户多次调用。
假设文件大小大于2MB,每次调用的硬件IO延迟为5ms。
第一种情况:内存
第一种情况是为内存编写代码,但以速度为代价。该函数将接受文件的一小部分,并按部分进行解析,从而使用更多的迭代,但占用的内存更少。
伪代码:
function parser() {
Open file and put into handle variable fHandle
while (file position not passed EOF) {
read 1024 bytes from file using fHandle into variable data
process(data)
}
Close file using handle fHandle
}第二种情况:速度
第二种情况是为了提高速度而编写代码,但代价是内存使用。该函数会将整个文件内容加载到内存中,并直接进行解析。
伪代码:
function parser() {
read entire file and store into variable data
declare parsing position variable and set to 0
while (parsing position not past data length) {
get position of next token and store into variable pos
process( substring from current position to pos of data )
}
}注意:当读取整个文件时,我们使用库直接可用的函数来读取整个文件。在开发人员端读取文件时不使用循环。
第三种情况:最终用户选择
那么为这两个函数编写代码是否可取,并且每当函数运行时,该函数将检测内存是否充足。如果有大量的空闲内存空间,函数将使用内存密集型版本。
伪代码:
function parser() {
if (memory is too little) {
Open file and put into handle variable fHandle
while (file position not passed EOF) {
read 1024 bytes from file using fHandle into variable data
process(data)
}
Close file using handle fHandle
} else {
read entire file and store into variable data
declare parsing position variable and set to 0
while (parsing position not past data length) {
get position of next token and store into variable pos
process( substring from current position to pos of data )
}
}
}发布于 2011-12-04 10:49:47
使用异步I/O (或第二个线程),在驱动器忙于获取下一个数据块时处理一个数据块。两全其美。
发布于 2011-12-04 10:50:05
如果您需要以任何一种方式读取整个文件,并且它可以毫无问题地装入内存,那么就从内存中读取它。它是每次都是同一个文件,还是一小部分文件?将它们缓存到内存中。
发布于 2011-12-04 10:50:35
如果解析的输入来自I/O,那么任何好的解析技术,比如递归下降,都会受到I/O的限制。换句话说,从I/O获取字符的平均时间应该超过处理该字符的平均时间,这是一个健康的因素。所以这真的无关紧要。唯一的区别将是你有多少工作存储,这通常不是什么大问题。
https://stackoverflow.com/questions/8372848
复制相似问题