Page 1 of 3 123 LastLast
Results 1 to 10 of 26

Thread: QueryPerformanceCounter problem and questions

  1. #1

    QueryPerformanceCounter problem and questions

    Hello

    I'm trying change GetTickCount from my codes to QueryPerformanceCounter

    code:

    Code:
    type TCustomTimer = class
      private
        PerfFrequency: Int64;
        InvPerfFrequency: Single;
      public
        function gettime: Single;
        constructor Create;
      end;
    
    type
      TForm1 = class(TForm)
        Button1: TButton;
        Edit1: TEdit;
        Memo1: TMemo;
        procedure FormCreate(Sender: TObject);
        procedure FormDestroy(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
    
        procedure Teste(Sender: TObject; var Done: Boolean);
      end;
    
    var
      Form1: TForm1;
      clock: TCustomTimer;
      _start, _end: Double;
    
    
    implementation
    
    {$R *.dfm}
    ///////////////
    { TCustomTimer }
    
    procedure TForm1.Teste(Sender: TObject; var Done: Boolean);
    begin
    
       _end := clock.gettime;
    
    
    
       //1000 = frequencia de intervalo em milesegundos
    
    
       if (_end-_start) >= 1000 then
       begin
    
         memo1.Lines.Add('evento');
         _start := _end;
    
    
       end;
    
    end;
    
    constructor TCustomTimer.Create;
    begin
      QueryPerformanceFrequency(PerfFrequency);
      InvPerfFrequency := 1.0 / PerfFrequency;
    end;
    
    function TCustomTimer.gettime: Single;
    var
      Time: Int64;
    begin
      QueryPerformanceCounter(Time);
      Result := 1000 * InvPerfFrequency * Time;
    end;
    ///////////////
    
    procedure TForm1.FormCreate(Sender: TObject);
    begin
    
    
      clock := TCustomTimer.Create;
    
      
      _start := clock.gettime;
    
      application.OnIdle := Form1.Teste;
    
    
    end;
    
    procedure TForm1.FormDestroy(Sender: TObject);
    begin
    
      Clock.Free;
    
    end;
    my code is ok?


    my question...

    what is this line?
    Code:
     InvPerfFrequency := 1.0 / PerfFrequency;
    Knowledge is power.

  2. #2

    QueryPerformanceCounter problem and questions

    You don't know what your own code does :?:
    I guess you are calculating the inverse of the frequency since
    you don't have to divide (slow) each time you get your time,
    but multiply instead.

    Your code looks correct to me.
    Maybe you should use double because single suffers from
    rounding errors using such big numbers.

  3. #3

    QueryPerformanceCounter problem and questions

    Quote Originally Posted by waran
    You don't know what your own code does :?:
    I guess you are calculating the inverse of the frequency since
    you don't have to divide (slow) each time you get your time,
    but multiply instead.

    Your code looks correct to me.
    Maybe you should use double because single suffers from
    rounding errors using such big numbers.
    I dont have created all of this code, and this is why i''m asking..

    about queryperformancecounter sometimes this can fail in some hardware? and i need use a timer with low precision(GetTickCount)?

    i know about this reading some webpage... actually this is not much clear...
    Knowledge is power.

  4. #4

    QueryPerformanceCounter problem and questions

    The PerformanceCounter works in Hardware if equipped (which is common nowadays).
    If no PerformanceCounter is available QueryPerformanceFrequency will return 0 (false) - you have to use something else then.

  5. #5

    QueryPerformanceCounter problem and questions

    Quote Originally Posted by waran
    The PerformanceCounter works in Hardware if equipped (which is common nowadays).
    If no PerformanceCounter is available QueryPerformanceFrequency will return 0 (false) - you have to use something else then.
    gettickcount for example?

    about queryperformancecounter what value it return? cpu cycles?
    Knowledge is power.

  6. #6

    QueryPerformanceCounter problem and questions

    The PerformanceCounter, unlike rdtsc, runs independent from your CPU
    and the frequency can vary (simply called "ticks per second").
    Its an independent hardware part on your mainboard.
    Ticks divided with the frequency gives you seconds.

    Yes, you can use GetTickCount instead since its purely software-based
    (like SDL_GetTicks or Windows' Multimedia-Timer). But the PerfCounter
    is usually the best choice if available.

  7. #7

    QueryPerformanceCounter problem and questions

    Quote Originally Posted by waran
    The PerformanceCounter, unlike rdtsc, runs independent from your CPU
    and the frequency can vary (simply called "ticks per second").
    Its an independent hardware part on your mainboard.
    Ticks divided with the frequency gives you seconds.

    Yes, you can use GetTickCount instead since its purely software-based
    (like SDL_GetTicks or Windows' Multimedia-Timer). But the PerfCounter
    is usually the best choice if available.
    what style of hardware dont have this avaliable?

    486 ?
    Knowledge is power.

  8. #8

    QueryPerformanceCounter problem and questions

    is ok now?

    TCustomTimer = class
    private
    PerfFrequency: Int64;
    InvPerfFrequency: Double;

    function TCustomTimer.gettime: Double;
    var
    Time: Int64;
    begin
    QueryPerformanceCounter(Time);
    Result := 1000 * InvPerfFrequency * Time;
    end;
    Knowledge is power.

  9. #9

    QueryPerformanceCounter problem and questions

    some comments about this optimized counter?

    http://17slon.com/blogs/gabr/labels/open%20source.html

    look the function:

    function GetTickCount64_Acc: int64;
    Knowledge is power.

  10. #10

    QueryPerformanceCounter problem and questions

    Quote Originally Posted by lordzero
    is ok now?

    TCustomTimer = class
    private
    PerfFrequency: Int64;
    InvPerfFrequency: Double;

    function TCustomTimer.gettime: Double;
    var
    Time: Int64;
    begin
    QueryPerformanceCounter(Time);
    Result := 1000 * InvPerfFrequency * Time;
    end;
    the gettime function desnot need to return a float type, since it always result in a plain natural number
    From brazil (:

    Pascal pownz!

Page 1 of 3 123 LastLast

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
  •