miércoles, 30 de enero de 2013
Perl: How to take time to benchmark or profiling
#!/usr/bin/perl
#How to benchmark your Perl code?
#Example of how to take time for benchmark or profiling in Perl.
#Calcular el tiempo transcurrido entre lineas de codigo.
use strict;
use warnings;
########### EXAMPLE 1 ###########
#sample works for second intervals
my $start_ = time;
sleep rand(17)/3; #Job: or some other crazy pieces of code
my $end_ = time;
my $length = $end_ - $start_;
print "It took us $length seconds.\n";
#Out: It took us 3 seconds.
########### EXAMPLE 2 ###########
#Using Time::HiRes
#How can I get the time in milliseconds in Perl using Time::HiRes
use Time::HiRes qw/ time sleep gettimeofday tv_interval /;
my $start = time; #Returns a floating seconds since the epoch.
my $s1; #seconds since the epoch
my $usec1; #microsegundos
my $s2; #seconds since the epoch
my $usec2; #microsegundos
($s1, $usec1) = gettimeofday; # get seconds and microseconds since the epoch
sleep rand(17)/3; #Job: or some other crazy pieces of code
my $end = time; #Returns a floating seconds since the epoch.
# get seconds and microseconds since the epoch
($s2, $usec2) = gettimeofday; # get seconds and microseconds since the epoch
print 'Slept for ', ( $s2 - $s1 ) , " seconds \n";
print 'Slept for ', ( $usec2 - $usec1 ) , " micro-seconds \n";
print 'Slept for ', ( $end - $start ) , " floating seconds since the epoch \n";
print 'Slept for ', tv_interval ( [$s1, $usec1], [$s2, $usec2]), " floating seconds \n";
#Out: Slept for 1 seconds
#Out: Slept for -360540 micro-seconds
#Out: Slept for 0.639461040496826 floating seconds since the epoch
#Out: Slept for 0.63946 floating seconds
########### EXAMPLE 3 ###########
#Using Benchmark
use Benchmark;
my $t0 = new Benchmark;
sleep rand(17)/3; #Job: or some other crazy pieces of code
my $t1 = new Benchmark;
my $td = timestr(timediff($t1, $t0));
print "It took us : " . $td . "\n";
#Out: It took us : 2 wallclock secs ( 0.01 usr + 0.00 sys = 0.01 CPU)
########### EXAMPLE 4 ###########
#Using Benchmark::Timer
#Install it on ubuntu with: apt-get install libbenchmark-timer-perl
#Example of Benchmarking with statistical confidence.
use Benchmark::Timer;
my $t = Benchmark::Timer->new(skip => 0);
for(1 .. 4) {
$t->start('test_01');
sleep rand(17)/3; #Job: or some other crazy pieces of code
$t->stop('test_01');
}
print $t->report;
#Out: 4 trials of test_01 (10.204s total), 2.551s/trial
########### EXAMPLE 5 ###########
# Para comparar la prestancia entre dos métodos.
# Este es un script aparte:
#!/usr/bin/perl -w
use strict;
use warnings;
use Time::HiRes qw(time);
use Benchmark qw(:all) ;
my $msg = "mensaje";
#Start test
cmpthese($times, {
'method 1' => sub { function_1( 100 ) },
'method 2' => sub { function_2( 100 ) },
});
#End test
sub function_1 {
my $iterations = shift;
for (my $i = $iterations; $i >= 1; $i--) {
#Hace algo 1
print "Mensaje: $msg";
}
}
sub function_2 {
my $iterations = shift;
for (my $i = $iterations; $i >= 1; $i--) {
#Hace algo 2
print "Mensaje: $msg";
}
}
Reference:
- http://perldoc.perl.org/Time/HiRes.html
- http://perldoc.perl.org/Benchmark.html
- http://search.cpan.org/~dcoppit/Benchmark-Timer-0.7102/lib/Benchmark/Timer.pm
Suscribirse a:
Enviar comentarios (Atom)
No hay comentarios:
Publicar un comentario