0

I am a C++ user and starting to branch out a bit. I need to clock how long it takes to do a few binary searches in seconds for a project.

My question is how do I make a stop clock in JavaScript in seconds to see how fast/slow a function runs? What is the time.h equivalent in C++ to JS? I was trying to search on google but kept getting different time/clock results.

Also, how can I refine my search next time so I don't have to resort to having to post? I would like to get better a googling!

Thank you!

pancakes324
  • 11
  • 1
  • 3
  • Does this answer your question? [How to Benchmark Javascript DOM Manipulation](https://stackoverflow.com/questions/16349136/how-to-benchmark-javascript-dom-manipulation) – AKX Sep 29 '20 at 19:39
  • The term you're looking for - figuring out how fast a function runs - is _benchmarking_. I've linked a relevant answer; the gist of it is you assign `const startTime = +new Date();`, do your thing (probably several thousand times), then assign `const endTime = +new Date();`, subtract the two for a duration, and there you have it. – AKX Sep 29 '20 at 19:40
  • `start=new Date(); /* do your stuff */ timeInMilliseconds=Date.now()-start;` – iAmOren Sep 29 '20 at 19:42
  • 1
    Would [console.time](https://developer.mozilla.org/en-US/docs/Web/API/Console/time) be useful? – evolutionxbox Sep 29 '20 at 19:42
  • `Date.now()` or `performance.now()` both return millisecond values. `performance.now()` should theoretically be more precise. Get the value at the start and end, subtract the start from the end. Then format the value as desired (see [How to convert time milliseconds to hours, min, sec format in JavaScript?](https://stackoverflow.com/q/19700283/215552)). – Heretic Monkey Sep 29 '20 at 19:59
  • Does this answer your question? [How to measure time taken by a function to execute](https://stackoverflow.com/questions/313893/how-to-measure-time-taken-by-a-function-to-execute) – Heretic Monkey Sep 29 '20 at 20:00

0 Answers0