问题
I am having a problem measuring the time elapsed executing my code in Tensorflow.js
.
I was building a super resolution model in tfjs
, and I wanted to know the latency. currently I am using the following method, as described below.
But I don't think this is the right way to it. Any kind of suggestion would be appreciated.
var w = new Date();
var r = w.getTime();
for (var q = 0; q<29; q++){
var x = tf.concat([new_I[q++],new_I[q++], new_I[q++],new_I[q]]);
var hr_image = await pretrainedModel.execute(x);
var y = new Date();
var u = y.getTime();
console.log(u-r);
}
Which one to use among tf.time
, Date
... ?
回答1:
Date, tf.time, performance
Date
can be used to indicate the time taken to execute a function.
a = new Date().getTime()
// execute function f()
b = new Date().getTime()
// executed time in milliseconds b - a
The caveat is that this function is affected by the time of the system
tf.Time()
give more details regarding the time taken to execute a function. It outputs the time taken by the backend to read the data and the wall time. It can be useful to know what tfjs is doing internally.
tf.time(() => f() ) // execute f as callback
Using performance
a = performance.now()
// execute f()
b = performance.now()
// executed time in milliseconds b - a
It has been implemented to evaluate the execution timing. Unlike Date, it is independent of the system clock.
Measure execution time
Coming to the code of the question, it is unclear what performance is measured. If the goal is to measured the overall performance of the code,
var w = new Date();
var r = w.getTime();
for (var q = 0; q<29; q++){
var x = tf.concat([new_I[q++],new_I[q++], new_I[q++],new_I[q]]);
var hr_image = await pretrainedModel.execute(x);
}
var y = new Date();
var u = y.getTime();
console.log(u-r);
If the goal is to measure the time taken for each prediction
for (var q = 0; q<29; q++){
var w = new Date();
var r = w.getTime();
var x = tf.concat([new_I[q++],new_I[q++], new_I[q++],new_I[q]]);
var hr_image = await pretrainedModel.execute(x);
var y = new Date();
var u = y.getTime();
console.log(u-r);
}
But as explain above using performance.now()
is better than using Date
来源:https://stackoverflow.com/questions/57853386/problem-measuring-the-time-elapsed-executing-my-code-in-tensorflow-js