in reply to Using a Variable Mutipule times(with different data)

This is mostly a methodologic question.

The use of different variables for different things is preferred as when someone reads/debugs your code it is clear that the different instances relate to different data.
All compilers nowadays (including perl, according to the excellent explenation here) define the scope of the variable, so the "dangling" variables get cleared away as soon as they are done.

An exception for this is objects that require expensive setup (like ftp connection for example). In these cases it makes more sense, from optimization point of view, to reuse these resources.

Sorry for not giving actual timing data, maybe someone will benchmark it.

  • Comment on Re: Using a Variable Mutipule times(with different data)

Replies are listed 'Best First'.
Re: Re: Using a Variable Mutipule times(with different data)
by b (Beadle) on Dec 30, 2000 at 02:50 UTC
    Here is a benchmark:
    #!/usr/bin/perl -w use strict; use Benchmark; timethese(1000, { 'global' => '&global', 'local' => '&local' }); sub global { my %hash; for(1..100) { for(1..10) { $hash{rand(100)} = 123; }; } %hash=(); } sub local { for(1..100) { my %hash; for(1..10) { $hash{rand(100)} = 123; }; } }
    And it gives:
    Benchmark: timing 1000 iterations of global, local... global: 14 wallclock secs (13.47 usr + 0.01 sys = 13.48 CPU) local: 14 wallclock secs (13.33 usr + 0.02 sys = 13.35 CPU)
    so it seems like there is not much difference.