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.
function aLeapYear1(const y: integer): boolean;
{ http://forum.lazarus.freepascal.org/index.php/topic,43055.msg300956.html#msg300956 }
begin
result := false;
if (y and 3) = 0 then
// divisible by 4
begin
if (y mod 100) = 0 then
// divisible by 100
begin
// must also be divisible by 400
if (y mod 400) = 0 then
result := true
end
else
result := true;
end;
end;
function aLeapYear2(const Year: integer): boolean;
{ http://forum.lazarus.freepascal.org/index.php/topic,43055.msg300960.html#msg300960 }
begin
Result := ((Year and 3) = 0) and ((Year mod 100 <> 0) or (Year mod 400 = 0));
end;
I will leave it at that. Further testing is better done in a new topic.