If your method has gaurd clauses to ensure only valid objects are added to the collection then there is a chance your code is being wasteful. The early exit event that occurs when the gaurd clauses are true will mean that the original object copy was not required.
You can improve this by doing a const pass-by-reference. You then use the const reference to evaluate your gaurd clauses. If you exit early, all you have spent is 4 bytes on the stack (32bit of course) and you save on the time it takes to copy.
If your code then needs its own copy of the data, create it as a local variable using the const reference as parameter for the copy constructor.
The benefits are directly related to the following:
- how many chances for early exit exist
- the size of the object begin copied
- whether a deep or shallow copy is occuring
- how long it takes to copy
- the frequency of early exit calls in relation to copy calls
No comments:
Post a Comment