Here's the code so far:
#1. In the operations that you see, is there anyplace where a code change could be applied that would make it faster?package GNS::Cache; use strict; ########### ## How this for strangeness ... ########### sub new { my $class=shift; my $size=shift; my $self=bless { }; $self->{size}=($size ? $size : 30); $self->{list}=[]; $self; } sub check { my $self=shift; my $id=shift; local $_; ## walk through the list foreach (@{$self->{list}}) { if ($_->[0]==$id) { return $_->[1]; } } return; } sub insert { my $self=shift; my $id=shift; my $data=shift; my $list=$self->{list}; push @$list,[$id,$data]; shift @$list if $#$list>=$self->{size}; } sub purge { my $self=shift; $self->{list}=[]; } sub dump { my $self=shift; local $_; foreach (@{$self->{list}}) { print "$_->[0] = $_->[1]\n"; } } ####### 1; __END__ #######
#2. In the check() function, if a hit is found, I would like the item move to the top of the list (representing it should stay in the cache longer). What I need to do is remove the current item ($_ as it stands now) and tack it on to the end of the list via a push. How would you recommend doing it? I dont care about cleverness, or obsfucation, but raw number of instructions. I had though of doing an undef $_; inside the loop, but that doesn't seem to actually remove the item from the array, just undef it out (the list continues to have the same # of elements). I suspect I need to splice it, but without having the index of the element of the array, I couldn't figure out how to do it... efficiently.
#3. In my purge() function, I do a simple $self->{list}=[] to reset the list. I presume Perl's memory management is good enough to handle that style of deallocation. Correct?
There are a bazillion methods of doing this, and I've tried a few, but I'm curious as to everyone else's ideas.
Thanx! and forgive me if this is not well written, I'm just on my first cup of java.
Thanx!
In reply to Efficiency Question by mr.nick
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |