Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > performance of static member function vs. instance member function

Reply
Thread Tools

performance of static member function vs. instance member function

 
 
0to60
Guest
Posts: n/a
 
      11-20-2003
I don't know if I have that terminology right, but does anyone know if
static member functions (or free standing functions for that matter) are any
less overhead than actual member functions that operate on an instance of
that class?

I'm writing a math calculator that responds to real time data and it needs
to be FREAKY fast. To minimize the number of arguments that certain methods
take, I have stored some data within a class and made those methods operate
on that instance's data. But other functions DON'T operate on that data;
they need data passed to them. Should I make those latter functions static,
instance members, or maybe even free standing functions?


 
Reply With Quote
 
 
 
 
lilburne
Guest
Posts: n/a
 
      11-20-2003
0to60 wrote:

> I don't know if I have that terminology right, but does anyone know if
> static member functions (or free standing functions for that matter) are any
> less overhead than actual member functions that operate on an instance of
> that class?


If there is a difference you'll be hard pressed to measure
it. I'm guessing but you'll probably be able to do at least
100 function calls (static or member) for each sqrt you do.

The only way to know for sure is to profile the code
produced for the particular compiler/platform that the code
has to run on.


 
Reply With Quote
 
 
 
 
David White
Guest
Posts: n/a
 
      11-20-2003
"0to60" <holeshot60_nospam_@yahoo.com> wrote in message
news:Cabvb.31516$. com...
> I don't know if I have that terminology right, but does anyone know if
> static member functions (or free standing functions for that matter) are

any
> less overhead than actual member functions that operate on an instance of
> that class?


It's compiler dependent. You'll have to do some tests. A given compiler on a
given machine might pass the 'this' pointer in a CPU register to a
non-static member function, but pass an explicit object pointer argument to
a static function on the stack (i.e., in regular RAM). I'd expect a good
compiler to make a non-static member function at least as efficient as a
static function that does the same thing, but you can only find out by
running tests.

> I'm writing a math calculator that responds to real time data and it needs
> to be FREAKY fast. To minimize the number of arguments that certain

methods
> take, I have stored some data within a class and made those methods

operate
> on that instance's data. But other functions DON'T operate on that data;
> they need data passed to them. Should I make those latter functions

static,
> instance members, or maybe even free standing functions?


Any non-virtual member functions that don't use any member data can be made
static. I'd expect a slight improvement in speed because no 'this' pointer
needs to be passed to them. But, again, you can't be sure without running
tests.

DW



 
Reply With Quote
 
David White
Guest
Posts: n/a
 
      11-21-2003
I forgot to mention that it's best to use inline functions, whether static
or non-static, especially if the function does very little work.

DW



 
Reply With Quote
 
jeffc
Guest
Posts: n/a
 
      11-21-2003

"0to60" <holeshot60_nospam_@yahoo.com> wrote in message
news:Cabvb.31516$. com...
> I don't know if I have that terminology right, but does anyone know if
> static member functions (or free standing functions for that matter) are

any
> less overhead than actual member functions that operate on an instance of
> that class?
>
> I'm writing a math calculator that responds to real time data and it needs
> to be FREAKY fast. To minimize the number of arguments that certain

methods
> take, I have stored some data within a class and made those methods

operate
> on that instance's data. But other functions DON'T operate on that data;
> they need data passed to them. Should I make those latter functions

static,
> instance members, or maybe even free standing functions?


For "freaky" speed, you should be using inline functions and non-virtual
functions. There is no speed difference I'm aware of related to the
language per se between static non-static functions.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can a static member function access non-static member? dolphin C++ 3 12-05-2007 12:39 PM
What is the correct grammar to make a function call by using static member data which is a pointer to a ordinary class member function? zaeminkr@gmail.com C++ 3 07-06-2007 12:50 PM
How do you call a regular member function from a static member function? aling C++ 6 10-30-2005 04:38 AM
Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class. DJ Dev ASP .Net 3 02-08-2004 04:19 PM
Deleting class instance from static member function? lallous C++ 5 10-02-2003 02:11 PM



Advertisments