Skip to content

Instantly share code, notes, and snippets.

@bokutin
Last active August 29, 2015 13:56
Show Gist options
  • Save bokutin/8952654 to your computer and use it in GitHub Desktop.
Save bokutin/8952654 to your computer and use it in GitHub Desktop.
for ( ; ; ) べんち
#!/usr/bin/env perl
# 参考にさせていただきました m(_ _)m
# http://akiniwa.hatenablog.jp/entry/2013/09/28/142947
#
# perl は for ( ; ; ) が遅いですよね!
use strict;
use feature ":5.10";
use Data::Section::Simple qw(get_data_section);
use IO::All;
system("ruby -v");
system("python --version");
system("perl -v");
my $sections = get_data_section;
for my $name (sort keys %$sections) {
my $file = io($name)->print($sections->{$name});
my $ll = $name =~ /(ruby|python|perl)/ ? $& : die;
my $cmd = "$ll $name";
say "--> $cmd";
system($cmd);
say "";
}
__DATA__
@@ time_perl.pl
use strict;
use warnings;
use Time::HiRes;
my $a = 0;
my $i;
my $start = Time::HiRes::gettimeofday();
for ($i = 0;$i < 10000*10000;$i++) {
$a+=1;
}
my $end = Time::HiRes::gettimeofday();
print $end - $start, "[s]";
@@ time_perl2.pl
use strict;
use warnings;
use Time::HiRes;
my $a = 0;
my $i;
my $start = Time::HiRes::gettimeofday();
for (1..10000*10000) {
$a+=1;
}
my $end = Time::HiRes::gettimeofday();
print $end - $start, "[s]";
@@ time_perl3.pl
use strict;
use warnings;
use Time::HiRes;
my $a = 0;
my $i;
my $start = Time::HiRes::gettimeofday();
$a+=1 for 1..10000*10000;
my $end = Time::HiRes::gettimeofday();
print $end - $start, "[s]";
@@ time_perl4.pl
use strict;
use warnings;
use Time::HiRes;
my $a = 0;
my $i;
my $start = Time::HiRes::gettimeofday();
$a++ for 1..10000*10000;
my $end = Time::HiRes::gettimeofday();
print $end - $start, "[s]";
@@ time_python.py
import time
start = time.time()
a = 0
while a<100000000:
a+=1
end = time.time()
print end - start, "[s]"
@@ time_ruby.rb
startT = Time.now
a = 0
for num in 1..10000*10000 do
a+=1
end
endT = Time.now
puts "#{(endT - startT)} [s]"
__END__
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin12.4.0]
Python 2.7.3
This is perl 5, version 16, subversion 2 (v5.16.2) built for darwin-thread-multi-2level
Copyright 1987-2012, Larry Wall
Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.
Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl". If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.
--> perl time_perl.pl
7.93556714057922[s]
--> perl time_perl2.pl
5.65393996238708[s]
--> perl time_perl3.pl
5.23565888404846[s]
--> perl time_perl4.pl
3.56885409355164[s]
--> python time_python.py
12.3141222 [s]
--> ruby time_ruby.rb
6.279838 [s]
最適化されるかもなと思ってたけど、そんなことはなく $a++ の方が速かったー。
% perl -MO=Concise -e '$a+=1'
6 <@> leave[1 ref] vKP/REFC ->(end)
1 <0> enter ->2
2 <;> nextstate(main 1 -e:1) v:{ ->3
5 <2> add[t2] vKS/2 ->6
- <1> ex-rv2sv sKRM/1 ->4
3 <#> gvsv[*a] s ->4
4 <$> const[IV 1] s ->5
-e syntax OK
% perl -MO=Concise -e '$a++'
5 <@> leave[1 ref] vKP/REFC ->(end)
1 <0> enter ->2
2 <;> nextstate(main 1 -e:1) v:{ ->3
4 <1> preinc[t2] vK/1 ->5
- <1> ex-rv2sv sKRM/1 ->4
3 <#> gvsv[*a] s ->4
-e syntax OK
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment