0

I need to use web workers for a demanding computation. I have const t0 = performance.now() and const t1 = performance.now() respectively at the beginning and the end of my code. I want to report t1 - t0 as the time required to perform the computation. But if the web worker is actually running in another thread, does it mean that it can be interrupted by some scheduler ? If yes, then it means that t1 - t0 could be greater than the actual computation time (like this: timeline)... How would you measure execution time in a multi threaded context, in javascript ? Thanks in advance to anyone willing to help me understand.

rucquoy
  • 5
  • 2
  • I can only advise you to only start profiling once you know that performance is below requirements. – Tomáš Zato Apr 17 '20 at 17:11
  • Thank you ! But there is no "requirement" strictly speaking, this is for a benchmarking app where i need to report execution time for specific tasks. – rucquoy Apr 18 '20 at 06:52
  • For benchmarking: measure multiple times, take minimum. Yes, system load will always affect your timings. There's no way to really avoid this. – Bergi Apr 19 '20 at 15:35

1 Answers1

0

The DOMHighResTimeStamp API uses global monotonic clocks which should never be paused or even throttled.

In certain scenarios (e.g. when a tab is backgrounded), the user agent may choose to throttle timers and periodic callbacks run in that context or even freeze them entirely. Any such throttling should not affect the resolution or accuracy of the time returned by the monotonic clock.

So you can be confident that performance.now() will return the correct time since the Worker was created, as long as the browser itself is still running. Indeed, for instance if the computer is set to sleep, then the clocks won't tick anymore.

Kaiido
  • 123,334
  • 13
  • 219
  • 285
  • OP is concerned with `performance.now` measuring wall clock time instead of cpu time. Your answer only shows that it does indeed measure wall clock time. – Bergi Apr 19 '20 at 15:33
  • @Bergi what makes you think so? They said they want to use it as a stop watch, nothing about a "wall clock" And my answer points to the flows of this method: timestamp is relative to the creation time of the worker, setting the computer to sleep will stop the clock (but the computation too, so that shouldn't be a problem). – Kaiido Apr 19 '20 at 23:10
  • "*should not affect the resolution or accuracy of the time*" does not sound like it's supposed to stop the clock when the thread sleeps – Bergi Apr 20 '20 at 07:09
  • @Bergi I said when the computer goes to sleep. [Here is a related Q/A](https://stackoverflow.com/questions/60753888/how-to-restart-web-workers-when-computer-restarts-from-sleep-mode/60846073#60846073) if you are interested, check the comments where I mislead the OP to using performance.now before I realize it will actually get stopped when the computer sleeps. Date is the only clock that doesn't suffer from that, but it can be changed while running... – Kaiido Apr 20 '20 at 07:17