in reply to Time efficiency of object creation in Perl (or the "bless" function)

ended up taking about half a day to run as opposed to the 30 seconds

Given the 144,000% slowdown you've described, I surprised that you need to ask :)

If your processing is cpu-intensive, the simple answer is no. And the more sophisticated the OO, the worse it gets.

If your application is IO-bound--web work; DB work or highly interactive--then you can "get away with it", but for anything requiring real processing with time pressure, you're better off without OO in Perl.

There are ways to mitigate some of the problems you've encountered. For most applications requiring 1000s of entities, those entities tend to group naturally into sets or collections, with the same processing being applied to whole collections rather than individual entities.

If you design your classes such that they represent a complete set of similar entities (stored internally as an array or hash), and the methods process the entire set, or large subsets--ie. you move the loops internal to the methods--then the OO overheads get amortised across large volumes of processing and so become minimal in the overall scheme of things.

This makes a lot of sense in other ways also. It is rare for an application that manipulates 1000s of objects to name them all individually. You almost always end up putting the object refs into arrays in order to loop over them. So, moving the arrays inside the OO and having methods that process the internal array of entities (not objects) either en-masse; or in large range-defined chunks, can yield benefits of clearer code structure at both the caller and internal levels. And that is after all the best reason for using OO.

But OO for its own sake is pointless at best; and if there is a timeliness component to your application spec, something far more detrimental.


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.
  • Comment on Re: Time efficiency of object creation in Perl (or the "bless" function)

Replies are listed 'Best First'.
Re^2: Time efficiency of object creation in Perl (or the "bless" function)
by kikumbob (Novice) on Jun 03, 2011 at 11:10 UTC
    I think the fact that I first learnt to program in Java has meant that I am perhaps a little too fond of using objects and object methods wherever I can. I think for my purposes the correct thing to do is probably, as you say, use objects as a way of organizing the collections I am processing.
      I think the fact that I first learnt to program in Java has meant that I am perhaps a little too fond of using objects and object methods wherever I can.

      Yes. Enforced habits are hard to break and everything is an object one of the hardest.

      One of many nice things about Perl is that it allows you to be judisious with your choices. By using OO at the higher levels of your application, and efficient coding techniques internally to those high level objects, you can get the best of both worlds.


      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.