Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > why are references so slow ?

Reply
Thread Tools

why are references so slow ?

 
 
Daniel Dünker
Guest
Posts: n/a
 
      09-26-2007
I have a script, with lots of deep recursion and noticed, that with the usage of references the whole programm becomes *much* slower (i noticed that in an earlier state, when i wanted to put something often used into a sub. The programm took at first less than 1 second, after i created the sub it took more than 5 seconds to complete.)
Now i am at a point where i want to use recursion, and perl says its even a deep recursion.
So, should i throw away all the parameters and just use global variables to get rid of referencing and dereferencing ?


--
Daniel Dünker <(E-Mail Removed)>
 
Reply With Quote
 
 
 
 
brian d foy
Guest
Posts: n/a
 
      09-26-2007
In article <(E-Mail Removed)>, Daniel
Dnker <(E-Mail Removed)> wrote:

> I have a script, with lots of deep recursion and noticed, that with the usage
> of references the whole programm becomes *much* slower (i noticed that in an
> earlier state, when i wanted to put something often used into a sub. The
> programm took at first less than 1 second, after i created the sub it took
> more than 5 seconds to complete.)
> Now i am at a point where i want to use recursion, and perl says its even a
> deep recursion.
> So, should i throw away all the parameters and just use global variables to
> get rid of referencing and dereferencing ?


Well, before you do anything, profile the application to see where the
slow parts really are. I would be surprised if it's the references, and
surprised if you haven't coded something that's an inefficient
algorithm (especially since you noted a deep recursion warning).

I talk about profiling in chapter 5, "Profiling", of Mastering Perl:

http://www252.pair.com/comdog/mastering_perl/Chapters/

Good luck
 
Reply With Quote
 
 
 
 
Brian McCauley
Guest
Posts: n/a
 
      09-26-2007
On Sep 26, 3:49 am, Daniel Dnker <(E-Mail Removed)> wrote:
> I have a script, with lots of deep recursion and noticed, that with the usage of
> references the whole programm becomes *much* slower (i noticed that in an
> earlier state, when i wanted to put something often used into a sub. The programm
> took at first less than 1 second, after i created the sub it took more than 5 seconds to
> complete.)


I'm not sure what this has to do with references but subroutine
calling it expensive in Perl. As are constructing and unpacking
argument lists.

> Now i am at a point where i want to use recursion, and perl says its evena deep
> recursion.


If you want to use recursion I don't think you can do it without
subroutines. You'd have to refactor your code to be iterative.

> So, should i throw away all the parameters and just use global variables to get rid
> of referencing and dereferencing ?


I think it would have been clearer if you'd given us an example but if
I've understood correctly you effectively have a bunch of variables
that you are just passing unchanged down the recursion. Yes I'd
replace these by variables that are by some definition "globals". That
is to say they are used inside a subroutine but are not passed as
arguments. There are two obvious ways to go about this...

# Original

sub foo {
my ($this,$that,$other) = @_;
# stuff using $this,$that,$other...
# ...but not changing $this or $that
foo ($this, $that, whatever);
# stuff...
}
######################################

# With closure
sub foo {
my ($this,$that,$other) = @_;
my $r;
$r = sub{
my ($other) = @_;
# stuff using $this,$that,$other...
$r->(whatever);
# stuff...
};
$r->($other);
undef $r; # break circular reference
}
####################################

# With package variables and local()

our ($this,$that);

sub r {
my ($other) = @_;
# stuff using $this,$that,$other...
$r->(whatever);
# stuff...
}

sub foo {
(local($this,$that),my ($other)) = @_;
r($other);
}


 
Reply With Quote
 
Daniel Dünker
Guest
Posts: n/a
 
      09-26-2007
On Wed, 26 Sep 2007 00:26:57 -0500
brian d foy <(E-Mail Removed)> wrote:

> In article <(E-Mail Removed)>, Daniel
> Dˆºnker <(E-Mail Removed)> wrote:
>
> > I have a script, with lots of deep recursion and noticed, that with the usage
> > of references the whole programm becomes *much* slower (i noticed that in an
> > earlier state, when i wanted to put something often used into a sub. The
> > programm took at first less than 1 second, after i created the sub it took
> > more than 5 seconds to complete.)
> > Now i am at a point where i want to use recursion, and perl says its even a
> > deep recursion.
> > So, should i throw away all the parameters and just use global variables to
> > get rid of referencing and dereferencing ?

>
> Well, before you do anything, profile the application to see where the
> slow parts really are. I would be surprised if it's the references, and
> surprised if you haven't coded something that's an inefficient
> algorithm (especially since you noted a deep recursion warning).
>
> I talk about profiling in chapter 5, "Profiling", of Mastering Perl:
>
> http://www252.pair.com/comdog/mastering_perl/Chapters/
>
> Good luck


