Rhino是否像Node.js一样自动优化脚本?

时间:2019-06-25 07:07:43

标签: javascript node.js regex performance rhino

我想测试搜索文本的不同方法及其对时间的影响。 为此,我这样写:

X_new = X_train.reshape((-1, 28, 28))

我想在许多试验中进行此测试,并取平均值以获得更一致的结果,但意识到节点会自动优化这种情况。

1次试用的结果(以毫秒为单位): // test case 1: no regex instance & regex.exec let t1total = 0; for (let j = 0; j < trialCount; j++){ let start = performance.now(); for (let i = 0; i < splitLog.length; i++) { /^([A-Z]+)[^\d]+([\d\/\-.:]+\sUTC)/.exec(splitLog[i]); } let end = performance.now(); t1total += end - start } t1total /= trialCount; // test case 2: pre-compile + regex.exec let t2total = 0; let compileStart = performance.now(); const preRegex = new RegExp(/^([A-Z]+)[^\d]+([\d\/\-.:]+\sUTC)/); let compileEnd = performance.now(); t2total += compileEnd - compileStart; for (let j = 0; j < trialCount; j++){ let start = performance.now(); for (let i = 0; i < splitLog.length; i++) { preRegex.exec(splitLog[i]); } let end = performance.now(); t2total += end - start } t2total /= trialCount;

1000次试验的结果(以毫秒为单位): Test 1: no regex instance + regex.exec 9.151600003242493 Test 2: pre-compile + regex.exec 4.707100033760071

因此,当重复创建相同的正则表达式时,节点将对自身进行优化。

现在想象一下,有一个脚本,其中的exec在尚未实例化的正则表达式上仅被调用一次。 Rhino重复调用这样的脚本是否会像运行测试用例1这样的循环那样在节点上进行类似的优化?

换句话说,Rhino是否优化重复调用实例化正则表达式的脚本,就像nodejs如何重复优化实例化同一个正则表达式?

1 个答案:

答案 0 :(得分:1)

我运行了一些测试来尝试解决这个问题,而且似乎确实存在类似的自动优化,但是不同方法之间存在明显的差异-与我使用Node.js的结果不同。

Trials : 1
Lines: 35629
Regex compile time:  111859500

no instance + exec:  196013300
precompile + exec:   127519700
no instance + match: 116066300
precompile + match:  68303500
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Trials: 1000
Lines: 35629
Regex compile time:  99829400

no instance + exec:  40101506
precompile + exec:   37426255
no instance + match: 45371233
precompile + match:  44917744

奇怪的是,我的结果确实发生了很大变化,这取决于进行了多少次试验。另外,重新实例化脚本似乎会中断此优化。完整的结果可以在here中找到。

我通过在脚本中编译正则表达式然后将其拉出到Java对象中来创建了“预编译”正则表达式。然后,我将该对象作为参数传递给需要已编译的正则表达式进行搜索的脚本。

注意:结果仅对运行搜索脚本的时间进行计时,并且不包括创建正则表达式或脚本本身的时间。

相关问题