我正在使用Node.js
来读取和解析编码数字的对的文件。我有这样一个文件:
1561 0506
1204 900
6060 44
我希望将其作为数组读取,如下所示:
[[1561,0506],[1204,900],[6060,44]]
为此,我使用readStream
,将文件读取为块并使用本机字符串函数进行解析:
fileStream.on("data",function(chunk){
var newLineIndex;
file = file + chunk;
while ((newLineIndex = file.indexOf("\n")) !== -1){
var spaceIndex = file.indexOf(" ");
edges.push([
Number(file.slice(0,spaceIndex)),
Number(file.slice(spaceIndex+1,newLineIndex))]);
file = file.slice(newLineIndex+1);
};
});
虽然这已经很多次了(我机器上需要4s的文件)。我看到了一些原因:
我在没有使用内置字符串函数的情况下重写了算法,而是循环而且,令我惊讶的是,它变得慢得多!有没有办法让它更快?
答案 0 :(得分:0)
警告:我没有测试过这个解决方案的性能,但它已经完成,所以应该很容易尝试。
如何根据this liner implementation中的注释使用this question。
使用衬垫:
var fs = require('fs')
var liner = require('./liner')
var source = fs.createReadStream('mypathhere')
source.pipe(liner)
liner.on('readable', function () {
var line
while (line = liner.read()) {
var parts = line.split(" ");
edges.push([Number(parts[0]), Number(parts[1])]);
}
})
正如您所看到的,我还将边缘数组移动为与分割部分分开的内联常量大小的数组,我猜这会加快分配。您甚至可以尝试使用indexOf(“”)而不是split(“”)进行交换。
除此之外,您可以检测代码以识别任何进一步的瓶颈。