Tensorflow.js tf.time() Function
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!
Syntax:
tf.time(f)
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:
Javascript
// Importing the tensorflow.js libraryimport * as tf from "@tensorflow/tfjs" // Using truncatedNormal() function in order to// define the parameter for the function usedconst p = tf.truncatedNormal([10, 10]); // Calling time() method and also using// dot() methodconst tm = await tf.time(() => p.dot(p)); // Printing outputconsole.log(`Kernel execution time: ${tm.kernelMs}, Wall execution time: ${tm.wallMs}`); |
Output:
Kernel execution time: 0.048498, Wall execution time: 85.19999992847443
Example 2:
Javascript
// Importing the tensorflow.js libraryimport * as tf from "@tensorflow/tfjs" // Calling time() method and also using// square() as well as ones() methodconst t = await tf.time(() => tf.square(tf.ones([15, 21]))); // Printing output for WebGL backendconsole.log(`uploadWaitMs: ${t.uploadWaitMs}, downloadWaitMs: ${t.downloadWaitMs}`); |
Output:
uploadWaitMs: 1.100000023841858, downloadWaitMs: 0
Reference: https://js.tensorflow.org/api/latest/#time


