excel - High resolution timer/code run time -> overhead? -


i'm trying find code run time using high resolution timer, i've noticed timer has inconsistent results , find out why is.

i found article how test running time of vba code?
, have implemented top answer, tried using find run time of several functions , noticed results changed drastically.

to see if fault of timer, made function started , stopped timer.

public sub test_ctimer()     dim results(0 4) double     dim t ctimer: set t = new ctimer     dim integer      'removes msg box overhead     msgbox "about start"      = 0 4         t.startcounter         results(i) = t.timeelapsed      next      = 0 4         msgbox results(i)     next end sub 

the first measurement takes more time (~ 1 magnitude greater) of following measurements. know why is?

below ctimer code how test running time of vba code?

option explicit  private type large_integer     lowpart long     highpart long end type  private declare function queryperformancecounter lib "kernel32" (lpperformancecount large_integer) long private declare function queryperformancefrequency lib "kernel32" (lpfrequency large_integer) long  private m_counterstart large_integer private m_counterend large_integer private m_crfrequency double  private const two_32 = 4294967296# ' = 256# * 256# * 256# * 256#  private function li2double(li large_integer) double dim low double     low = li.lowpart     if low < 0         low = low + two_32     end if     li2double = li.highpart * two_32 + low end function  private sub class_initialize() dim perffrequency large_integer     queryperformancefrequency perffrequency     m_crfrequency = li2double(perffrequency) end sub  public sub startcounter()     queryperformancecounter m_counterstart end sub  property timeelapsed() double dim crstart double dim crstop double     queryperformancecounter m_counterend     crstart = li2double(m_counterstart)     crstop = li2double(m_counterend)     timeelapsed = 1000# * (crstop - crstart) / m_crfrequency end property 

the linked class not great. there overhead associated calling queryperformancecounter , overhead should determined first used adjust subsequent calls. this explain results being slower expected, however not explain first 1 being substantially slower. anyway, see http://support.microsoft.com/kb/172338 describes approach adjust it. class have implemented not have access @ time cannot post relevant parts.

also read unrelated specific issue, related timings on windows in general, http://msdn.microsoft.com/en-gb/library/windows/desktop/dn553408%28v=vs.85%29.aspx interesting.

as workaround in meantime, add call start stop in class_initialize.

this not answer @ - more of extended comment - edit if ever find conclusive reason ;)

to reiterate upvoting: cannot explain reason results op querying. hypothesise vba interpreter (i.e. close jit'er) cannot evidence either way. commentary above related functions op using non sequitur.


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -