Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^3: Is there any Performance difference between 'Create new object' vs 'reset value and reuse object'?

by davido (Cardinal)
on Jan 11, 2016 at 18:06 UTC ( [id://1152499]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Is there any Performance difference between 'Create new object' vs 'reset value and reuse object'?
in thread Is there any Performance difference between 'Create new object' vs 'reset value and reuse object'?

I would never say that the general best practice is to reuse an object rather than create a new one. The best practice may be entirely the opposite. In the general case, NOT tinkering with an object's guts is the best practice. In the specific case where time efficiency trumps all other concerns, and the object is cheaper to reuse than to regenerate, then it may be a useful practice. That's about the strongest assertion you could make.

If it is useful in your case to reuse an object, it may be a good practice to create a subclass of the object that provides a "reset" method, or that allows "new" to be called on the object, with "reset" semantics, although that might confuse people who expect $obj->new to create a clone. The point being, such a subclass would allow the calling code to avoid touching the object's internals, which is a better practice.


Dave

Replies are listed 'Best First'.
Re^4: Is there any Performance difference between 'Create new object' vs 'reset value and reuse object'?
by khandielas (Sexton) on Jan 11, 2016 at 18:28 UTC

    Thanks, Dave.

    That is exactly what I mean. I think it is good to create a method to partially reset the values/properties so that this object can be reused properly. So basically the code will be like below:

    my $obj = myClass->new(); while ( ... ) { $obj->reset(); $obj->otherMethods(); .... } $obj->cleanup(); #Instead of while( ... ) { my $obj = myClass-<new(); $obj->otherMethods(); }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-03-28 10:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found