Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

mesurer le temps d'exécution d'un traitement

6 réponses
Avatar
programmation
Bonjour,

Je voulais mesurer le temps d'ex=E9cution d'un traitement alors j'ai mis
ceci:


<code type=3D"c">
int main()
{
double debut,end;
..............
..............
debut =3D clock();
//traitements
..........
..........

fin =3D clock();

fprintf(stderr, "\ntemps : %f\n", (double)(fin-debut) / (double)
CLOCKS_PER_SEC);

return 0;
}</code>


Est ce que cette fonction est bonne ou bien il y a autres fonctions
qui nous donnent le temps pr=E9cis ni plus ni moins ?
j'obtiens un temps t diff=E9rent ou bien =E9gale =E0 chaque fois que je
lance l'ex=E9cution de plus il arrive que
t =3D0.000000 malgr=E9 que il existe un traitement.
et comment =E9viter d'obtenir un temps nul ?


Merci.

6 réponses

Avatar
Christian ASTOR
On 10 sep, 09:26, programmation wrote:

Je voulais mesurer le temps d'exécution d'un traitement



Voir la fonction DoBench() avec QueryPerformanceCounter() du vieil
article :
http://msdn.microsoft.com/en-us/library/aa260969(VS.60).aspx
Avatar
programmation
> Voir la fonction DoBench() avec QueryPerformanceCounter() du vieil
article :http://msdn.microsoft.com/en-us/library/aa260969(VS.60).aspx



Mais, cette fonction utilise la fonction time() donc pourquoi pas on
utilise directement time() ?
Avatar
Christian ASTOR
On 10 sep, 15:03, programmation wrote:
> Voir la fonction DoBench() avec QueryPerformanceCounter() du vieil
> article :http://msdn.microsoft.com/en-us/library/aa260969(VS.60).aspx



Mais, cette  fonction utilise la fonction time() donc pourquoi pas on
utilise directement time() ?



Hein ?!!
Elle utilise QueryPerformanceCounter() !!!
Avatar
programmation
On 10 sep, 15:52, Christian ASTOR wrote:
On 10 sep, 15:03, programmation wrote:

> > Voir la fonction DoBench() avec QueryPerformanceCounter() du vieil
> > article :http://msdn.microsoft.com/en-us/library/aa260969(VS.60).aspx
> Mais, cette  fonction utilise la fonction time() donc pourquoi pas on
> utilise directement time() ?

Hein ?!!
Elle utilise QueryPerformanceCounter() !!!



Pouvez vous m'aider pour que je puisse utiliser cette fonction dans le
programme car je n'arrive pas à l'exploiter ?

Merci.
Avatar
programmation
On 10 sep, 21:09, programmation wrote:
On 10 sep, 15:52, Christian ASTOR wrote:

> On 10 sep, 15:03, programmation wrote:

> > > Voir la fonction DoBench() avec QueryPerformanceCounter() du vieil
> > > article :http://msdn.microsoft.com/en-us/library/aa260969(VS.60).as px
> > Mais, cette  fonction utilise la fonction time() donc pourquoi pas on
> > utilise directement time() ?

> Hein ?!!
> Elle utilise QueryPerformanceCounter() !!!




Bonjour,

Voici le code que je trouve dans ce lien
http://msdn.microsoft.com/en-us/library/aa260969(VS.60).aspx

#include "time.h"

enum { ttuUnknown, ttuHiRes, ttuClock } TimerToUse = ttuUnknown;
LARGE_INTEGER PerfFreq; // ticks per second
int PerfFreqAdjust; // in case Freq is too big
int OverheadTicks; // overhead in calling timer

void DunselFunction() { return; }

