Created
June 14, 2012 12:44
-
-
Save sekia/2930044 to your computer and use it in GitHub Desktop.
リストの要素数を数えるコードの速度比較 ref: http://qiita.com/items/5a00636dbf812722c959
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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% -- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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