我正在尝试将名为Memored的库的读取性能与Node.js中的常规旧RAM变量进行比较。
我预计使用 Memored 存储的数据在读取数据方面至少比 RAM 存储稍慢,但结果显示相反(请阅读下面的输出(。
我在Windows 10上的Visual Studio Code终端中运行它。这一切都是在Typescript中完成的,稍后将其编译为JavaScript,然后使用"node"命令运行。
这是我的RAM测试:
var normalRAM = {
firstname: 'qwe',
lastname: 'fsa'
}
var s = process.hrtime(); //start timer
console.log(normalRAM); // read from ram
var e = process.hrtime(s) //stop timer
console.log("end0", e[0]); //results in seconds
console.log("end1", e[1]); //results in nanoseconds
这是我的记忆测试:
// Clustering needed to show Memored in action
if (cluster.isMaster)
{
// Fork workers.
for (let i = 0; i < 1; i++)
{
cluster.fork();
}
}
else
{
var han = {
firstname: 'Han',
lastname: 'Solo'
}
// Store and read
memored.store('character1', han, function ()
{
console.log('Value stored!');
var hrstart = process.hrtime(); // start timer
memored.read('character1', function (err: any, value: any)
{
var hrend = process.hrtime(hrstart) // stop timer
console.log('Read value:', value);
console.log("hrend0", hrend[0]); //results in seconds
console.log("hrend1", hrend[1]); //results in nanoseconds
});
});
}
结果:
The RAM read speeds are around 6500000 nanoseconds.
The Memored read speeds are around 1000000 nanoseconds
我在这里测试的速度不正确吗?我的方法有哪些缺陷?也许我最初的假设是错误的?
我切换了以下两行:
var hrend = process.hrtime(hrstart) // stop timer
console.log('Read value:', value);
对此:
console.log('Read value:', value);
var hrend = process.hrtime(hrstart) // stop timer
这在真实场景中更有意义,因为在返回数据后,无论如何我都需要像这样从 RAM 读取它。我的问题的答案可能是"您的 Memored 测试执行得更快,因为它仅在数据返回供我的回调使用时才进行测试,而不是当我实际从'value'变量读取它时"。