Page 3 of 3 FirstFirst 123
Results 21 to 30 of 30

Thread: Which libs to use ?

  1. #21

    Which libs to use ?

    Whats a good alternative to Timer?

  2. #22

    Which libs to use ?

    QueryPerformanceCounter :mrgreen:
    Peregrinus, expectavi pedes meos in cymbalis
    Nullus norvegicorum sole urinat

  3. #23

    Which libs to use ?

    Quote Originally Posted by JSoftware
    QueryPerformanceCounter :mrgreen:
    Written for Delphi, but easy to adapt to FPC. My "Quick Timers" unit:
    Code:
    (******************************************************************************
     *  The contents of this file are subject to the Mozilla Public License       *
     *  Version 1.1 (the "License"); you may not use this file except in          *
     *  compliance with the License. You may obtain a copy of the License at      *
     *  http://www.mozilla.org/MPL/                                               *
     *                                                                            *
     *  Software distributed under the License is distributed on an "AS IS"       *
     *  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the   *
     *  License for the specific language governing rights and limitations        *
     *  under the License.                                                        *
     *                                                                            *
     *  The Original Code is uQTimers.pas.                                        *
     *                                                                            *
     *  The Initial Developer of the Original Code is Robert "The Wicked Flea"    *
     *  Kosek. Portions created by Robert Kosek are Copyright (C) May 2006.       *
     *  All Rights Reserved.                                                      *
     ******************************************************************************)
    unit uQTimers;
    
    interface
    
    uses Windows;
    
    type
      TQuickTimer = record
        Start, Stop: Int64;
      end;
    
    { Returns a number for the running timer.                                      }
    function StartTimer: Integer;
    { When given a number the timer will be removed and the result will be
      the seconds elapsed.                                                         }
    function StopTimer(which: Integer): Extended;
    
    var ProcessorSpeed: Int64;
        QTimers: array of TQuickTimer;
    
    implementation
    
    function StartTimer: Integer;
    begin
      Result := length(QTimers);
      SetLength(QTimers,length(QTimers)+1);
      QueryPerformanceCounter(QTimers[Result].Start);
    end;
    
    function StopTimer(which: Integer): Extended;
      procedure RemoveIndex(Index: Integer);
      var i: integer;
      begin
        for i := Index to Length(QTimers)-1 do
          QTimers[i] := QTimers[i+1];
          
        SetLength(QTimers,Length(QTimers)-1);
      end;
    begin
      QueryPerformanceCounter(QTimers[which].Stop);
      Result := (QTimers[which].Stop - QTimers[which].Start) / ProcessorSpeed;
    
      if which = length(QTimers) then
        SetLength(QTimers,length(QTimers)-1)
      else
        RemoveIndex(which);
    end;
    
    procedure KillTimers;
    begin
      SetLength(QTimers,0);
    end;
    
    initialization
      QueryPerformanceFrequency(ProcessorSpeed);
    finalization
      KillTimers;
    end.

  4. #24

    Which libs to use ?

    Robert, your unit seems usable but it has some serious bugs. Take for instance:

    i := starttimer;
    i2 := starttimer;
    stoptimer(i);
    stoptimer(i2);

    this would make the procedure try to access the value in QTimers[i2] which would be QTimers[1]. This array entry does however not exist due to that we set the length of the array to 1 when we stopped timer i.
    Peregrinus, expectavi pedes meos in cymbalis
    Nullus norvegicorum sole urinat

  5. #25
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25

    Which libs to use ?

    Robert: Well thats one way to distribute your code. :lol:
    Jason McMillen
    Pascal Game Development
    Co-Founder





  6. #26

    Which libs to use ?

    QueryPerformanceCounter(
    AFAIK, there is no such function in unix :?

  7. #27

    Which libs to use ?

    Quote Originally Posted by JSoftware
    Robert, your unit seems usable but it has some serious bugs. Take for instance:

    i := starttimer;
    i2 := starttimer;
    stoptimer(i);
    stoptimer(i2);

    this would make the procedure try to access the value in QTimers[i2] which would be QTimers[1]. This array entry does however not exist due to that we set the length of the array to 1 when we stopped timer i.
    It's not a bug, it's the design purpose that I had in mind. Such as nested timers. The simple way to fix that would be to implement a timer_kill function so that you could deallocate the entry when it's not in use. The use I happened to design it for would be:
    Code:
    i := StartTimer;
      i2 := StartTimer;
      StopTimer(i2);
    StopTimer(i);
    This way you could have a timer within a loop, and one without. So you could easily find both the mean and total time, mean of execution and the total time the loop took to execute.

    WILL: LOL, Yup. I figured I'd help someone who is looking for snippets.

  8. #28

    Which libs to use ?

    Quote Originally Posted by Chebmaster
    QueryPerformanceCounter(
    AFAIK, there is no such function in unix :?
    How about using rdtsc instead? Doesn't that work in unix?
    Peregrinus, expectavi pedes meos in cymbalis
    Nullus norvegicorum sole urinat

  9. #29

    Which libs to use ?

    It should, but... The asm potentially limits the code portability, by tying it to Intel-32 bit platforms. Not that it matters too much, though.

    But i heard, using RDTSC on a multi-processor system can royally screw things up.

  10. #30

    Which libs to use ?

    Quote Originally Posted by Chebmaster
    But i heard, using RDTSC on a multi-processor system can royally screw things up.
    I think sly or Will or someone else on this forum presented a way to get around that problem some time ago
    Peregrinus, expectavi pedes meos in cymbalis
    Nullus norvegicorum sole urinat

Page 3 of 3 FirstFirst 123

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •