954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

performance benefit by not calling static member function by object

We can call static member function through both class name and objects...but we generally use class name to call static member function instead of objects....
Is there any performance benefit in it by using class name or is it just to remove confusion by not calling through object

SpS
Posting Pro
599 posts since Aug 2005
Reputation Points: 70
Solved Threads: 32
 

Well my first hunch was no ofcourse there would be no difference calling how you call a static member function, the class / object would only be used by the compiler to mangle the name... Thought I usually ask my compiler for help, a test program says more than a mear answer, anyway... my compiler (gcc 3.4.? ) says something in the lines of t is not a class or namespace (ie you cant use :: operator on a object, it is purely a naming operator)... I'm not a standards following guy, I do what works, and this does not ;) (ie I cant answer to if this should be possible or not)

perniciosus
Junior Poster in Training
78 posts since Nov 2005
Reputation Points: 29
Solved Threads: 4
 

Since the static method will always be called on the class instead of the instance you might get a tiny performance boost by using it like that, but most likely the compiler will optimise it that way for you.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

Well it seem I was wrong...

btw, no it will not be called through the object, it is only used for name resolution, only virtual functions will be called through virtual tables...

(try assembler output with your favorit compiler (gcc -O0 -c -S below)

sample output
(first one is test::t() ; second is t.t())

call	__ZN4test1tEv
	call	__ZN4test1tEv


here is a non static function call, with object on stack

leal	-1(%ebp), %eax
	movl	%eax, (%esp)
	call	__ZN4test2t2Ev


And here are a virtual call, with same object on stack but through pointer ofcourse...

movl	-28(%ebp), %eax
	movl	(%eax), %edx
	movl	-28(%ebp), %eax
	movl	%eax, (%esp)
	movl	(%edx), %eax
	call	*%eax


else it would look like this (trough stack variable)

leal	-24(%ebp), %eax
	movl	%eax, (%esp)
	call	__ZN4test2t3Ev


Or like this (through obscured reference so it can not know the type)

movl	-32(%ebp), %eax
	movl	(%eax), %edx
	movl	-32(%ebp), %eax
	movl	%eax, (%esp)
	movl	(%edx), %eax
	call	*%eax


All compiled unoptimized ofcourse

perniciosus
Junior Poster in Training
78 posts since Nov 2005
Reputation Points: 29
Solved Threads: 4
 

Just saw the disassembly code through VC6.0...In case of object it automatically gets optimised to classname::static function call

SpS
Posting Pro
599 posts since Aug 2005
Reputation Points: 70
Solved Threads: 32
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You