in reply to Risque Romantic Rosetta Roman Race

Since you already have timing code set up, could you please replace

map { $rtoa{$_} } split//, uc(shift)

with

@rtoa{ split//, uc(shift) }

and see if it is any faster?

Replies are listed 'Best First'.
Re^2: Risque Romantic Rosetta Roman Race
by eyepopslikeamosquito (Archbishop) on May 10, 2023 at 09:06 UTC

    Your version crashed and burned. Assuming a simple typo, I changed:

    @rtoa{ split//, uc(shift) }
    to:
    @rtoa{ split//, uc($_) }

    and it ran significantly faster:

    $ perl rtoa-pgatram-tybalt.pl t1.txt >tybalt.tmp rtoa pgatram start read_input_files : 1 secs roman_to_arabic : 4 secs output : 1 secs total : 6 secs $ diff tybalt.tmp pgatram.tmp

      Sorry, I should have provided more context. I was looking at an earlier version which I had changed to

      { my %rtoa = ( M=>1000, D=>500, C=>100, L=>50, X=>10, V=>5, I=>1 ); sub roman_to_dec { reduce { $a+$b-$a%$b*2 } @rtoa{ split//, uc(shift) } } }

      Would it be faster as

      @rtoa{ split//, uc } # save uc from having to process an argument by +letting it just default to $_

        Faster still to avoid the uc entirely? Just double the hash:

        state %rtoa = ( M=>1000, D=>500, C=>100, L=>50, X=>10, V=>5, I=>1, m=>1000, d=>500, c=>100, l=>50, x=>10, v=>5, i=>1, );

        A quick test shows it's not significantly different - just feels better. :-)


        🦛

        Ha ha, I'd already tried that and couldn't measure any difference. I'd also tried losing the uc function by increasing the hash to include lower case letters, but that was a bit slower. Update: hippo suggested that while I was typing. :)

        The most promising approach may be to find an alternative to:

        M=>1000, D=>500, C=>100, L=>50, X=>10, V=>5, I=>1
        to do the mapping. For example, 'M1000D500C100L50X10V5I1' and use a regex or something. I made a few half-hearted attempts but didn't find anything faster.

        It might also be faster to remove the reduce and do the summing in another uglier way ... but reduce is so elegant I couldn't bring myself to try anything along those lines, that would be like losing an old friend. :)

      Faster AND golfier :)