簡體   English   中英

關於performance.now()來測試函數的執行時間

[英]about performance.now() to test function execute time

我目前正在創建一個簡短的javascript演示,僅用於檢查功能性能目的。 代碼如下:

 (function(){ 'use strict'; var getFunctionExecuteTime = function(myFoo) { if(typeof myFoo !== 'function'){ return -1; }else{ var t1 = performance.now(); myFoo(); var t2 = performance.now(); return t2-t1; } }; var foo = function() { console.log('running.'); }; function foo2(){ console.log('running by foo2.'); } console.log('function foo2 spent time: ', getFunctionExecuteTime(foo2)); console.log('function foo spent time: ', getFunctionExecuteTime(foo)); console.log('function --- spent time: ', getFunctionExecuteTime(function(){ console.log('running.'); })); })(); 

我想測試的這三個函數,它們的執行時間假設接近,但我得到的Chrome控制台的輸出很有趣,如下所示:

由foo2運行。 function foo2花費的時間:5.1000000021304

運行。 function foo花費時間:0.11000000085914508

運行。 功能---花費時間:0.115999995614402

即使我交換了函數的順序,第一個總是花費更多的時間來執行,所以有人可以給我一個暗示真正發生的事情嗎?

快速回答:

這是引擎V8優化Javascript代碼的方式。

  1. 您的函數第一次運行時,Javascript代碼將直接轉換為機器代碼,允許它立即執行。

  2. 調用另一個線程來處理優化步驟。 Javascript代碼現在變成了一個中間字節碼。

  3. 下次調用該函數時,將調用優化代碼。

你可以在這里閱讀更多。

調查步驟:

這是我對Node.js環境的調查:

  1. 如果你更改函數調用的順序如下:
console.log('function foo spent time: ', getFunctionExecuteTime(foo));
console.log('function foo2 spent time: ', getFunctionExecuteTime(foo2));

結果:foo將比foo2運行更長的時間。

running.
function foo spent time:  2.8903000000864267
running by foo2.
function foo2 spent time:  0.10759999975562096
running.
function --- spent time:  0.058200000785291195
  1. 有趣的是,如果你運行這段代碼:
const { performance } = require('perf_hooks');

const perf = () => {
  const t1 = performance.now();
  const t2 = performance.now();

  return t2 - t1;
};

for (let i = 0; i < 10; i++) {
  console.log(`Called ${i + 1} times. Time spent: ${perf()}`);
}

結果:同一函數的第一次調用總是比其他函數執行的時間更長。

Called 1 times. Time spent: 0.4595999997109175
Called 2 times. Time spent: 0.026399999856948853
Called 3 times. Time spent: 0.018099999986588955
Called 4 times. Time spent: 0.0015000002458691597
Called 5 times. Time spent: 0.0015000002458691597
Called 6 times. Time spent: 0.0014000004157423973
Called 7 times. Time spent: 0.0021000001579523087
Called 8 times. Time spent: 0.0034999996423721313
Called 9 times. Time spent: 0.002199999988079071
Called 10 times. Time spent: 0.0027000000700354576
  1. 以下是我想到的兩件事:

    • Node.js的事件循環需要很短的時間來啟動,條件檢查,......
    • 或者它是V8引擎(在Chrome和Node.js中用於編譯Javascript代碼)優化代碼的方式。

    所以,要弄清楚,這里是對上面代碼的一個小修改:


const { performance } = require('perf_hooks');

console.log('Waiting 1...');
console.log('Waiting 2...');
console.log('Waiting 3...');
console.log('Waiting 4...');
console.log('Waiting 5...');

const perf = () => {
  const t1 = performance.now();
  const t2 = performance.now();

  return t2 - t1;
};

for (let i = 0; i < 10; i++) {
  console.log(`Called ${i + 1} times. Time spent: ${perf()}`);
}

結果:

Waiting 1...
Waiting 2...
Waiting 3...
Waiting 4...
Waiting 5...
Called 1 times. Time spent: 0.8381999991834164
Called 2 times. Time spent: 0.00279999990016222
Called 3 times. Time spent: 0.0024000005796551704
Called 4 times. Time spent: 0.0026000002399086952
Called 5 times. Time spent: 0.00279999990016222
Called 6 times. Time spent: 0.0018000006675720215
Called 7 times. Time spent: 0.021200000308454037
Called 8 times. Time spent: 0.001600000075995922
Called 9 times. Time spent: 0.0014000004157423973
Called 10 times. Time spent: 0.001499999314546585
  1. 結論:

    所以,顯然這是V8引擎的工作方式。 你的getFunctionExecuteTime函數是在這里得到優化的東西。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM