in reply to Re: Most Significant Set Bit
in thread Most Significant Set Bit

Unfortunately the log function isn't inexpensive

True, but you can halve the expense for large numbers of evaluations by storing log(2), which is just a constant.


🦛

Replies are listed 'Best First'.
Re^3: Most Significant Set Bit
by Danny (Chaplain) on Mar 15, 2024 at 23:20 UTC
    Strangely using a stored $log2 = log(2) in the above test did not improve the log function speed. It was actually slightly slower.

    EDIT: similar also with use constant log2 => log(2)

    EDIT2: The interpreter must compile such constant expressions into a constant before running.

      The interpreter must compile such constant expressions into a constant before running.

      Yes

      $ perl -MO=Concise,-exec -e'my $x = log(2);' 1 <0> enter v 2 <;> nextstate(main 1 -e:1) v:{ 3 <$> const[NV 0.693147180559945] s/FOLD 4 <1> padsv_store[$x:1,2] vKS/LVINTRO 5 <@> leave[1 ref] vKP/REFC -e syntax OK