Array slicing through a reference: Inefficient?

E

Ethan Brown

Dear Group--

I'm working on a project where I'm working with large arrays, and I'm
using references to pass arrays around to reduce memory use as much as
possible. I often work with small subsets of a referenced array and
access it like:

my @subrange = @{$big_array_ref}[$start_idx .. $end_idx];

Also, to get the array size, I'll do:

my $num_points = @$big_array_ref;

Is this the best practice? Do I need to be concerned in a statement
like

@{$big_array_ref}[$start_idx .. $end_idx];

that the array referenced by $big_array_ref is being completely copied
before the subrange is taken? Likewise for the $num_points example.

Thanks,

--Ethan Brown
--Keyboards: "The Fabulous Pelicans" (www.pelicans.com)
--In a band? Use http://www.WheresTheGig.com for free.
 
T

Tassilo v. Parseval

Also sprach Ethan Brown:
I'm working on a project where I'm working with large arrays, and I'm
using references to pass arrays around to reduce memory use as much as
possible. I often work with small subsets of a referenced array and
access it like:

my @subrange = @{$big_array_ref}[$start_idx .. $end_idx];

Also, to get the array size, I'll do:

my $num_points = @$big_array_ref;

Is this the best practice? Do I need to be concerned in a statement
like

@{$big_array_ref}[$start_idx .. $end_idx];

that the array referenced by $big_array_ref is being completely copied
before the subrange is taken? Likewise for the $num_points example.

There is no need to worry here. If you have a reference to an array,
this means that the actual array also exists somewhere and you just
happen to access it through a reference. You're not duplicating any
arrays and no temporary huge arrays are built either.

Tassilo
 
P

pkent

Ethan Brown said:
Is this the best practice? Do I need to be concerned in a statement
like

@{$big_array_ref}[$start_idx .. $end_idx];

that the array referenced by $big_array_ref is being completely copied
before the subrange is taken? Likewise for the $num_points example.

As far as I know - but I haven't read the friendly source - the array is
not copied. In other words you're saying to perl "give me the slice
$start to $end from the array _pointed to_ by $arrayref". So I'd say to
you: don't worry , I think you're doing things the right way

P
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top