0
votes

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
You should change your title to something a bit more explicit (stackoverflow.com/help/how-to-ask) - Seblor
What is the question here? Why do you think your code is not the right way to do it? Your code looks okay to me as it is. - Thomas Dondorf

1 Answers

0
votes

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