Bonjour,
Comment chronométrer le temps de fonctions en ms.
J'ai utilisé GetTickCount, mais il ne semble pas assez précis (résolution minimale = 10 ms).
Cordialement,
Christophe,
Version imprimable
Bonjour,
Comment chronométrer le temps de fonctions en ms.
J'ai utilisé GetTickCount, mais il ne semble pas assez précis (résolution minimale = 10 ms).
Cordialement,
Christophe,
Bonjour,
Tu peux utiliser les timer multimedia. Ils sont plus précis, mais rien ne te garantit que tu auras une précision à la ms.
Si tu veux une meilleure précision, tu peux utiliser le Compteur de Performance.
On le consulte avec QueryPerformanceCounter() et on connait sa fréquence avec QueryPerformanceFrequency().
Regarde ici : http://www.codeproject.com/KB/system/timers_intro.aspx
J'ai utilisé les Queue timers et ça marche plutôt bien.
Salut,
Voici un peu de code
Une classe intéressante!!!
Voici un exemple d'utilisationCode:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90 namespace MCN { class CMcnPerformanceTime { public: CMcnPerformanceTime(bool bAuto = true) { m_bStarted = false; if(bAuto) Start(); } virtual ~CMcnPerformanceTime() { } static CString FormatMessage() { LPVOID lpMsgBuf; ::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, ::GetLastError(), 0, (LPTSTR) &lpMsgBuf, 0, NULL); CString strLastError = (LPCSTR)lpMsgBuf; ::LocalFree( lpMsgBuf ); return strLastError; } bool Start() { m_bStarted = false; if( ::QueryPerformanceFrequency(&m_nFrequency) == FALSE) { m_strLastError = FormatMessage(); return false; } if( ::QueryPerformanceCounter(&m_nBeginTime) == FALSE) { m_strLastError = FormatMessage(); return false; } m_strLastError.Empty(); m_bStarted = true; return true; } long double Stop() { if(m_bStarted == false) { m_strLastError = "Time counter not started\n"; return 0; } m_bStarted = false; if ( ::QueryPerformanceCounter(&m_nEndTime) == FALSE) { m_strLastError = FormatMessage(); return 0; } m_strLastError.Empty(); long double nElapseTime = (long double) ( m_nEndTime.QuadPart - m_nBeginTime.QuadPart ); nElapseTime /= m_nFrequency.QuadPart; nElapseTime *= 1000; return nElapseTime; // nanosecondes } CStringA GetLastError() const { return m_strLastError; } private: LARGE_INTEGER m_nFrequency; LARGE_INTEGER m_nBeginTime; LARGE_INTEGER m_nEndTime; CStringA m_strLastError; bool m_bStarted; }; // class CMcnPerformanceTime } // namespace MCN
:DCode:
1
2
3
4
5
6
7
8
9
10
11
12
13 void MyFunction() { MCN::CMcnPerformanceTime timer(false); timer.Start(); // start the timer // work, work, work... long double dElapse = timer.Stop(); // stop the timer // bon combien dans dElapse in nanoseconde??? }
Ça n'est pas la première fois que je vois cette absurdité:
Pourquoi passer par un LPVOID qu'on caste (mal) ?Citation:
static CString FormatMessage()
{
LPVOID lpMsgBuf;
::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS, NULL,
::GetLastError(), 0, (LPTSTR) &lpMsgBuf, 0, NULL);
CString strLastError = (LPCSTR)lpMsgBuf;
::LocalFree( lpMsgBuf );
return strLastError;
}
Code:
1
2
3
4
5
6
7
8
9
10
11
12 static CString FormatMessage() { LPTSTR lpMsgBuf; ::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, ::GetLastError(), 0, reinterpret_cast<LPTSTR>(&lpMsgBuf), 0, NULL); CString strLastError = lpMsgBuf; ::LocalFree( lpMsgBuf ); return strLastError; }
Une absurdité emprunté d'un code de Nico et de MSDN ;)
Mais la classe est impéccable et souple d'utilisation.
:D
Juste une petite rectification, le temps donnée par la classe CMcnPerformanceTime est en milliseconde et non pas en nanoseconde.