Ejemplo_Estructuradedatos_Pl

Run Settings
LanguagePerl
Language Version
Run Command
use strict; use Benchmark; use Scalar::Util; my $MUESTRAS = 100; # 1) mide tiempo de creacion de estructura de datos print "# construyendo estructuras de datos:\n"; print "> hash :\n"; timethis( $MUESTRAS, "crea_estructura_datos('hash')" ); print "> array:\n"; timethis( $MUESTRAS, "crea_estructura_datos('array')" ); # 2) mide memoria que necesita cada estructura print "\n# memoria RAM usada (KB):\n"; my $ref_hash = crea_estructura_datos('hash'); printf("\n> hash: %1.1f\n", Devel::Size::total_size($ref_hash)/1024); my $ref_array = crea_estructura_datos('array'); printf("> array: %1.1f\n", Devel::Size::total_size($ref_array)/1024); # 3) mide tiempo de consulta de estructura de datos print "\n# consultando estructuras de datos:\n"; print "> hash:\n"; timethis( $MUESTRAS, sub{ consulta_estructura_datos($ref_hash) } ); print "> array:\n"; timethis( $MUESTRAS, sub{ consulta_estructura_datos($ref_array) } ); ############################################ sub crea_estructura_datos { my ($hash_o_array) = @_; my $referencia; if($hash_o_array eq 'hash') { foreach my $n (1..100_000) { # las llaves ocupan menos como cadenas de caracteres # http://codenode.com/perl-memory-usage $referencia->{"$n"} = $n * 10; #7165.6KB en CPU 64bits #$referencia->{sprintf("%01.3f",$n)} = $n * 10; #7556.2KB " #$referencia->{$n/0.3} = $n * 10; #7914.3KB " } #descomenta para generar grafo de estructura como el del blog # OJO: mejor que sea una estructura no muy grande #my $grafo = GraphViz::Data::Grapher->new($referencia); #print $grafo->as_png("hash.png"); } else { foreach my $n (1..100_000) { push(@{$referencia}, $n * 10); # 3367.9KB #$referencia->[$n-1] = $n * 10; # lo mismo } #my $grafo = GraphViz::Data::Grapher->new($referencia); #print $grafo->as_png("array.png"); } return $referencia; } sub consulta_estructura_datos { my ($referencia) = @_; my $index; if(Scalar::Util::reftype($referencia) eq "HASH") { foreach my $n (1..100_000) { $index = int(rand(100_000)); $referencia->{$index} += 1; } } else { foreach my $n (1..100_000) { $index = int(rand(100_000)); $referencia->[$index] += 1; } } return $referencia; }
Editor Settings
Theme
Key bindings
Full width
Lines