我的函数基准:
mark@ichikawa:~/inbox/D3/read_logs$ time python countbytes.py
bytes: 277464
real 0m0.037s
user 0m0.036s
sys 0m0.000s
mark@ichikawa:~/inbox/D3/read_logs$ time node countbytes.js
bytes: 277464
real 0m0.144s
user 0m0.120s
sys 0m0.032s
测量是在Ubuntu 13.04 x86_64位机器上进行的。
这是我的基准测试的简单版本(我也做了1000次迭代)。I显示了我写的读取tgz文件的函数所花费的时间是我用Python编写的函数的3倍多。
对于1000次迭代,文件大小为277kB(我使用process。Hrtime和timeit):
Node: 30.608409032000015
Python: 6.84210395813
对于1000次迭代大小为9.7MB:
Node: 590.491709309999
Python: 200.796745062
如果你有任何关于如何加快阅读tgz文件的想法,请告诉我。
下面是代码:var fs = require('fs');
var tar = require('tar');
var zlib = require('zlib');
var Stream = require('stream');
var countBytes = new Stream;
countBytes.writable = true;
countBytes.count = 0;
countBytes.bytes = 0;
countBytes.write = function (buf) {
countBytes.bytes += buf.length;
};
countBytes.end = function (buf) {
if (arguments.length) countBytes.write(buf);
countBytes.writable = false;
console.log('bytes: ' + countBytes.bytes);
};
countBytes.destroy = function () {
countBytes.writable = false;
};
fs.createReadStream('supercars-logs-13060317.tgz')
.pipe(zlib.createUnzip())
.pipe(tar.Extract({path: "responsetimes.log.13060317"}))
.pipe(countBytes);
你知道怎么加快速度吗?
我看起来不错,但我很好奇为什么使用tar
流?
我将使用Transform
来实现countBytes。我喜欢你用through2
var fs = require('fs')
, tar = require('tar')
, zlib = require('zlib')
, thr = require('through2')
, cache = {bytes: 0}
;
fs.createReadStream('supercars-logs-13060317.tgz')
.pipe(zlib.createUnzip())
.pipe(thr(function(chunk, enc, next){
cache.bytes += chunk.length
next(null, chunk)
}))
.on('end', function(){
console.log(cache.count)
})