Time a function: Difference between revisions

→‎Hi-res version: Added library independant version
(→‎Hi-res version: Added library independant version)
Line 585:
and 1999 are palindromic.
Press ENTER to exit.</pre>
 
 
This version still relies on the Windows API but does not make use of any additional libraries.
<lang PureBasic>Procedure.f ticksHQ(reportIfPresent = #False)
Static maxfreq.q
Protected T.q
If reportIfPresent Or maxfreq = 0
QueryPerformanceFrequency_(@maxfreq)
If maxfreq
ProcedureReturn 1.0
Else
ProcedureReturn 0
EndIf
EndIf
QueryPerformanceCounter_(@T)
ProcedureReturn T / maxfreq ;Result is in milliseconds
EndProcedure
 
If OpenConsole()
Define timed.f, cnt
PrintN("Starting timing of a calculation,")
PrintN("for this we test how many of 0-1000000 are palindromic.")
; Dependent on Windows API
If ticksHQ(#True)
timed = ticksHQ() ;start time
; Same Foo() as above...
cnt = Foo(1000000)
timed = ticksHQ() - timed ;difference
EndIf
PrintN("The function need " + StrF(timed * 1000, 3) + " msec,")
PrintN("and " + Str(cnt) + " are palindromic.")
Print("Press ENTER to exit."): Input()
EndIf</lang>
Sample output:
<pre>Starting timing of a calculation,
for this we test how many of 0-1000000 are palindromic.
The function need 174.811 msec,
and 1999 are palindromic.</pre>
 
=={{header|Python}}==
Anonymous user