1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

3桁区切り

Last updated at Posted at 2015-12-01

断捨離

1,234,567,890

に変更するコードを連ねてんだけど、、、なんで、こんな事したのか不明
計算結果も残ってなかったし。

多分、 変数リファレンスをファイルハンドルで open させるとか、formline とかのメモなんだろうけど、、、

use Benchmark qw( timethese cmpthese  ) ;
my $str = '1234567890' ;

sub _unpack {
    my $s = $str ;
    my $len  = length $s ;
    my $mod = $len % 3 ;
    return join ',', unpack +( $mod == 0 ? '' : "A$mod" ) . +( "A3" x int ( $len / 3 ) )  , $s  ;
}

sub _while {
    my $s = $str ;
    1 while $s =~ s/(\d)((\d\d\d)+\b)/$1,$2/g ;
    return $s ;
}

sub _assertion {
    my $s = $str ;
    $s =~ s/(?<=\d)(?=(\d\d\d)+(?!\d))/,/g ;
    return $s ;
}

sub _regexp {
    my $s = reverse $str ;
    $s =~ s/(.{1,3})/$1,/g ; 
    chop $s ;
    return scalar reverse $s ;
}

sub _formline {
    local $^A = q{} ;
    my $s = reverse $str ;
    formline "^<<~~,", $s ; 
    $^A =~ s/\s+//g ;
    chop $^A ;
    return scalar reverse $^A ;
}

sub _filehandle {
    local $/ = \3 ;
    my $s = reverse $str ;
    open my $fh, q{<}, \ $s ;
    my $o = join ',', <$fh> ;
    close $fh ;
    return scalar reverse $o ;
}

#### cmpthese
{
    cmpthese(
        timethese( undef,
            {
                'assertion'  => '_assertion',  
                'filehandle' => '_filehandle',  
                'formline'   => '_formline', 
                'regexp'     => '_regexp',  
                'unpack'     => '_unpack',
                'while'      => '_while',  
            }   
        )   
    ) ; 
}

折角なので計算させた

これは、直近のマシンでの結果

Benchmark: running assertion, filehandle, formline, regexp, unpack, while for at least 3 CPU seconds...
 assertion:  3 wallclock secs ( 3.10 usr +  0.00 sys =  3.10 CPU) @ 282994.52/s (n=877283)
filehandle:  3 wallclock secs ( 3.13 usr +  0.00 sys =  3.13 CPU) @ 242140.26/s (n=757899)
  formline:  4 wallclock secs ( 3.24 usr +  0.01 sys =  3.25 CPU) @ 367804.62/s (n=1195365)
    regexp:  3 wallclock secs ( 3.13 usr +  0.00 sys =  3.13 CPU) @ 463741.21/s (n=1451510)
    unpack:  4 wallclock secs ( 3.16 usr +  0.00 sys =  3.16 CPU) @ 756560.44/s (n=2390731)
     while:  3 wallclock secs ( 3.19 usr +  0.00 sys =  3.19 CPU) @ 173044.51/s (n=552012)
               Rate     while filehandle assertion  formline    regexp    unpack
while      173045/s        --       -29%      -39%      -53%      -63%      -77%
filehandle 242140/s       40%         --      -14%      -34%      -48%      -68%
assertion  282995/s       64%        17%        --      -23%      -39%      -63%
formline   367805/s      113%        52%       30%        --      -21%      -51%
regexp     463741/s      168%        92%       64%       26%        --      -39%
unpack     756560/s      337%       212%      167%      106%       63%        --

当たり前だが、unpack が速い。

2021/01/14

久しぶりに見返してしまったら、自分で突っ込まざるを得ない、、、

こっちの方↓が早いし分り易い。

sub _unpack2 { 
    my $s = $str ; 
    return scalar reverse join ',',  unpack '(A3)*', reverse $s   
}

上の _unpackとの比較(マシーンは別)

    unpack:  4 wallclock secs ( 3.32 usr +  0.04 sys =  3.36 CPU) @ 520258.04/s (n=1748067)

   unpack2:  3 wallclock secs ( 3.20 usr +  0.03 sys =  3.23 CPU) @ 610075.85/s (n=1970545)

            Rate  unpack unpack2
unpack  520258/s      --    -15%
unpack2 610076/s     17%      --
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?