Skip to content

Instantly share code, notes, and snippets.

@sekia
Created June 14, 2012 12:44
Show Gist options
  • Save sekia/2930044 to your computer and use it in GitHub Desktop.
Save sekia/2930044 to your computer and use it in GitHub Desktop.
リストの要素数を数えるコードの速度比較 ref: http://qiita.com/items/5a00636dbf812722c959
Rate empty_assign array anon_array grep alias
empty_assign 7.68/s -- -2% -10% -17% -39%
array 7.84/s 2% -- -9% -15% -37%
anon_array 8.57/s 12% 9% -- -7% -31%
grep 9.21/s 20% 18% 8% -- -26%
alias 12.5/s 63% 60% 46% 36% --
#!/usr/bin/env perl
use strict;
use warnings;
use Benchmark qw/cmpthese/;
sub get_list { 1 .. 1_000_000 }
cmpthese(
-5,
+{
q/alias/ => sub { sub { 0 + @_ }->(get_list) },
q/anon_array/ => sub { 0 + @{[get_list]} },
q/array/ => sub { 0 + (my @ary = get_list) },
q/empty_assign/ => sub { scalar (() = get_list) },
q/grep/ => sub { scalar grep 1, get_list },
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment