Slow UDF call?

Stefan Koch uplink.coder at googlemail.com
Sat Aug 17 20:15:02 UTC 2019


On Saturday, 17 August 2019 at 19:43:22 UTC, Giovanni Di Maria 
wrote:
> Hi,
> i have seen that a simple operation (in a loop) is faster than 
> the equivalent UDF.
> The first example takes 4 seconds, the second example takes 16 
> seconds.
> Local variables influence the speed of execution?
> Thank you very much.
> Giovanni
>
> ======================================
>
> import std.stdio;
> void main()
> {
>     int a,b;
>     int s;
>     int k;
>     writeln("START");
>     for(k=1;k<=2_000_000_000;k++)
>     {
>         a=7;
>         b=20;
>         s=a+b;
>     }
>     writeln("Fine ",k," ",s);
> }
>
> ======================================
>
>
> import std.stdio;
> void main()
> {
>     int a,b;
>     int s;
>     int k;
>     writeln("START");
>     for(k=1;k<=2_000_000_000;k++)
>     {
>         a=7;
>         b=20;
>         s=somma(a,b);
>     }
>     writeln("Fine ",k," ",s);
> }
>
> int somma(int n1,int n2)
> {
>     return n1+n2;
> }

Yes they do

A function call has a cost.
In case of a function which performes a 1 cycle  (nominally 
without ILP) operation, the overhead of the function call 
dominates.
try compiling with -inline and compare again.


More information about the Digitalmars-d-learn mailing list