What is the best way to measure how long code takes to execute?

后端 未结 10 1247
梦谈多话
梦谈多话 2020-12-08 04:04

I\'m trying to determine which approach to removing a string is the fastest.

I simply get the start and end time a

10条回答
  •  遥遥无期
    2020-12-08 04:46

    The trouble with all the clock-based approaches is that you are never quite certain what you are timing. You might, whether you realise it or not, include in your timing:

    • delays while the o/s pre-empts your processor;
    • context-switching
    • stalls while the program waits for data;
    • and much much more.

    I'm not saying that all of these apply to this particular timing but to timings in general. So you should supplement any timing you do with some consideration of how many basic operations your alternative codes execute -- some considerations of complexity not ignoring (as we usually do) the constant terms.

    In your particular case you should aim to time much longer executions; when your times are sub-second the o/s is extremely likely to mess you around. So run 10^6 iterations and use the average over enough runs to give you a meaningful calculation of average and variance. And make sure, if you take this approach, that you don't inadvertently speed up the second trial by having data already loaded after the end of the first trial. You have to make sure that each of the 10^6 trials does exactly what the 1st trial does.

    Have fun

    Mark

提交回复
热议问题