Recent

Author Topic: [SOLVED] TAChart hours displayed backwards  (Read 9533 times)

lucamar

  • Hero Member
  • *****
  • Posts: 4217
Re: [SOLVED] TAChart hours displayed backwards
« Reply #30 on: November 01, 2018, 02:47:59 pm »
This function (taken from this forum post) may be quicker, but I don't think it would be that much (unless there are thousands of calls to it):

Code: Pascal  [Select][+][-]
  1. function Fun_IsLeapYear(aYear: integer): boolean;
  2. begin
  3.   Result := (aYear and 3) = 0;   // Test simple case
  4.   if Result then begin
  5.     aYear:=aYear shr 2;          // Reduce (Divide by 4)
  6.     Result:=(aYear mod 25)<>0;   // Eliminate those that cannot
  7.     if not Result then           // ..be divided by 100, they are always leaps
  8.       Result:=(aYear and 3)=0;   // Those that can be divided by 4(00) are leaps
  9.   end;
  10. end;

If it has any advantage it may be that it can be easily re-implemented in optimized assembler. :)
« Last Edit: November 01, 2018, 02:50:07 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

munair

  • Hero Member
  • *****
  • Posts: 887
  • compiler developer @SharpBASIC
    • SharpBASIC
Re: [SOLVED] TAChart hours displayed backwards
« Reply #31 on: November 01, 2018, 07:46:19 pm »
Quite revealing. With FPC year and 3 is more than twice as fast as year mod 4. With FBC it is the opposite. My benchmark shows that both LeapYear functions are equally fast. The third version proposed by Lucamar is slightly slower and has the disadvantage that it changes the function parameter.

Code: Pascal  [Select][+][-]
  1. function aLeapYear1(const y: integer): boolean;
  2. { http://forum.lazarus.freepascal.org/index.php/topic,43055.msg300956.html#msg300956 }
  3. begin
  4.   result := false;
  5.   if (y and 3) = 0 then
  6.     // divisible by 4
  7.     begin
  8.       if (y mod 100) = 0 then
  9.         // divisible by 100
  10.         begin
  11.           // must also be divisible by 400
  12.           if (y mod 400) = 0 then
  13.             result := true
  14.         end
  15.       else
  16.         result := true;
  17.     end;
  18. end;

Code: Pascal  [Select][+][-]
  1. function aLeapYear2(const Year: integer): boolean;
  2. { http://forum.lazarus.freepascal.org/index.php/topic,43055.msg300960.html#msg300960 }
  3. begin
  4.   Result := ((Year and 3) = 0) and ((Year mod 100 <> 0) or (Year mod 400 = 0));
  5. end;

I will leave it at that. Further testing is better done in a new topic.
It's only logical.

 

TinyPortal © 2005-2018