Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Tensorflow.js tf.time() Function

  • Last Updated : 08 Jul, 2021

Tensorflow.js is an open-source library that was developed by Google for running machine learning models as well as deep learning neural networks in the browser or node environment.

The tf.time() function is used to execute the stated function, f() as well as return a promise in order that determines along with timing details. The resultant is an object along with the properties like Wall execution time, Kernel execution time, excluding transfer of data. In case the WebGL backend is used as well as query ticker is unavailable then this method will throw an error object. Moreover, the WebGL backend will provide the further properties like uploadWaitMs i.e. blocking time of CPU on texture uploads, and downloadWaitMs i.e. blocking time of CPU on texture downloads.

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!



Parameters: This function accepts a single parameter as mentioned below:

  • f: It is the stated function to execute as well as time.

Return Value: It returns a Promise of TimingInfo.

Example 1: 


// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Using truncatedNormal() function in order to
// define the parameter for the function used
const p = tf.truncatedNormal([10, 10]);
// Calling time() method and also using
// dot() method
const tm = await tf.time(() =>;
// Printing output
console.log(`Kernel execution time: 
  ${tm.kernelMs}, Wall execution time: ${tm.wallMs}`


Kernel execution time: 0.048498, Wall execution time: 85.19999992847443

Example 2:


// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Calling time() method and also using
// square() as well as ones() method
const t = await tf.time(() => tf.square(tf.ones([15, 21])));
// Printing output for WebGL backend
console.log(`uploadWaitMs: ${t.uploadWaitMs},
  downloadWaitMs: ${t.downloadWaitMs}`);


uploadWaitMs: 1.100000023841858, downloadWaitMs: 0


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!