wilbert wrote:@Guimauve, on OS X and Ubuntu 11 your code works fine.
gettimeofday does return microseconds on OS X as well as on Ubuntu 11 (on my computer).
For OS X you can also use nanoseconds as I posted yesterday
http://www.purebasic.fr/english/viewtop ... 19&t=49359
Code: Select all
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; AUTOMATICALLY GENERATED CODE, DO NOT MODIFY
; UNLESS YOU REALLY, REALLY, REALLY MEAN IT !!
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Code generated by : Dev-Type V3.139.671
; Project name : High Resolution Timer
; File name : HighResTimer.pb
; File Version : 0.1.9
; Programmation : In progress
; Programmed by : Guimauve
; Creation Date : 04-03-2012
; Last update : 04-03-2012
; Coded for PureBasic V4.60
; Platform : Windows, Linux, MacOS X
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Notes :
;
; This is a Try to create a Windows, Linux and
; MacOS X compliant High Resolution Timer sustem.
;
; It's insperated from Timer Class found here :
; http://www.songho.ca/misc/timer/timer.html
;
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
CompilerIf #PB_Compiler_OS <> #PB_OS_Windows
Structure TimeVal
Second.l
MicroSecond.l
EndStructure
Macro GetTimeValSecond(TimeValA)
TimeValA\Second
EndMacro
Macro GetTimeValMicroSecond(TimeValA)
TimeValA\MicroSecond
EndMacro
Macro ResetTimeVal(TimeValA)
GetTimeValSecond(TimeValA) = 0
GetTimeValMicroSecond(TimeValA) = 0
EndMacro
; The "TimeZone" Structure has been declared here only
; to Import the "gettimeofday()" function. It should not
; be used anywere.
Structure TimeZone
MinutesWest.L
DestinationTime.l
EndStructure
ImportC ""
gettimeofday(*TimeValA.TimeVal, *TimeZoneA.TimeZone)
EndImport
CompilerEndIf
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Structure declaration <<<<<
Structure HighResTimer
Stopped.b
StartMicroSec.d
StopMicroSec.d
CompilerIf #PB_Compiler_OS = #PB_OS_Windows
Frequency.q
StartCount.q
EndCount.q
CompilerElse
StartCount.TimeVal
EndCount.TimeVal
CompilerEndIf
EndStructure
; <<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< The observators <<<<<
Macro GetHighResTimerStopped(HighResTimerA)
HighResTimerA\Stopped
EndMacro
Macro GetHighResTimerStartMicroSec(HighResTimerA)
HighResTimerA\StartMicroSec
EndMacro
Macro GetHighResTimerStopMicroSec(HighResTimerA)
HighResTimerA\StopMicroSec
EndMacro
CompilerIf #PB_Compiler_OS = #PB_OS_Windows
Macro GetHighResTimerFrequency(HighResTimerA)
HighResTimerA\Frequency
EndMacro
Macro GetHighResTimerStartCount(HighResTimerA)
HighResTimerA\StartCount
EndMacro
Macro GetHighResTimerEndCount(HighResTimerA)
HighResTimerA\EndCount
EndMacro
CompilerElse
Macro GetHighResTimerStartCount(HighResTimerA)
HighResTimerA\StartCount
EndMacro
Macro GetHighResTimerEndCount(HighResTimerA)
HighResTimerA\EndCount
EndMacro
CompilerEndIf
; <<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< The mutators <<<<<
Macro SetHighResTimerStopped(HighResTimerA, P_Stopped)
GetHighResTimerStopped(HighResTimerA) = P_Stopped
EndMacro
Macro SetHighResTimerStartMicroSec(HighResTimerA, P_StartMicroSec)
GetHighResTimerStartMicroSec(HighResTimerA) = P_StartMicroSec
EndMacro
Macro SetHighResTimerStopMicroSec(HighResTimerA, P_StopMicroSec)
GetHighResTimerStopMicroSec(HighResTimerA) = P_StopMicroSec
EndMacro
CompilerIf #PB_Compiler_OS = #PB_OS_Windows
Macro SetHighResTimerFrequency(HighResTimerA, P_Frequency)
GetHighResTimerFrequency(HighResTimerA) = P_Frequency
EndMacro
Macro SetHighResTimerStartCount(HighResTimerA, P_StartCount)
GetHighResTimerStartCount(HighResTimerA) = P_StartCount
EndMacro
Macro SetHighResTimerEndCount(HighResTimerA, P_EndCount)
GetHighResTimerEndCount(HighResTimerA) = P_EndCount
EndMacro
CompilerElse
Macro SetHighResTimerStartCount(HighResTimerA, P_StartCount)
CopyTimeVal(P_StartCount, GetHighResTimerStartCount(HighResTimerA))
EndMacro
Macro SetHighResTimerEndCount(HighResTimerA, P_EndCount)
CopyTimeVal(P_EndCount, GetHighResTimerEndCount(HighResTimerA))
EndMacro
CompilerEndIf
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< The Reset operator <<<<<
Macro ResetHighResTimer(HighResTimerA)
SetHighResTimerStopped(HighResTimerA, 0)
SetHighResTimerStartMicroSec(HighResTimerA, 0.0)
SetHighResTimerStopMicroSec(HighResTimerA, 0.0)
CompilerIf #PB_Compiler_OS = #PB_OS_Windows
SetHighResTimerFrequency(HighResTimerA, 0)
SetHighResTimerStartCount(HighResTimerA, 0)
SetHighResTimerEndCount(HighResTimerA, 0)
CompilerElse
ResetTimeVal(GetHighResTimerStartCount(HighResTimerA))
ResetTimeVal(GetHighResTimerEndCount(HighResTimerA))
CompilerEndIf
EndMacro
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Code generated in : 00.010 seconds (17200.00 lines/second) <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Macro HighResTimerElapsedMilliSec(HighResTimerA)
(HighResTimerElapsedMicroSec(HighResTimerA) * 0.001)
EndMacro
Macro HighResTimerElapsedSec(HighResTimerA)
(HighResTimerElapsedMicroSec(HighResTimerA) * 0.000001)
EndMacro
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< The Initialize operator <<<<<
Procedure InitializeHighResTimer(*HighResTimerA.HighResTimer)
ResetHighResTimer(*HighResTimerA)
CompilerIf #PB_Compiler_OS = #PB_OS_Windows
QueryPerformanceFrequency_(@GetHighResTimerFrequency(*HighResTimerA))
CompilerElse
ResetTimeVal(GetHighResTimerStartCount(*HighResTimerA))
ResetTimeVal(GetHighResTimerEndCount(*HighResTimerA))
CompilerEndIf
EndProcedure
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< The Start operator <<<<<
Procedure StartHighResTimer(*HighResTimerA.HighResTimer)
SetHighResTimerStopped(*HighResTimerA, #False)
CompilerIf #PB_Compiler_OS = #PB_OS_Windows
If GetHighResTimerFrequency(*HighResTimerA) = 0
InitializeHighResTimer(*HighResTimerA.HighResTimer)
EndIf
QueryPerformanceCounter_(@GetHighResTimerStartCount(*HighResTimerA))
CompilerElse
gettimeofday(@GetHighResTimerStartCount(*HighResTimerA), #Null)
CompilerEndIf
EndProcedure
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< The Stop operator <<<<<
Procedure StopHighResTimer(*HighResTimerA.HighResTimer)
SetHighResTimerStopped(*HighResTimerA, #True)
CompilerIf #PB_Compiler_OS = #PB_OS_Windows
QueryPerformanceCounter_(@GetHighResTimerEndCount(HighResTimerA))
CompilerElse
gettimeofday(@GetHighResTimerEndCount(*HighResTimerA), #Null)
CompilerEndIf
EndProcedure
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< The ElapsedMicroSec <<<<<
Procedure.d HighResTimerElapsedMicroSec(*HighResTimerA.HighResTimer)
CompilerIf #PB_Compiler_OS = #PB_OS_Windows
If GetHighResTimerStopped(*HighResTimerA) = #False
QueryPerformanceCounter_(@GetHighResTimerEndCount(*HighResTimerA))
EndIf
SetHighResTimerStartMicroSec(*HighResTimerA, GetHighResTimerStartCount(*HighResTimerA) * (1000000.0 / GetHighResTimerFrequency(*HighResTimerA)))
SetHighResTimerStopMicroSec(*HighResTimerA, GetHighResTimerEndCount(*HighResTimerA) * (1000000.0 / GetHighResTimerFrequency(*HighResTimerA)))
CompilerElse
If GetHighResTimerStopped(*HighResTimerA) = #False
gettimeofday(@GetHighResTimerEndCount(*HighResTimerA), #Null)
EndIf
SetHighResTimerStartMicroSec(*HighResTimerA, (GetTimeValSecond(GetHighResTimerStartCount(*HighResTimerA)) * 1000000.0) + GetTimeValMicroSecond(GetHighResTimerStartCount(*HighResTimerA)))
SetHighResTimerStopMicroSec(*HighResTimerA, (GetTimeValSecond(GetHighResTimerEndCount(*HighResTimerA)) * 1000000.0) + GetTimeValMicroSecond(GetHighResTimerStartCount(*HighResTimerA)))
CompilerEndIf
ProcedureReturn GetHighResTimerStopMicroSec(*HighResTimerA) - GetHighResTimerStartMicroSec(*HighResTimerA)
EndProcedure
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< !!! WARNING - YOU ARE NOW IN A TESTING ZONE - WARNING !!! <<<<<
; <<<<< !!! WARNING - THIS CODE SHOULD BE COMMENTED - WARNING !!! <<<<<
; <<<<< !!! WARNING - BEFORE THE FINAL COMPILATION. - WARNING !!! <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
StartHighResTimer(HResTimer.HighResTimer)
Delay(5)
Debug StrD(HighResTimerElapsedMicroSec(HResTimer), 6)
Delay(50)
Debug StrD(HighResTimerElapsedMicroSec(HResTimer), 6)
Delay(499)
Debug StrD(HighResTimerElapsedMicroSec(HResTimer), 6)
Delay(500)
Debug StrD(HighResTimerElapsedMicroSec(HResTimer), 6)
Delay(1499)
Debug StrD(HighResTimerElapsedMicroSec(HResTimer), 6)
Delay(1501)
Debug StrD(HighResTimerElapsedMicroSec(HResTimer), 6)
; <<<<<<<<<<<<<<<<<<<<<<<
; <<<<< END OF FILE <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<
For me with computer with Linux Mint 12 x64 the debugger output show this :
0.000000
0.000000
0.000000
1000000.000000
2000000.000000
4000000.000000
It's exactly like the time is rounded to the nearest second. If this command work properly, it should return something on after the Delay(5), Delay(50) and Delay(499) but always 0.000000.
I say it's not normal or I have some wrong BIOS settings, I don't know.
Best regards
Guimauve