[fpc-devel] Little feature teaser

Mattias Gaertner nc-gaertnma at netcologne.de
Fri Aug 2 17:01:44 CEST 2013


On Fri, 02 Aug 2013 13:18:53 +0200
Sven Barth <pascaldragon at googlemail.com> wrote:

>[...]
> === source begin ===
> 
> program tgenfuncs;
> 
> {$modeswitch result}
> 
> generic function IsIn<T>(aElement: T; const aArray: array of T): Boolean;
> var
>    elem: T;
> begin
>    for elem in aArray do
>      if elem = aElement then
>        Exit(True);
>    Result := False;
> end;
> 
> begin
>    Writeln(specialize IsIn<LongInt>(42, [21, 42, 84]));
>    Writeln(specialize IsIn<LongInt>(5, [21, 42, 84]));

What is the speed?
Is it much slower compared to a line of "if
(aElement=val) or (..)... then" or a "case" statement?

What about code size?

>    Writeln(specialize IsIn<String>('Foobar', ['Foo', 'Bar', 'Blubber']));
> end.


Mattias



More information about the fpc-devel mailing list