in reply to Re^3: Performance, Abstraction and HOP
in thread Performance, Abstraction and HOP
I can't image trees implemented with arrays being that much more memory hungary than arrays alone.
Don't imagine--measure :)
P:\test>junk Array[ 1..10000]: 200056 Sum @array = 50005000 Tree[ 1..10000 ]: 1120016 Sum tree = 50005000 Rate tree array tree 17.1/s -- -82% array 95.1/s 455% --
I think that 6x bigger and 5x slower pretty much makes my point.
#!/usr/bin/perl -lw use strict; use List::Util qw[ sum shuffle ]; use Benchmark qw[ cmpthese ]; use Devel::Size qw[ total_size ]; my $MAX = 10000; our @array = 1 .. $MAX; our $tree = make_tree($MAX/2,undef,undef); $tree = insert( $_, $tree) for shuffle 1 .. $MAX; print "Array[ 1..$MAX]: ", total_size( \@array ); print "Sum \@array = ", sum( @array ); print "Tree[ 1..$MAX ]: ", total_size( $tree ); print "Sum tree = ", sum_tree( $tree ); cmpthese -1, { tree => q[ my $sum = sum_tree( $tree ); ], array=> q[ my $sum = sum @array ], }; exit; sub sum_tree { my $tree = shift; return 0 if not defined($tree); return node($tree) + sum_tree(right($tree)) + sum_tree(left($tree) +); } sub insert { (my $elem, my $tree) = @_; if(not defined($tree)){ return make_tree($elem, undef, undef); } my $curr = node($tree); if( $elem == $curr) { return $tree; } elsif($elem < $curr) { return make_tree($curr, insert($elem,left($tree)), right($tree)); } elsif($elem > $curr) { return make_tree($curr, left($tree), insert($elem,right($tree))); } } sub make_tree { [$_[0], $_[1], $_[2]] } sub node { $_[0]->[0] } sub left { $_[0]->[1] } sub right { $_[0]->[2] }
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^5: Perl memory usage
by Anonymous Monk on Sep 01, 2005 at 20:02 UTC | |
by kscaldef (Pilgrim) on Sep 02, 2005 at 08:26 UTC | |
by Anonymous Monk on Sep 02, 2005 at 14:56 UTC | |
by kscaldef (Pilgrim) on Sep 02, 2005 at 17:30 UTC | |
by Anonymous Monk on Sep 02, 2005 at 17:48 UTC |
In Section
Meditations