in reply to Re^2: Normalizing a range of numbers to a percentage
in thread Normalizing a range of numbers to a percentage
the ord() and substr() don't need to do float calculations.
In this$_ * $const first the integer in $_ is promoted to a double (to match the type of $const), then the multiplication is done, and then (to make it useful for the OP though you aren't doing it here) the result needs to be converted (trunc'd) back to an integer. (If you added back that necessity, the difference would be more marked.)
Runtime memoization and look up using a hash (per the Memoize module) would be much slower because each input integer needs to be be converted to a string, then that string must be hashed, then taken modulo the hash size (which must be looked up, then that table entry inspected, and (potentially) a linear search of an array performed, before the value is found.
The lookup essential consists of a direct index and done.
For the OPs purpose, an array lookup would probably be even quicker:
#! perl -slw use strict; my @lookup = map{ int( $_ / 255 * 100 ) } 0 .. 255; print "$_ :: ", $lookup[ $_ ] for 0 .. 255;
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^4: Normalizing a range of numbers to a percentage
by Lotus1 (Vicar) on Mar 09, 2019 at 17:08 UTC |