Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Dereferencing Performance - Confused by Results

by Athanasius (Archbishop)
on Oct 04, 2014 at 03:45 UTC ( [id://1102798]=note: print w/replies, xml ) Need Help??


in reply to Dereferencing Performance - Confused by Results

Hello jmmitc06,

The syntax:

@smrow = @{$sm_rows[$_]};

doesn’t just perform a dereference, it also copies the contents of the right-hand side into @smrow. So you have to balance the additional overhead of making copies against the saving on dereferences within the loop.

Update: Perhaps you should investigate the Data::Alias module on . For example, you can replace @smrow = @{$sm_rows[$_]}; with:

use Data::Alias; ... alias @smrow = @{$sm_rows[$_]};

which avoids any copying of the data.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: Dereferencing Performance - Confused by Results
by jmmitc06 (Beadle) on Oct 04, 2014 at 04:10 UTC
    Thanks, I knew that some time ago but I didn't think about it, that makes much more sense then.
Re^2: Dereferencing Performance - Confused by Results
by jmmitc06 (Beadle) on Oct 04, 2014 at 19:16 UTC

    Thanks, the Alias module really made a big difference. It gives some strange errors in my multiprocessed code (using threads):

     "thread x terminated abnormally: can't use an undefined value as a hash reference at [program_name] line [line with alias]", <[file I'm opening]> [random line]

    It must be something involving the order in which alias and threads get around to doing that voodoo they do so well, because sometimes it works fine other times it gives errors. Oh well, it's a cool module I think I'll find many uses for it and it makes it fast enough that multiprocessing isn't really necessary.

      It gives some strange errors in my multiprocessed code (using threads):

      Aliasing won't work across threads; you'd need to use threads::shared data.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

        BroswerUK, Sorry to keep this thread going but could you give a little more detail about this?

        The code above is part of the subroutine &find_symmetry which the "threads" execute. I feed the threads input data, a string, from a queue and they output a hash ref to another queue. Other than the queues, the threads do not share variables between them. I would like them to be able to use the alias functionality shown above in that portion of code, which is part of &find_symmetry. So to me, I'm not wanting to alias across threads but rather within threads? Is this impossible?

        More confusing to me, sometimes they will work fine if I keep retrying to run the script. They either fail very early or it will work fine and the output from the program is correct. I don't understand this, I would understand if it always failed or always worked, but on the same input every time, the sometimes working behavior confuses me. I think this requires sharing the code for the subroutine to give some context, there is more but all of the aliasing would be within here. Basically the input queue has a list of files, each thread opens the file, parses the file and makes the $compound hash ref. This is given to two subs to make Mapping and Adjacency, then to &find_symmetry, then the ref is placed on the output queue.

        sub find_symmetry { my $compound = shift @_; $$compound{Symmetries} = []; my $M_matrix = $$compound{Mapping}; my $A_matrix = $$compound{Adjacency}; my @isomorphs; my $row = 0; my $dimension = $#$A_matrix; my @indices = (0..$dimension); my @reverse = reverse @indices; my @used = map {0;} (@indices); my @mapping = map {-1;} (@indices); my (@sm_rows, @same_rows); foreach my $row (@$A_matrix) { push @sm_rows, $row; } foreach my $row (@$M_matrix) { push @same_rows, $row;} my $equate_sub = sub { for (reverse(0..$row)) { my (@smrow, @omrow); alias @smrow = @{$sm_rows[$_]}; alias @omrow = @{$sm_rows[$mapping[$_]]}; for (0..$row) { if ($smrow[$_] != $omrow[$mapping[$_]]) { return 0; } } } return 1; }; LOOP: while(1) { do { $mapping[$row] ++ } while ( $mapping[$row] <= $dimension and +($used[$mapping[$row]] == 1 || $same_rows[$row][$mapping[$row]] == 0 +)); next if ($mapping[$row] > $dimension); if ( $row == $dimension ) { OUTER: { for (@indices) { my (@smrow2, @omrow2); alias @smrow2 = @{$sm_rows[$_]}; alias @omrow2 = @{$sm_rows[$mapping[$_]]}; for (@reverse) { if ( $smrow2[$_] != $omrow2[$mapping[$_]] ) { last OUTER; } } } push @{$$compound{Symmetries}}, [@mapping]; # print join(",",@mapping), "\n"; } } elsif ( $row == 0 or &$equate_sub == 1 ) { $used[$mapping[$row]] = 1; $row++; } } continue { while ( $row >= 0 and $mapping[$row] >= $dimension ) { last LOOP if ($row == 0); $mapping[$row] = -1; $row --; $used[$mapping[$row]] = 0; } } $$compound{Mapping} = undef; $$compound{Adjacency} = undef; # return \@isomorphs; }

        Any clarity you can give on this would be great, threads always confuse me with what sometimes works with them and what sometimes won't. They are really useful but sometimes annoying.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1102798]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2024-04-19 08:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found