Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Accurate time interval measurement

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Accurate time interval measurement

    ' You can obtain accurate time interval measurements if your hardware
    ' supports a high-resolution performance counter as demonstrated in
    ' this code. If your hardware does not support a high-resolution
    ' counter, the code calculates the time interval with a somewhat less
    ' precision using the built-in TIMER function.
    '
    ' Best Regards,
    '
    ' Erik
    Code:
    #COMPILE EXE
    #REGISTER NONE
    #DIM ALL
    '
    #INCLUDE "win32api.inc"
    '
    FUNCTION PBMAIN AS LONG
        LOCAL PerfFreq AS QUAD, Res AS LONG, Prec AS LONG
        LOCAL t1 AS QUAD, t2 AS QUAD
        LOCAL ta AS DOUBLE, tb AS DOUBLE
        LOCAL tx AS STRING
        '
        ' Get, in counts per second, the current performance-counter frequency
        ' if supported by your hardware.
        Res = QueryPerformanceFrequency(PerfFreq)
        '
        ' Find Precision: number of significant decimals of time in seconds
        IF Res THEN
            Prec = CEIL(LOG10(PerfFreq))
        ELSE
            Prec = 2 ' Precision of TIMER is only about 1/18th of a second.
        END IF
        '
        ' Measure first time
        IF Res THEN ' if possible use this:
            QueryPerformanceCounter t1
        ELSE        ' else use the built-in timer
            ta = TIMER
        END IF
        '
        SLEEP 550 ' sleep 0.55 seconds
        '
        ' Measure second time
        IF Res THEN
            QueryPerformanceCounter t2
        ELSE
            tb = TIMER
        END IF
        '
        tx = "Measured time interval in seconds is: "
        IF Res THEN
            tx = tx + FORMAT$(ROUND((CDBL(t2)-CDBL(t1)) / CDBL(PerfFreq), Prec))
            MSGBOX tx, %MB_ICONINFORMATION, "Accurate time interval with" + STR$(Prec) + " decimals"
        ELSE
            tx = tx + FORMAT$(ROUND((tb - ta), Prec))
            MSGBOX tx, %MB_ICONINFORMATION, "Time interval with two decimals using TIMER:"
        END IF
        '
    END FUNCTION
    ------------------

  • #2
    I get a "line length greater than 256 characters" when I try a
    copy and paste operation to a new file in the IDE, but I can
    paste it into a WinWord document and save it as a DOS text file
    and surround the file name with double quotes ("FastTime.bas").

    Just a tip for anyone having similar problems.

    ------------------
    Old Navy Chief, Systems Engineer, Systems Analyst, now semi-retired

    Comment


    • #3
      Originally posted by Donald Darden:
      ...length greater than 256 characters"...
      Donald, click the "Edit/Delete" icon on the post in question.
      Then "Select all" (Ctrl-A), then "edit / copy". You can then
      paste directly into the IDE with all the Cr/Lf's intact.

      This was, and to a large degree, still is, an irritant. The
      problem is a well known windows clipboard bug that's been
      around since (I think) win/95.
      ------------------


      [This message has been edited by Mel Bishop (edited May 31, 2004).]
      There are no atheists in a fox hole or the morning of a math test.
      If my flag offends you, I'll help you pack.

      Comment


      • #4
        ' This version just collects the essential code in a function to obtain
        ' the current time in seconds past midnight with the highest possible accuracy.
        ' Without a performance counter this function defaults to the TIMER function.
        '
        ' Best Regards,
        '
        ' Erik
        Code:
        #COMPILE EXE
        #REGISTER NONE
        #DIM ALL
        '
        #INCLUDE "win32api.inc"
        '
        FUNCTION CurrentTimePointInSeconds AS DOUBLE
            ' This function returns the number of seconds since midnight
            ' as a Double-precision floating-point value.
            STATIC PerfFreq AS QUAD, Res AS LONG, Prec AS LONG, First AS LONG, TimeCorrection AS DOUBLE
            LOCAL t1 AS QUAD, ta AS DOUBLE
            '
            IF ISFALSE First THEN
                ' Get, in counts per second, the current performance-counter frequency
                ' if supported by your hardware.
                Res = QueryPerformanceFrequency(PerfFreq)
                '
                ' Determine Precision
                IF Res THEN
                    Prec = CEIL(LOG10(PerfFreq))
                    QueryPerformanceCounter t1
                    ta = ROUND(CDBL(t1) / CDBL(PerfFreq), Prec)
                    ' Find time correction to obtain number of seconds past midnight.
                    TimeCorrection = TIMER - ta
                    '
                    FUNCTION = ta + TimeCorrection
                    First = %TRUE : EXIT FUNCTION
                ELSE
                    Prec = 2 ' Precision of TIMER is only about 1/18th of a second.
                END IF
                First = %TRUE
            END IF
            ' Measure current time
            IF Res THEN ' if possible use this:
                QueryPerformanceCounter t1
                FUNCTION = ROUND(CDBL(t1) / CDBL(PerfFreq), Prec) + TimeCorrection
            ELSE        ' else use the built-in timer
                ta = TIMER
                FUNCTION = ROUND(ta, Prec)
            END IF
            '
        END FUNCTION
        '
        FUNCTION PBMAIN AS LONG
            LOCAL t1 AS DOUBLE, t2 AS DOUBLE
            LOCAL tx AS STRING
            '
            ' Measure first time
            t1 = CurrentTimePointInSeconds
            '
            SLEEP 550 ' sleep 0.55 seconds
            '
            ' Measure second time
            t2 = CurrentTimePointInSeconds
            '
            tx = "Time 1: " + FORMAT$(t1) + " seconds past midnight" + $CRLF
            tx = tx + "Time 2: " + FORMAT$(t2) + " seconds past midnight" + $CRLF + $CRLF
            tx = tx + "Time interval in seconds is: " + FORMAT$(t2 - t1, "######.#########")
            MSGBOX tx, %MB_ICONINFORMATION, "Maximum accuracy time interval measurement"
            '
        END FUNCTION
        ------------------

        Comment

        Working...
        X