Wow thanks, that's a very interesting tool, and of course you are right, there are still lot's of inefficient things in my script, but it seems that the slowest part's are actually really referencing and dereferencing, as for example:
4455 3.53946 3.40000 133: return \%pixels;
which is the part that takes the most time.

--
Daniel Dünker <(E-Mail Removed)>
 
Reply With Quote
 
Michele Dondi
Guest
Posts: n/a
 
      09-26-2007
On Wed, 26 Sep 2007 09:03:58 -0000, Brian McCauley
<(E-Mail Removed)> wrote:

>I'm not sure what this has to do with references but subroutine
>calling it expensive in Perl. As are constructing and unpacking
>argument lists.
>
>> Now i am at a point where i want to use recursion, and perl says its even=

> a deep
>> recursion.

>
>If you want to use recursion I don't think you can do it without
>subroutines. You'd have to refactor your code to be iterative.


(Mostly as an aside) if the recursion can be cast in the form of tail
recursion than much hassle can be eliminated, but existing perls
unfortunately don't handle it natively so one has to take care of that
"manually" with *magic* goto()s.


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      09-26-2007
Daniel =?UTF-8?B?RMO8bmtlcg==?= <(E-Mail Removed)> wrote:
>
> Wow thanks, that's a very interesting tool, and of course you are right,
> there are still lot's of inefficient things in my script, but it seems
> that the slowest part's are actually really referencing and
> dereferencing, as for example:
> 4455 3.53946 3.40000 133: return \%pixels;
> which is the part that takes the most time.


How do you know it is the reference, rather than the return, that is
taking the time? You could separate them into two different lines:

my $foo=\%pixels;
return $foo;

Also, I don't find Devel::SmallProf to be particularly reliable. Small
errors in its null compensation methods can lead to substantial spurious
timings.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      09-26-2007
Michele Dondi schreef:

> (Mostly as an aside) if the recursion can be cast in the form of tail
> recursion than much hassle can be eliminated, but existing perls
> unfortunately don't handle it natively so one has to take care of that
> "manually" with *magic* goto()s.


Can you show an example?

--
Affijn, Ruud

"Gewoon is een tijger."

 
Reply With Quote
 
Michele Dondi
Guest
Posts: n/a
 
      09-26-2007
On Wed, 26 Sep 2007 23:06:29 +0200, "Dr.Ruud"
<(E-Mail Removed)> wrote:

>Michele Dondi schreef:
>
>> (Mostly as an aside) if the recursion can be cast in the form of tail
>> recursion than much hassle can be eliminated, but existing perls
>> unfortunately don't handle it natively so one has to take care of that
>> "manually" with *magic* goto()s.

>
>Can you show an example?


It's surely discussed in HOP, but I don't know if it's online yet. No,
it isn't - see <http://hop.perl.plover.com/>. Then there's a free
online book (ongoing work as of when I bookmarked it) available at
<http://billhails.net/Book/> which talks about it, with examples, at
<http://billhails.net/Book/interpreter-0-0-10.html#tail-recursion>.


HTH,
Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      09-27-2007
Michele Dondi schreef:
> Dr.Ruud:
>> Michele Dondi:


>>> (Mostly as an aside) if the recursion can be cast in the form of
>>> tail recursion than much hassle can be eliminated, but existing
>>> perls unfortunately don't handle it natively so one has to take
>>> care of that "manually" with *magic* goto()s.

>>
>> Can you show an example?

>
> It's surely discussed in HOP, but I don't know if it's online yet. No,
> it isn't - see <http://hop.perl.plover.com/>. Then there's a free
> online book (ongoing work as of when I bookmarked it) available at
> <http://billhails.net/Book/> which talks about it, with examples, at
> <http://billhails.net/Book/interpreter-0-0-10.html#tail-recursion>.


Thanks.

--
Affijn, Ruud

"Gewoon is een tijger."

 
Reply With Quote
 
brian d foy
Guest
Posts: n/a
 
      09-27-2007
In article <(E-Mail Removed)>, Daniel
Dnker <(E-Mail Removed)> wrote:


> Wow thanks, that's a very interesting tool, and of course you are right,
> there are still lot's of inefficient things in my script, but it seems that
> the slowest part's are actually really referencing and dereferencing, as for
> example:
> 4455 3.53946 3.40000 133: return \%pixels;
> which is the part that takes the most time.



remember that one line of a report like that doesn't mean much devoid
of the context of everything else going on. Feel free to post more of
the report

also, I don't know what is going on in that subroutine, but returning a
reference, which is just a scalar, shouldn't be talking up that much
time. Can you show the subroutine?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: slow slow slow! Expert lino fitter Computer Support 5 12-12-2008 04:00 PM
Re: slow slow slow! chuckcar Computer Support 0 12-10-2008 11:25 PM
Re: slow slow slow! Beauregard T. Shagnasty Computer Support 2 12-10-2008 09:03 PM
Re: slow slow slow! Expert lino fitter Computer Support 0 12-10-2008 02:33 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM



Advertisments