[Astuce] Mesurer le temps qui passe...

mars 2007 modifié dans Objective-C, Swift, C, C++ #1
On cherche quelques fois à  connaà®tre le temps de "travail" d'une fonction alors voici ma solution :

MPClockStarted()
// Fonction à  mesurer
MPClockStoppedForObject(self,@fonctionMesuree);


<br />#pragma mark --- Clock Time ---<br />//-------------------------------------------------------------------------------------------------------------------------------------------------------<br />double _MPClockTimeStopped(BOOL isStopped)<br />{<br />	static double theDouble[10];<br />	static int theIndex=0;<br /><br />	struct timeval theTime;<br />	gettimeofday(&amp;theTime,NULL);<br />	double result=-1;<br /><br />	if (isStopped)<br />	{<br />		theIndex--;<br />		double final=(double)theTime.tv_sec + ((double)theTime.tv_usec / 1000000.0);<br />		double result_d=((double)(final - theDouble[theIndex]) * 1e3);<br />		result=(double)result_d;<br />		if (theIndex&lt;0)<br />			theIndex=0;<br />	}<br />	else<br />	{<br />		theDouble[theIndex]=(double)theTime.tv_sec + ((double)theTime.tv_usec / 1000000.0);<br />		theIndex++;<br />		if (theIndex&gt;9)<br />		{<br />			theIndex=9;<br />			NSLog(@&quot;Error: MPClockTimeStopped - theIndex&gt;10&quot;);<br />			return -1;<br />		}<br />	}<br /><br />	return result;<br />}<br /><br />void MPClockStarted()<br />{<br />	_MPClockTimeStopped(NO);<br />}<br /><br />void MPClockStoppedForObject(id aObject, NSString *comment)<br />{<br />	double clockTime=_MPClockTimeStopped(YES);<br />	if (clockTime&lt;1000)<br />		NSLog([NSString stringWithFormat:@&quot;%@ (%@) time: %f ms&quot;,aObject,comment,clockTime]);<br />	else<br />		NSLog([NSString stringWithFormat:@&quot;%@ (%@) time: %f sec&quot;,aObject,comment,(clockTime/1000.0)]);<br />}<br />//-------------------------------------------------------------------------------------------------------------------------------------------------------<br />

Réponses

  • schlumschlum Membre
    20:55 modifié #2
    En multi-threadé, à  moins que ta fonction soit entièrement en section critique, c'est faussé. Non ?
    Je fais confiance à  "Shark" pour toutes ces choses  :P
Connectez-vous ou Inscrivez-vous pour répondre.