Perl: assigns [] or {} expensive? How to quickly reset numeric / associative arrays?

I have a card game - written in Perl and with several objects, like this:

package PlayingTable; our (%Games, $Num); sub new { my $pkg = shift; my $game = { ID => ++$Num, PHASE => WAITING, KIBITZERS => [], PLAYERS => [], INFO => '', RED5 => '', TALON => [], TABLE => {}, ROUND => 0, PASS_ROUND => 0, START => undef, TURN => undef, NPASSED => 0, HOLDER => undef, WHISTER1 => undef, WHISTER2 => undef, ACTIVE => undef, PASSIVE => undef, SHOW => undef, BEFORE => undef, SUIT1 => undef, TRUMP => undef, WINNER => undef, }; $Games{$Num} = $game; bless($game, $pkg); } 

and in objects I have many references to the hash and list, which I often have to reset. For example, when the game round is completed (in one case: when all the players have passed), I just call $ player β†’ {CARDS} = {}; to reduce the number of cards in the player by hand to 0.

My question is if assigning [] and {} is good enough practice or too expensive , because the perl interpreter will be malloc (or something else to allocate memory), this new hash and array objects internally (will it really be? Or enough catcher?).

I use (and don't want to update) the CentOS perl package:

 This is perl, v5.8.8 built for x86_64-linux-thread-multi 

with CentOS 5.6 / 64 bit, on a machine 4 GB and maximum. at the same time 500 players in the evenings. My perl process (daemon without forking, polling TCP sockets) is being used right now:

 top - 13:50:07 up 13 days, 3:25, 1 user, load average: 2.64, 3.36, 3.46 Tasks: 179 total, 2 running, 177 sleeping, 0 stopped, 0 zombie Cpu0 : 3.6%us, 0.3%sy, 0.0%ni, 96.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Cpu1 : 6.0%us, 1.3%sy, 0.0%ni, 92.7%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Cpu2 : 13.7%us, 0.3%sy, 0.0%ni, 85.3%id, 0.7%wa, 0.0%hi, 0.0%si, 0.0%st Cpu3 : 42.7%us, 1.7%sy, 0.0%ni, 54.6%id, 0.0%wa, 0.3%hi, 0.7%si, 0.0%st Mem: 4018280k total, 2831016k used, 1187264k free, 313128k buffers Swap: 7999472k total, 13612k used, 7985860k free, 1775196k cached PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 13685 afarber 15 0 112m 46m 2704 R 41.8 1.2 176:45.14 pref.pl 

Thanks! Alex

+4
source share
3 answers

Optimization Rules

  • Not necessary.
  • Not yet.
  • Profile before optimization.

In another comment, you say that you have no performance issues, so you are still in rule 1.

Regarding how to clear arrays and hashes, there is one potential error that needs to be avoided. It’s good practice to always return copies of the personal data of objects. Consider

 #! /usr/bin/env perl use strict; use warnings; package My::Class; sub new { my($class,@a) = @_; bless { a => \@a } => $class; } sub a { my($self) = @_; $self->{a}; } package main; my $obj = My::Class->new(1, 2, 3); my $a = $obj->a; print "@$a\n"; push @$a, qw/ foo bar /; my $b = $obj->a; print "@$b\n"; 

His conclusion

  1 2 3
 1 2 3 foo bar 

Returning a link to private data provides a handle for creating uncontrolled and probably unexpected changes.

If your code uses links, be sure to clear the same arrays and hashes, and not create links to new ones. Otherwise, everyone else will continue to use the old data, not knowing that something has changed. In terms of Perl write

 @{ $game->{PLAYERS} } = (); 

but not

 $game->{PLAYERS} = []; 
+7
source

Your sample is good practice.

There is a performance issue with Perl on RHEL-based systems, including CentOS. The package that RedHat built is significantly slower than Perl. You should experiment with the ActiveState build or the new Perl build (you can build Perl 5.14 in / opt / perl 514 if you want, it's not difficult) to see if you get the best performance.

0
source

I did a search on this because I think this is an interesting question, but I did not find any exact numbers.

However, here are some articles I found:

0
source

All Articles