2013-08-03 19 views
14

Próbuję uzyskać czas wykonania funkcji asynchronicznej. Na pozór mogę użyć do tego celu process.hrtime. Stworzyłem prosty przykład:jak używać process.hrtime, aby uzyskać czas wykonania funkcji asynchronicznej

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); 

przesyła je

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

Ale ja nie rozumiem, gdzie jest czas exectuion w milisekundach? Spodziewam się uzyskać 1000 ms w tym przykładzie.

Odpowiedz

27

Got go:

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); 

Drukuje

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

Oznacza to 1 sekunda i 7001730 nanosekund od początku do końca

+1

Dzięki! To pomogło! – Tyguy7

Powiązane problemy