http://www.perlmonks.org?node_id=1047924


in reply to how to improve the performance of a perl program

Do you consider Devel::NYTProf a viable option?


Enjoy, Have FUN! H.Merijn

Replies are listed 'Best First'.
Re^2: how to improve the performance of a perl program
by ghosh123 (Monk) on Aug 05, 2013 at 16:50 UTC

    Yes, I consider so. But I need an example to better understand it. Also can you please explain me how and why my no.1 point which is chain->of->accessors could be a problem ?
    Please also explain how can I use Class::Accessor ?
    Thanks.

      $chain->of->accessors often isn't a problem. However, if you've identified a bottleneck, you may need to consider what work is being done:

      • An inheritance lookup is done to decide which "->of" pertains to object $chain. This is a simple operation, but is more expensive than a direct subroutine call.
      • A subroutine call is executed. This involves pushing the call-frame onto the call-stack, and within the subroutine popping off items from the param stack. This is usually pretty quick, but considerably slower than looking up the value of a variable.
      • The accessor must do whatever work it must do. Perhaps it does no work other than returning a value. Maybe it's computing the 1-Billionth prime newly on each call. The cost depends entirely on how the accessor is implemented.
      • Next an object is returned so that ->accessors may be invoked on it. The return involves popping the current sub off the call-stack.
      • This process repeats for ->accessors.

      If that sounds like a lot of work, you're jumping to conclusions. If you put all that inside of a tight loop, inside of an algorithm that computes the Cartesian product of two human DNA sequences, yes... it's way too much work to be doing inside of a tight loop. If you're diving into that chain of accessors only every so often, then all the object lookup and call-stack work really fades into the background, and you maybe need to just consider how much work the individual accessors are doing internally. But until you've identified bottlenecks, it's a total waste of your time and the salary your employer pays you to just start making untested assumptions about performance, because you could be looking completely in the wrong places.

      As for your question about how to use Class::Accessor, before I explain how, let me ask you why you think you want to use it. Class::Accessor has about as much to do with code speed optimization as cruise controls have to do with drag racing. So if you do understand that Class::Accessor isn't a means to code speed optimization, and you still need it, then I suggest you read its documentation and ask a specific question about it, rather than asking "how to use" it when "how" is demonstrated right in its documentation.


      Dave