Alternative to calculating Elapsed time using QueryPerformanceCounter

nikhil sehgal <[email protected]> Mon, 10 Apr 2017 13:50:58 +0530
Newsgroups gmane.comp.gnome.mono.general
Message-ID <CAF0vV7zbhAtJVVtpLAcfXHYhH_Fvs00VH8b8p6E_6=RbHrNpHw__46416.0692565658$1491812472$gmane$org@mail.gmail.com>
--===============3620867305554709978==
Content-Type: multipart/alternative; boundary=001a11445820ae6203054ccba7f8

--001a11445820ae6203054ccba7f8
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

QueryPerformanceCounter returns a monotonically increasing tick count.

Current we are using above mentioned Performance Counter and Performance
Frequency using =E2=80=9CKernel32.dll=E2=80=9D.

Basically we use QueryPerformanceCounter to get a high resolution timer
value before and after the event you want to time.
Then use QueryPerformanceFrequency to get the number of ticks per second.
Divide the time difference by this value to convert the value to seconds.

alternatively we have thought to use stopwatch to achive same on Linux
using mono framrwork

Alternative way of Performance Counter and Performance Frequency by using
System. Diagnostics. Following are the code to find the same.

System.Diagnostics.Stopwatch watch =3D new System.Diagnostics.Stopwatch();
watch.Start();
MyFunction();
watch.Stop();
double elapsedMs =3D watch.ElapsedTicks;

Console.WriteLine(elapsedMs);



However result we are getting on windows using QueryPerformanceCounter and
on Linux using Stopwatch have different precision Please find screenshot
below.

--001a11445820ae6203054ccba7f8
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><p class=3D"MsoNormal" style=3D"margin-bottom:0.0001pt;lin=
e-height:normal"><span style=3D"font-size:12pt;font-family:&quot;times new =
roman&quot;,serif">QueryPerformanceCounter
returns a monotonically increasing tick count.</span><span style=3D"font-si=
ze:12pt;font-family:arial,sans-serif"><br>
</span><span style=3D"font-size:12pt;font-family:&quot;times new roman&quot=
;,serif"><br>
Current we are using above mentioned Performance Counter and Performance
Frequency using =E2=80=9CKernel32.dll=E2=80=9D.</span><span style=3D"font-s=
ize:12pt;font-family:arial,sans-serif"><br>
</span><span style=3D"font-size:12pt;font-family:&quot;times new roman&quot=
;,serif">=C2=A0</span><span style=3D"font-size:12pt;font-family:arial,sans-=
serif"><br>
</span><span style=3D"font-size:12pt;font-family:&quot;times new roman&quot=
;,serif">Basically we
use=C2=A0QueryPerformanceCounter=C2=A0to get a high resolution timer value
before and after the event you want to time.</span><span style=3D"font-size=
:12pt;font-family:arial,sans-serif"><br>
</span><span style=3D"font-size:12pt;font-family:&quot;times new roman&quot=
;,serif">Then
use=C2=A0QueryPerformanceFrequency=C2=A0to get the number of ticks per seco=
nd.
Divide the time difference by this value to convert the value to seconds.</=
span><span style=3D"font-size:12pt;font-family:arial,sans-serif"><br>
<br>
<span style=3D"background-image:initial;background-position:initial;backgro=
und-size:initial;background-repeat:initial;background-origin:initial;backgr=
ound-clip:initial">alternatively we have thought to use stopwatch
to achive same on Linux using mono framrwork=C2=A0</span><br>
<br>
</span><span style=3D"font-size:12pt;font-family:&quot;times new roman&quot=
;,serif">Alternative way of
Performance Counter and Performance Frequency by using System. Diagnostics.
Following are the code to find the same.</span><span style=3D"font-size:12p=
t;font-family:arial,sans-serif"><br>
</span><span style=3D"font-size:12pt;font-family:&quot;times new roman&quot=
;,serif">=C2=A0 =C2=A0=C2=A0<br>
System.Diagnostics.Stopwatch watch =3D new System.Diagnostics.Stopwatch();<=
br>
watch.Start();<br>
MyFunction();<br>
watch.Stop();<br>
double elapsedMs =3D watch.ElapsedTicks;<span></span></span></p>

<p class=3D"MsoNormal" style=3D"margin-bottom:0.0001pt;line-height:normal">=
<span style=3D"font-size:12pt;font-family:&quot;times new roman&quot;,serif=
">Console.WriteLine(elapsedMs);<span></span></span></p>

<p class=3D"MsoNormal" style=3D"margin:0in 0in 0.0001pt 1in;line-height:nor=
mal"><span style=3D"font-size:12pt;font-family:&quot;times new roman&quot;,=
serif"><span>=C2=A0</span></span></p>

<p class=3D"MsoNormal" style=3D"margin-bottom:0.0001pt;line-height:normal">=
<span style=3D"font-size:12pt;font-family:&quot;times new roman&quot;,serif=
">However result we are getting
on windows using QueryPerformanceCounter=C2=A0and on Linux using Stopwatch =
have
different precision Please find screenshot below.<span></span></span></p>

<p class=3D"MsoNormal" style=3D"margin:0in 0in 0.0001pt 1in;line-height:nor=
mal"><span style=3D"font-size:12pt;font-family:&quot;times new roman&quot;,=
serif"><span>=C2=A0</span></span></p>

<p class=3D"MsoNormal" style=3D"margin-bottom:0.0001pt;line-height:normal">=
<img width=3D"565" height=3D"282"><span style=3D"font-size:12pt;font-family=
:&quot;times new roman&quot;,serif"><span></span></span></p>

<p class=3D"MsoNormal" style=3D"margin:0in 0in 0.0001pt 1in;line-height:nor=
mal"><span style=3D"font-size:12pt;font-family:&quot;times new roman&quot;,=
serif"><span>=C2=A0</span></span></p>

<p class=3D"MsoNormal"><img width=3D"624" height=3D"402"><span></span></p>
</div>

--001a11445820ae6203054ccba7f8--

--===============3620867305554709978==
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Content-Disposition: inline

X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX18KTW9uby1saXN0
IG1haWxsaXN0ICAtICBNb25vLWxpc3RAbGlzdHMuZG90Lm5ldApodHRwOi8vbGlzdHMuZG90Lm5l
dC9tYWlsbWFuL2xpc3RpbmZvL21vbm8tbGlzdAo=

--===============3620867305554709978==--