Comments re High resolution replacement for Sleep
#1
Smile 
I had a bit of a 'carrry on' creating post #2. I couldn't figure out the difference between 'Quick Edit' and 'Full Edit'. On one occasion, I got post #1 merging with post #2. On another occasion, I got post #2 being duplicated within itself.

Would someone explain the difference?

Anyway, the question is whether to use SleepX or SleepXX. I cannot answer that - only you can by trying the two source codes. On my Windows 10 machine with a Performance Counter Frequency of 10MHz and a CPU Base Frequency of 3.5GHz SleepXX is the more accurate.

If you have the time, would you let me know what your Performance Counter Frequency is, your CPU Base Frequency, and a console output of the more accurate.

Ta much.  Smile
Reply
#2
Instead of a preprogrammed Base Frequency macro,  you can determine it at run time so your code is not machine specific Smile

'
Code:
#COMPILE EXE
#DIM ALL
#INCLUDE "win32api.inc"
GLOBAL CPUBaseFreq AS QUAD

FUNCTION PBMAIN () AS LONG
    CPUBaseFreq = GetBaseCPUFreq
    ? "CPU Base Frequency: " & STR$(CPUBaseFreq) & " Hz"
    '...
END FUNCTION
FUNCTION GetBaseCPUFreq() AS QUAD
    LOCAL retval , hkey AS DWORD
    LOCAL MHz AS DWORD
    retval = RegOpenKeyEx( %HKLM,"HARDWARE\DESCRIPTION\System\CentralProcessor\0"  ,0,%key_query_value,hkey)
    retval = regqueryvalueex(hkey,"~Mhz",0,%REG_DWORD,MHZ,4)
    retval = regclosekey(hkey)
    FUNCTION = MHZ * 10 ^6
END FUNCTION
'
Reply
#3
Thanks, Stuart

I am getting 'Undefined equate' on line #14 - RegOpenKeyEx
Reply
#4
Class A/class B amplifier analogy is backwards, so actually detracts from explanation of SLEEPXX.
A class A amp wastes power by being half on at zero input. Typically, one, or more (in series), class A amps drive a high(er) power class B amp that has both active drive components off when input is zero.
Is it too late to edit out?

No comment, either way, on SLEEPXX code ATT; though better resolution is a good thing.

Cheers,
Reply
#5
Stuart's code works if simply add: %HKLM = &H80000002???
Reply
#6
(09-14-2025, 03:19 AM)David Roberts Wrote: Thanks, Stuart

I am getting 'Undefined equate' on line #14 - RegOpenKeyEx

Sorry , I used  José's includes which has the equate:
%HKLM = %HKEY_LOCAL_MACHINE

With PB includes, you have to specify the root key in full:
retval = RegOpenKeyEx( %HKEY_LOCAL_MACHINE,"HARDWARE\DESCRIPTION\System\CentralProcessor\0"  ,0,%key_query_value,hkey)   
Reply
#7
Thanks, Dale. My taking a wrong turn on the analogies isn't relevant. The results with SleepX and SleepXX speak for themselves.

Thanks, Stuart.
Reply
#8
Couldn't work out why replacing the macro with the function didn't work.  Finally realised that I had to change /10^15 to *10^3 to get the correct values!

Took a bit of time to work out  that the issue was the macro substitution messing up the order of operations in the calculation.
The macro is not replacing CPUBaseFreq with the VALUE of 3.5*10^9, it is replacing it with the STRING  3.5*10^9 in  the source code, which resulted in a difference order of calculation and a vastly different magnitude of the result.

So qTime/CPUBaseFreq 
became
qTime/3.5*10^9

which is not the same as
qTime/(3.5*10^9)



(I guess the 10^15 was derived by trial and error since it has no logical basis when dividing tix by hertz Smile )
Reply
#9
Oops.  Smile

Post #3 at the source code forum incorporates your GetBaseCPUFreq.

There is no need for the offending macro now.

Thank you, Stuart.
Reply
#10
"If you have the time, would you let me know what your Performance Counter Frequency is, your CPU Base Frequency, and a console output of the more accurate."

Performance Counter Frequency = 10.0 Mhz
CPU Base Frequency = 2.712 GHz

In SLEEPXX
IF <= 3 THEN removed
  leaving:
  tix qTimeNow
  qTarget = qTimeNow + n*CPUBaseFreq*0.001
ELSE and below removed

console output:
Code:
Quarter of a millisecond: 0.25011 ms

0 seconds:0.000915 ms

2 seconds:2000.00025 ms

Silly:1234.000192 ms

01       1.000569 ms
02       2.000056 ms
03       3.00005  ms
04       4.000054 ms
05       5.000057 ms
06       6.000113 ms
07       7.000098 ms
08       8.000083 ms
09       9.000107 ms
10      10.000101 ms
11      11.000195 ms
12      12.000125 ms
13      13.000153 ms
14      14.000107 ms
15      15.000118 ms
16      16.000076 ms
17      17.000116 ms
18      18.00092  ms
19      19.000202 ms
20      20.000146 ms
21      21.000116 ms
22      22.000131 ms
23      23.000065 ms
24      24.000117 ms
25      25.000121 ms
26      26.000182 ms
27      27.0003   ms
28      28.000197 ms
29      29.000107 ms
30      30.000088 ms
(and lined up the numeric columns)
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)