Any performance diference ?(operators)

Started by
5 comments, last by Vaalegk 21 years, 4 months ago
Hi,my question is : Is there any diference between overloading a operator or writing a general operator, well a dont know if the question is clear, but mybe the fallowing code can explain it better class Vector { "some members" "some methods if needed" Vector operator + (Vector &r); }; OR class Vector { "some members" "some methods if needed" }; Vector operator+(Vector &a,Vector &b); I mean , is there any performance hit? wich one is better?
Advertisement
Well, it seems to me the member function would be better, since it only requires pushing a reference to one vector onto the stack for the function call (instead of two).

But someone who knows better about the architecture of functions could probably tell you better.

http://roninmagus.hopto.org
acronymfinder.com - Find any acronym you need!
There's no performance difference. Implement operations outside the class if possible with the class' public interface, otherwise implement the operations inside the class. Also take a look at Boost operators to reduce your workload. The arguments should have const modifier.

[edited by - civguy on December 4, 2002 3:32:38 PM]
The two are identical in performance. Ronin, all member functions require an extra pointer to be put on the stack, the "this" pointer.

Don''t listen to me. I''ve had too much coffee.
quote:Original post by Ronin Magus
Well, it seems to me the member function would be better, since it only requires pushing a reference to one vector onto the stack for the function call (instead of two).

But someone who knows better about the architecture of functions could probably tell you better.

http://roninmagus.hopto.org
acronymfinder.com - Find any acronym you need!


The this pointer needs to be pushed on the stack to, so I don''t think there is a difference in performance, in both cases they need to push two things on the stack.
"THE INFORMATION CONTAINED IN THIS REPORT IS CLASSIFIED; DO NOT GO TO FOX NEWS TO READ OR OBTAIN A COPY." , the pentagon
Thank you guys that''s all i need to know
You should make it a member function without a compelling reason to do otherwise. The possibility that it might make a performance differance is not a compelling reason. That you actually have a performance problem that can be resolved by doing that is a compelling reason. Big differance between the two. It isn''t going to take much to change it should you ever find a compelling reason to do so. Far too many people spend far too much time optimizing something that makes no differance while failing to optimize something that does. I''ve spent many years optimizing other people''s code and personally I would prefer most make no attempt to do so. A well coded program is easily optimized even if no consideration is given to optimization. People who do a bunch of screwy things in the name of performance most often create programs that require major modifications in order to resolve the real performance problems. The worst cases being where what they did in the name of performance is the actual cause of the performance problem. As a general rule you cannot guess what you would know if you only knew more.
Keys to success: Ability, ambition and opportunity.

This topic is closed to new replies.

Advertisement