From: Tonuzi Selim <[EMAIL PROTECTED]> > I am sorry but I am not a programmer and I have no > knowledge in C.
Being a programmer and knowing C are two completely unrelated things. I'm even inclined to say that not knowing C makes you a better programmer. > I am parsing a text file that contains instances and > wires linking these instances. > > I have millions of signals and I have some questions > that might sounds stupid to programmes like you. > > first point : > > I have a hash with millions of keys. > > $SIGNALS{$SIGNALNAME}->{INPUTS} = []; > $SIGNALS{$SIGNALNAME}->{OUTPUT} = []; > $SIGNALS{$SIGNALNAME}->{LEVEL} = []; > > every time a get a txt portion describing a signal: > I do the following thing : > > unless (exists $SIGNALS{$SIGNALNAME}) { > $SIGNALS{$SIGNALNAME}={}; This line above is not necessary. The line below will autocreate the hash. > $SIGNALS{$SIGNALNAME}->{isINPUTof}=[]; You forgot to initialize ->{isOUTPUTof} ! > } > push(@{$SIGNALS{$SIGNALNAME}->{isINPUTof}},$1); > push(@{$SIGNALS{$SIGNALNAME}->{isOUTPUTof}},$2); > ... > the keys are about 50 letters. > > is it better to do the following thing ? > > $tmp = $SIGNALS{$SIGNALSNAME} > push(@{$tmp->{isINPUTof}},$1); > push(@{$tmp->{isOUTPUTof}},$2); Not really. I once tried this and it actually seemed slower. You might try something a little tricky : for my $signal ($SIGNALS{$SIGNALNAME}) { if (! defined $signal) { $signal->{isINPUTof}=[]; $signal->{isOUTPUTof}=[]; } push @{$signal->{isINPUTof}}, $1; push @{$signal->{isOUTPUTof}}, $2; } or for my $signal ($SIGNALS{$SIGNALNAME}) { if (! defined $signal) { $signal->{isINPUTof}=[ $1 ]; $signal->{isOUTPUTof}=[ $2 ]; } else { push @{$signal->{isINPUTof}}, $1; push @{$signal->{isOUTPUTof}}, $2; } } In this I use two features of Perl. 1) Autovivification ... if you use an undefined variable $signal as a hash reference it WILL be a hash reference. Therefore you do not have to write $signal = {}; 2) alias created by "for" ... the "for my $variable (list)" doesn't copy the elements of the list to the $variable, it causes the $variable to work as an ALIAS to the elements. Which means that if you change the $variable, you change the element of the list. In this case the list contains just one element, $SIGNALS{$SIGNALNAME} so we only use the "for" loop for the aliasing ... and whatever we do to $signal affects $SIGNALS{$SIGNALNAME}. > Would it be faster to replace 'isINPUTof' by 'I' and > isOUTPUTof by 'O' ? I believe the difference would be so small that it's not worth doing. > I can't benchmark it because it will take days. You do not have to benchmark the whole script, make a small test case using the two ways of doing the thing and benchmark them. Eg. #!perl use Benchmark; sub Long { my $i = 0; while ($i++ < 10000) { $long{isINPUTof} = $long{isINPUTof} + 1; $long{isOUTPUTof} = $long{isOUTPUTof} + 1; } } sub Short { my $i = 0; while ($i++ < 10000) { $short{I} = $short{I} + 1; $short{O} = $short{O} + 1; } } timethese 100, { Long => \&Long, Short => \&Short, } __END__ Benchmark: timing 100 iterations of Long, Short... Long: 3 wallclock secs ( 3.00 usr + 0.00 sys = 3.00 CPU) @ 33.29/s (n=100) Short: 2 wallclock secs ( 2.64 usr + 0.00 sys = 2.64 CPU) @ 37.82/s (n=100) As you can see there is a difference, but keep in mind that this benchmark doesn't do anything else that it accesses those hashes! So maybe you could make the keys shorter, but only if it will not affect readability. Jenda =========== [EMAIL PROTECTED] == http://Jenda.Krynicky.cz ========== When it comes to wine, women and song, wizards are allowed to get drunk and croon as much as they like. -- Terry Pratchett in Sourcery _______________________________________________ ActivePerl mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs