Schwartzian Transform will not obvious faster if list count greater than 1M

Viewed 87

I did a little test. Think we got millions strings like "Testor_00_pg_1_8.7127", and sort them by three number in string. I did a comparison between Schwartzian Transform and plain sort.

The plain sort:

sub test_sub_orig{
    return sort {
                    my ($a1,$a2,$a3)=($a=~/_(\d+)_pg_(\d+)_(\d+\.\d+)/i);
                    my ($b1,$b2,$b3)=($b=~/_(\d+)_pg_(\d+)_(\d+\.\d+)/i);
                    $a1 <=> $b1 or $b2 <=> $a2 or $a3 <=> $b3;
                } @_;
}

The Schwartzian Transform:

sub test_sub_trans{
    return map {
                    $_->[0]
               }
           sort {
                    $a->[1] <=> $b->[1] or
                    $b->[2] <=> $a->[2] or
                    $a->[3] <=> $b->[3]
                }
           map {
                    $_=~/_(\d+)_pg_(\d+)_(\d+\.\d+)/i;
                    [$_, $1, $2, $3 ]
               } @_;
}

And below is my result:

Benchmark Result Trend(By count of strings)

The X-axis is the count of strings. The orange line is Schwartzian Transform's "Fast multiple" than plain sort, then dark grey line is Schwartzian Transform's time cost.

I wonder, why string count greater than 1M, get a such great efficiency degrade?

**And, why we get largest multiple when count is small not larger? **

0 Answers
Related