which is faster ?

D

Dr.Ruud

which is faster ?

$#array
scalar @array or
my $n = @array ?

If magic or tie or operator-overload is not involved, there will hardly
be any difference in speed. Just benchmark to find out.

$#array is the highest index of @array. The other two are about the
number of elements. $[ gives the minimum index which is normally 0.
 
J

Jürgen Exner

George Mpouras said:
$#array
scalar @array or
my $n = @array ?

IMNSHO you are asking the wrong question. Because each of these three
code pieces has a different semantic the right question would be "Which
code piece has the right semantic for my algorithm?"

jue
 
J

Jürgen Exner

robin said:
I assume with strict the my one with work faster, I think the strict
pragma makes it faster perl.

Your statement can be interpreted in two ways:
- I assume with strict the my one with work faster than the other two
options shown
- I assume with strict the my one with work faster than without using
strict

Which one is it?

jue
 
R

Rainer Weikusat

robin said:
I assume with strict the my one with work faster, I think the strict
pragma makes it faster perl.

According to the corresponding documentation, access to 'my' variables
should be faster than acceses to 'other variables' and I expect this
to be actually true because a variable declared with my reside in a
scope-specific array and are accessed by array index while 'package
globals' require a hash lookup in the symbol table hash of the package
they belong to. But that's not at all related to 'strict' which
reconfigures the Perl compile by modifying the variable $^H and has no
runtime effects. (AFAIK).
 

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,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top