c# - How robust is repeating a piece of code to measure it's execution time? -
let's want measure how long myfunction()
executes.
let's myfunction()
executes fast (a few milliseconds) , stopwatch cannot measure function time enough precision.
i put myfunction()
inside of loop executes 1,000,000 times , measure that. time execution of loop , divide result number of times loop executed.
the questions are:
how accurate measurement? want know how long first execution of
myfunction()
takes, not how long average takes.does time executing same function scale linearly? depend on properties of
myfunction()
(so kind of caching used)? properties?
let's run myfunction()
1,000,000 times in 3 seconds.
you know:
you can @ least run
myfunction()
in time less or equal 3μs, on similar data , similar cache , jitter situations.it runs faster equivalent method takes 3.5 seconds, long tests equivalent. (but 3.01 close sure unless repeats consistently).
it runs slower equivalent method takes 2.5 seconds, long tests equivalent. (but 2.99 close sure unless repeats consistently).
you not know:
how time affected on first call jitting, static constructors, , memoisation of values either static or related particular values used.
how time affected method still being accessible instruction cache (or indeed, whether is).
how time affected data used still being accessible data cache (or indeed, whether is).
how consistent time subsequent first run; steady 3μs per run, or more 1μs occasional large delays?
just portion of time spent on looping rather running.
these 5 things don't know (or 4 if run prior loop @ least reduce impact of first one) mean don't know awful lot in absolute sense.
but 3 things know can useful relative other possible approaches, or if you're above or below threshold (if need beat 2μs you're screwed, if need beat 500ms you're fine, if need consistently beat 4μs don't know if have).
Comments
Post a Comment