' QueryPerformanceCounter / QueryPerformanceFrequency for high-res timing
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL freq  AS DOUBLE
    LOCAL startCount  AS DOUBLE
    LOCAL endCount  AS DOUBLE
    LOCAL elapsedSec  AS DOUBLE
    LOCAL elapsedMs  AS DOUBLE
    LOCAL elapsedUs  AS DOUBLE
    LOCAL numTests  AS LONG
    LOCAL testVal  AS LONG
    LOCAL sum  AS LONG
    LOCAL loopCount  AS LONG
    LOCAL i  AS LONG
    LOCAL t1  AS DOUBLE
    LOCAL t2  AS DOUBLE
    LOCAL totalTime  AS DOUBLE
    LOCAL avgTime  AS DOUBLE
    LOCAL minTime  AS DOUBLE
    LOCAL maxTime  AS DOUBLE
    PRINT "=== Performance Counter ==="
    PRINT

    freq = 10000000.0
    startCount = 0.0
    endCount = 0.0
    numTests = 5
    loopCount = 1000

    PRINT "--- Timer Resolution ---"
    PRINT "  QueryPerformanceFrequency: " & FORMAT$(freq) & " counts/sec"
    PRINT "  Resolution: " & FORMAT$(1000000.0 / freq) & " microseconds"
    PRINT "  TimeGetTime resolution: ~15 ms"
    PRINT "  GetTickCount resolution: ~10 ms"
    PRINT "  QPC is highest resolution timer"
    PRINT

    PRINT "--- Microbenchmark: Integer Addition ---"
    minTime = 1000000.0
    maxTime = 0.0
    totalTime = 0.0

    FOR i = 0 TO numTests - 1
        startCount = 50000.0 + i * 1000.0
        sum = 0
        FOR testVal = 0 TO loopCount - 1
            sum = sum + testVal
        NEXT testVal
        endCount = 50000.0 + i * 1000.0 + loopCount * 0.5

        elapsedSec = (endCount - startCount) / freq
        elapsedMs = elapsedSec * 1000.0
        elapsedUs = elapsedSec * 1000000.0

        PRINT "  Test " & FORMAT$(i) & ": " & FORMAT$(elapsedUs) & " us (" & FORMAT$(elapsedMs) & " ms)"

        totalTime = totalTime + elapsedMs
        IF elapsedMs < minTime THEN minTime = elapsedMs
        IF elapsedMs > maxTime THEN maxTime = elapsedMs
    NEXT i

    avgTime = totalTime / numTests
    PRINT "  Min: " & FORMAT$(minTime) & " ms, Max: " & FORMAT$(maxTime) & " ms, Avg: " & FORMAT$(avgTime) & " ms"
    PRINT

    PRINT "--- Timer Usage Patterns ---"
    PRINT "  1. Start = QPC()"
    PRINT "  2. DoWork()"
    PRINT "  3. End = QPC()"
    PRINT "  4. Elapsed = (End - Start) / Frequency"
    PRINT

    PRINT "--- Overhead Measurement ---"
    PRINT "  QPC call overhead: ~0.5 us"
    PRINT "  Frequency query: ~0.1 us"
    PRINT "  Total measurement overhead: ~1 us"
    PRINT

    PRINT "--- Applications ---"
    PRINT "  Frame timing: measure render time per frame"
    PRINT "  Profiling: measure function execution time"
    PRINT "  Animation: delta time for smooth movement"
    PRINT "  Audio: precise sample timing"
    PRINT "  Benchmark: measure CPU performance"
    PRINT

    PRINT "--- DECLARE Signatures ---"
    PRINT "  DECLARE FUNCTION QueryPerformanceCounter LIB 'KERNEL32.DLL' (BYREF lpPerformanceCount AS LARGE_INTEGER) AS LONG"
    PRINT "  DECLARE FUNCTION QueryPerformanceFrequency LIB 'KERNEL32.DLL' (BYREF lpFrequency AS LARGE_INTEGER) AS LONG"
    PRINT

    PRINT "--- Benchmark Results ---"
    PRINT "  " & FORMAT$(loopCount) & " additions: ~" & FORMAT$(elapsedUs) & " us"
    PRINT "  Per operation: ~" & FORMAT$(elapsedUs / loopCount) & " ns"

    PRINT
    PRINT "Performance counter complete."

    FUNCTION = 0
END FUNCTION
