Bill Allombert on Sun, 01 May 2022 16:35:54 +0200


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: question about speed


On Sun, May 01, 2022 at 10:17:53AM -0400, Thomas Kellar wrote:
> I have code that looks like the below running billions
> of times and the top one is a little faster than the bottom
> one. This is not intuitive to me.  Is there any explanation?
> 
> if (a == b,
>     return(0);
> );
> return(1);
> 
> why is the above faster:
> and the below slower:
> 
> if (a == b,
>     return(0);
>    ,
>    return(1);
> );
> 
> Is that always the case?

Only if a!=b, because there is no need to evaluate an inline closure
(the "else" block).

Some suggestion:
- avoid if() when it is not needed: 
  in your example 'return (a!=b)' would be sufficient
- avoid return() at the end of functions.
   a!=b; can be sufficient.
- consider using gp2c.

Cheers,
Bill