void DetermineTimer()
{
void (*pFunc)() = DunselFunction;

// Assume the worst
TimerToUse = ttuClock;
if ( QueryPerformanceFrequency(&PerfFreq) )
{
// We can use hires timer, determine overhead
TimerToUse = ttuHiRes;
OverheadTicks = 200;
for ( int i=0; i < 20; i++ )
{
LARGE_INTEGER b,e;
int Ticks;
QueryPerformanceCounter(&b);
(*pFunc)();
QueryPerformanceCounter(&e);
Ticks = e.LowPart - b.LowPart;
if ( Ticks >= 0 && Ticks < OverheadTicks )
OverheadTicks = Ticks;
}
// See if Freq fits in 32 bits; if not lose some precision
PerfFreqAdjust = 0;
int High32 = PerfFreq.HighPart;
while ( High32 )
{
High32 >>= 1;
PerfFreqAdjust++;
}
}
return;
}

double DoBench(void(*funcp)())
{
double time; /* Elapsed time */

// Let any other stuff happen before we start
MSG msg;
PeekMessage(&msg,NULL,NULL,NULL,PM_NOREMOVE);
Sleep(0);

if ( TimerToUse == ttuUnknown )
DetermineTimer();

if ( TimerToUse == ttuHiRes )
{
LARGE_INTEGER tStart, tStop;
LARGE_INTEGER Freq = PerfFreq;
int Oht = OverheadTicks;
int ReduceMag = 0;
SetThreadPriority(GetCurrentThread(),
THREAD_PRIORITY_TIME_CRITICAL);
QueryPerformanceCounter(&tStart);
(*funcp)(); //call the actual function being timed
QueryPerformanceCounter(&tStop);
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
// Results are 64 bits but we only do 32
unsigned int High32 = tStop.HighPart - tStart.HighPart;
while ( High32 )
{
High32 >>= 1;
ReduceMag++;
}
if ( PerfFreqAdjust || ReduceMag )
{
if ( PerfFreqAdjust > ReduceMag )
ReduceMag = PerfFreqAdjust;
tStart.QuadPart = Int64ShrlMod32(tStart.QuadPart, ReduceMag);
tStop.QuadPart = Int64ShrlMod32(tStop.QuadPart, ReduceMag);
Freq.QuadPart = Int64ShrlMod32(Freq.QuadPart, ReduceMag);
Oht >>= ReduceMag;
}

// Reduced numbers to 32 bits, now can do the math
if ( Freq.LowPart == 0 )
time = 0.0;
else
time = ((double)(tStop.LowPart - tStart.LowPart
- Oht))/Freq.LowPart;
}
else
{
long stime, etime;
SetThreadPriority(GetCurrentThread(),
THREAD_PRIORITY_TIME_CRITICAL);
stime = clock();
(*funcp)();
etime = clock();
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
time = ((double)(etime - stime)) / CLOCKS_PER_SEC;
}

return (time);
}


dans ce code il y a les fonctions comme "QueryPerformanceCounter()",
"clock()"...

Je ne sais pas comment je vais utiliser et appeler ce code dans mon
programme C ?

Merci.
Avatar
Christian ASTOR
On 11 sep, 07:42, programmation wrote:
Je ne sais pas comment je vais utiliser et appeler ce code dans mon
programme C ?



En passant une fonction en paramètre de DoBench()...
Sinon, en simplifié =>

#include <windows.h>
#include <stdio.h>
#include <tchar.h>

int _tmain(int argc, _TCHAR* argv[])
{
__int64 nFrequency, nStartTime, nStopTime, nTotalTime;
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
QueryPerformanceFrequency((LARGE_INTEGER*)&nFrequency);
QueryPerformanceCounter((LARGE_INTEGER*)&nStartTime);

Sleep(2000);

QueryPerformanceCounter((LARGE_INTEGER*)&nStopTime);
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
nTotalTime = nStopTime - nStartTime;
double nTime = nTotalTime /(double)nFrequency;
char sFormat[32];
char sTime[255];
sprintf(sFormat, "%%.%df", 10);
sprintf(sTime, sFormat, nTime);
fprintf(stdout, "Temps : %s secondesn", sTime);
return 0;
}