Jump to content
Eyad Syria-lover

How Can We Calculate The Elapsed Time For A Function To Be Executed?

Recommended Posts

If you care about compatibility with older browsers, you can use Date() objects. It has millisecond precision, but you can benchmark code by running it thousands of times.

var iterations = 100000;

var start = (new Date()).getTime();
for(var i = 0; i < iterations; i++) {
  // Put code here for benchmarking
}
var end = (new Date()).getTime();

// Find out how long it took
var elapsedTime = end - start;

// Find out how much time passes for just one function call
var averageTime =  elapsedTime / iterations;

// Output data
console.log(averageTime);

Share this post


Link to post
Share on other sites

If you care about compatibility with older browsers, you can use Date() objects. It has millisecond precision, but you can benchmark code by running it thousands of times.

var iterations = 100000;

var start = (new Date()).getTime();
for(var i = 0; i < iterations; i++) {
  // Put code here for benchmarking
}
var end = (new Date()).getTime();

// Find out how long it took
var elapsedTime = end - start;

// Find out how much time passes for just one function call
var averageTime =  elapsedTime / iterations;

// Output data
console.log(averageTime);

This Is Taking More Than Expected Because The Iteration Is Very Big,Do You Have Another Way Sir?

Share this post


Link to post
Share on other sites

It's Returning So Many Numbers After Decimal...

So, what's the problem? Most floating-point numbers in Javascript aren't going to be precise after a few decimals, so if the precision of that is supposed to be to 3 decimal places then you can drop the other digits.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...