2013-08-03 38 views
14

Tôi đang triing để có được thời gian thực hiện chức năng async. Dường như tôi có thể sử dụng process.hrtime cho việc này. Tôi tạo ra ví dụ đơn giản:cách sử dụng process.hrtime để nhận thời gian thực hiện chức năng async

console.log("starting"); 
var start = process.hrtime(); 
console.log("start"); 
console.log(start); 

setTimeout(function(){ 
    console.log("HELLO"); 
    var end = process.hrtime(); 
    console.log("end"); 
    console.log(end); 
}, 1000); 

Nó ra

starting 
start 
[ 131806, 731009597 ] 
HELLO 
end 
[ 131807, 738212296 ] 

Nhưng tôi không hiểu đâu là thời gian exectuion trong miliseconds? Tôi mong đợi để có được 1000 ms trong ví dụ này.

Trả lời

27

Got it:

console.log("starting"); 
var start = process.hrtime(); 
console.log("start"); 
console.log(start); 

setTimeout(function(){ 
    console.log("HELLO"); 
    var end = process.hrtime(start); 
    console.log("end"); 
    console.log(end); 
}, 1000); 

Prints

starting 
start 
[ 132798, 207101051 ] 
HELLO 
end 
[ 1, 7001730 ] 

Điều đó có nghĩa 1 giây và 7.001.730 nano giây từ lúc bắt đầu đến kết thúc

+1

Cảm ơn! Điều này đã giúp! – Tyguy7

Các vấn đề liên quan