Variable declaration - C vs script style

M

markoa

Hello,

I've been wondering whether there are any differences in performance
when declaring variables within loops
comparing to all forward declarations. I'm mostly writing up to a few
hundred lines long scripts, but I need
relatively a lot variables, which look kind of ugly and IMO make the
script less readable when declared all
at the top. For example,

my ($var1, var2, var3, var4, var5, ...);
my (another set of variables);
while (...) {
$var1 = ...
$var2 = &foo($var1);
...
if ($var3) {
...
}

for ($var4 : ...)
}

vs

while (...) {
my $var1 = ...
my $var2 = &foo($var1);
...
my $var3 = ....;
if ($var3) {
...
}

for (my $var4 : ...)
}

Will the latter approach lead to more memory allocation and/or
significantly more work
for the garbage collector?

Marko
 
J

J. Gleixner

markoa said:
Hello,

I've been wondering whether there are any differences in performance
when declaring variables within loops

You know that you can test these things yourself, right?

perldoc Benchmark
 
B

brian d foy

markoa said:
I've been wondering whether there are any differences in performance
when declaring variables within loops
comparing to all forward declarations.

If that issue is a perfomance problem for you, then you're script is
already really, really fast or Perl is the wrong tool for you
(seriously). Don't even worry about it.

You should, however, limit variables just to the scope where you
actaully need them. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top