<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Philben - Ms Access &#187; Time</title>
	<atom:link href="https://blog.developpez.com/philben/ptag/time/feed" rel="self" type="application/rss+xml" />
	<link>https://blog.developpez.com/philben</link>
	<description></description>
	<lastBuildDate>Thu, 26 Sep 2013 19:43:53 +0000</lastBuildDate>
	<language>fr-FR</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.42</generator>
	<item>
		<title>Chronométrage en VBA</title>
		<link>https://blog.developpez.com/philben/p10973/vba-access/chronometrage_en_vba</link>
		<comments>https://blog.developpez.com/philben/p10973/vba-access/chronometrage_en_vba#comments</comments>
		<pubDate>Fri, 20 Apr 2012 22:37:57 +0000</pubDate>
		<dc:creator><![CDATA[philben]]></dc:creator>
				<category><![CDATA[VBA - Ms Access]]></category>
		<category><![CDATA[Code VBA]]></category>
		<category><![CDATA[Time]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Vous souhaitez comparer le temps d&#8217;éxecution de différentes implémentations d&#8217;une fonction ou d&#8217;un code ? Des solutions sont présentées&#8230; Les solutions Habituellement, j&#8217;utilise la fonction standard Timer() pour déterminer le temps écoulé entre le début et la fin d&#8217;un code. &#8230; <a href="https://blog.developpez.com/philben/p10973/vba-access/chronometrage_en_vba">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Vous souhaitez comparer le temps d&rsquo;éxecution de différentes implémentations d&rsquo;une fonction ou d&rsquo;un code ?<br />
Des solutions sont présentées&#8230;<br />
<span id="more-6"></span><br />
<strong>Les solutions</strong><br />
Habituellement, j&rsquo;utilise la fonction standard Timer() pour déterminer le temps écoulé entre le début et la fin d&rsquo;un code. Sa résolution est de 10ms ce qui est suffisant dans la plupart des cas si le temps écoulé est >= 1 seconde. Dans le cas contraire, on peut lancer x fois le même code pour réduire la part de l&rsquo;erreur.<br />
Noter que si l&rsquo;heure système est modifiée, Timer() suit aussi cette modification !</p>
<p>Une deuxième solution consiste à utiliser l&rsquo;API GetTickCount() qui retourne le nombre de millisecondes écoulées depuis le démarrage de Windows. Théoriquement, Au bout de ~49,71 jours le compteur revient à zéro (2^32 ms soit un entier long non signé) mais avec VBA, l&rsquo;API retourne un entier long signé et le compteur devient négatif après ~24,85 jours. Sa résolution est de l&rsquo;ordre de 15ms.</p>
<p>Une troisième solution consiste à utiliser l&rsquo;API timeGetTime() qui retourne aussi le nombre de millisecondes écoulées depuis le démarrage de Windows tout comme GetTickCount() mais sa résolution est de l&rsquo;ordre de la milliseconde.</p>
<p>Enfin, l&rsquo;API de référence est QueryPerformanceCounter(). Sa résolution dépend du système mais elle est de l&rsquo;ordre de la microseconde.</p>
<p><strong>Une classe VBA de chronométrage</strong><br />
je souhaitai écrire une classe qui implémente QueryPerformanceCounter() mais le travail avait déjà été fait depuis l&rsquo;an 2000 par C. Eswar Santhosh. Vous trouverez <a href="http://pcmnac-projects-and-researches.googlecode.com/svn/trunk/researches/vb6/VBMania/textos/StringFuncs/Timer%20Class.cls">ici</a> le code.</p>
<p><strong>Calcul des résolutions</strong><br />
En m&rsquo;appuyant sur cette classe, j&rsquo;ai écrit une fonction qui permet d&rsquo;estimer la résolution des différentes solutions présentées :</p>
<div class="codecolorer-container vb blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #E56717; font-weight: bold;">Option</span> <span style="color: #E56717; font-weight: bold;">Compare</span> Database <br />
<span style="color: #E56717; font-weight: bold;">Option</span> <span style="color: #E56717; font-weight: bold;">Explicit</span> <br />
&nbsp;<br />
<span style="color: #E56717; font-weight: bold;">Private</span> <span style="color: #151B8D; font-weight: bold;">Declare</span> <span style="color: #E56717; font-weight: bold;">Function</span> GetTickCount Lib <span style="color: #800000;">&quot;kernel32&quot;</span> () <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Long</span> <br />
<span style="color: #E56717; font-weight: bold;">Private</span> <span style="color: #151B8D; font-weight: bold;">Declare</span> <span style="color: #E56717; font-weight: bold;">Function</span> timeGetTime Lib <span style="color: #800000;">&quot;winmm.dll&quot;</span> () <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Long</span> <br />
&nbsp;<br />
<span style="color: #E56717; font-weight: bold;">Public</span> <span style="color: #E56717; font-weight: bold;">Function</span> testtime() <br />
&nbsp; &nbsp;Const cNbTest <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Long</span> = 100, cNbFunc <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Long</span> = 3 <br />
&nbsp; &nbsp;Const cMin <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Long</span> = 1, cSum <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Long</span> = 2, cBcle <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Long</span> = 3, cMax <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Long</span> = 4 <br />
&nbsp; &nbsp;<span style="color: #151B8D; font-weight: bold;">Dim</span> oT <span style="color: #151B8D; font-weight: bold;">As</span> cTimer <br />
&nbsp; &nbsp;<span style="color: #151B8D; font-weight: bold;">Dim</span> aRes(0 <span style="color: #8D38C9; font-weight: bold;">To</span> cNbFunc, cMin <span style="color: #8D38C9; font-weight: bold;">To</span> cMax) <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> <br />
&nbsp; &nbsp;<span style="color: #151B8D; font-weight: bold;">Dim</span> TimerT0 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Single</span>, TickT0 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Long</span>, TimeT0 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Long</span> <br />
&nbsp; &nbsp;<span style="color: #151B8D; font-weight: bold;">Dim</span> i <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Long</span>, j <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Long</span>, k <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Long</span> <br />
&nbsp;<br />
&nbsp; &nbsp;<span style="color: #151B8D; font-weight: bold;">Set</span> oT = <span style="color: #E56717; font-weight: bold;">New</span> cTimer <br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">For</span> i = 0 <span style="color: #8D38C9; font-weight: bold;">To</span> cNbFunc <br />
&nbsp; &nbsp; &nbsp; aRes(i, cMin) = 999 <br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">Next</span> i <br />
&nbsp;<br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">With</span> oT <br />
&nbsp; &nbsp; &nbsp; Debug.<span style="color: #151B8D; font-weight: bold;">Print</span> <span style="color: #800000;">&quot;-&gt; High timer activé ?&quot;</span>, .IsHiTimer <br />
&nbsp; &nbsp; &nbsp; Debug.<span style="color: #151B8D; font-weight: bold;">Print</span> <span style="color: #800000;">&quot;-&gt; Nombre de tests : &quot;</span>, cNbTest <br />
&nbsp;<br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">For</span> i = 0 <span style="color: #8D38C9; font-weight: bold;">To</span> cNbFunc <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;k = 0 <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">For</span> j = 1 <span style="color: #8D38C9; font-weight: bold;">To</span> cNbTest <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">Select</span> <span style="color: #8D38C9; font-weight: bold;">Case</span> i <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">Case</span> 0 <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.StartTimer <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;k = i <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">Do</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; k = k + 1 <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">Loop</span> <span style="color: #8D38C9; font-weight: bold;">Until</span> k &gt; i <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.EndTimer <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">Case</span> 1 <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.StartTimer <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TimerT0 = Timer <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">Do</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; k = k + 1 <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">Loop</span> <span style="color: #8D38C9; font-weight: bold;">Until</span> Timer &gt; TimerT0 <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.EndTimer <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">Case</span> 2 <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.StartTimer <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TickT0 = GetTickCount <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">Do</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; k = k + 1 <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">Loop</span> <span style="color: #8D38C9; font-weight: bold;">Until</span> GetTickCount &gt; TickT0 <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.EndTimer <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">Case</span> 3 <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.StartTimer <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TimeT0 = timeGetTime <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">Do</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; k = k + 1 <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">Loop</span> <span style="color: #8D38C9; font-weight: bold;">Until</span> timeGetTime &gt; TimeT0 <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.EndTimer <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">Select</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; aRes(i, cSum) = aRes(i, cSum) + .Elapsed <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">If</span> .Elapsed &lt; aRes(i, cMin) <span style="color: #8D38C9; font-weight: bold;">Then</span> aRes(i, cMin) = .Elapsed <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">If</span> .Elapsed &gt; aRes(i, cMax) <span style="color: #8D38C9; font-weight: bold;">Then</span> aRes(i, cMax) = .Elapsed <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">DoEvents</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">Next</span> j <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;aRes(i, cBcle) = k <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Debug.<span style="color: #151B8D; font-weight: bold;">Print</span> <span style="color: #800000;">&quot;Boucle n°&quot;</span> &amp; i, <span style="color: #800000;">&quot;Min (ms):&quot;</span> &amp; Round(aRes(i, cMin), 3), _ <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #800000;">&quot;Max (ms):&quot;</span> &amp; Round(aRes(i, cMax), 3), <span style="color: #800000;">&quot;Somme (ms):&quot;</span> &amp; Round(aRes(i, cSum), 3), <span style="color: #800000;">&quot;Nb Boucles:&quot;</span> &amp; k <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">DoEvents</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">Next</span> i <br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">With</span> <br />
&nbsp;<br />
&nbsp; &nbsp;Debug.<span style="color: #151B8D; font-weight: bold;">Print</span> <span style="color: #800000;">&quot;-&gt; Résolutions (en millisecondes) rectifiées du placebo :&quot;</span> &amp; vbNewLine &amp; _ <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #800000;">&quot;Timer() &nbsp; &nbsp; &nbsp; &nbsp;: &quot;</span> &amp; Round((aRes(1, cSum) - aRes(0, cSum)) / cNbTest), _ <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #800000;">&quot;Temps moyen d'une boucle (µs): &quot;</span> &amp; Round(aRes(1, cSum) / aRes(1, cBcle) * 1000, 5) &amp; vbNewLine &amp; _ <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #800000;">&quot;GetTickCount() : &quot;</span> &amp; Round((aRes(2, cSum) - aRes(0, cSum)) / cNbTest), _ <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #800000;">&quot;Temps moyen d'une boucle (µs): &quot;</span> &amp; Round(aRes(2, cSum) / aRes(2, cBcle) * 1000, 5) &amp; vbNewLine &amp; _ <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #800000;">&quot;TimeGetTime() &nbsp;: &quot;</span> &amp; Round((aRes(3, cSum) - aRes(0, cSum)) / cNbTest), _ <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #800000;">&quot;Temps moyen d'une boucle (µs): &quot;</span> &amp; Round(aRes(3, cSum) / aRes(3, cBcle) * 1000, 5) <br />
&nbsp; &nbsp; Debug.<span style="color: #151B8D; font-weight: bold;">Print</span> <span style="color: #800000;">&quot;Résultat de Timer() à prendre avec des pincettes car résolution théorique de 10ms...&quot;</span> <br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp;<span style="color: #151B8D; font-weight: bold;">Set</span> oT = <span style="color: #00C2FF; font-weight: bold;">Nothing</span> <br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #E56717; font-weight: bold;">Function</span></div></div>
<p>Bon chronométrage !</p>
<p>@+</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
