473,626 Members | 3,952 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

performance of static member function vs. instance member function

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?
Jul 22 '05 #1
4 7402
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.
Jul 22 '05 #2
"0to60" <ho************ ****@yahoo.com> wrote in message
news:Ca******** ***********@new ssvr31.news.pro digy.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

Jul 22 '05 #3
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

Jul 22 '05 #4

"0to60" <ho************ ****@yahoo.com> wrote in message
news:Ca******** ***********@new ssvr31.news.pro digy.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.
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
3467
by: Joost Ronkes Agerbeek | last post by:
Why is it allowed in C++ to call a static member function of an object through an instance of that object? Is it just convenience? tia, Joost Ronkes Agerbeek
11
4595
by: Roger Leigh | last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++ for Linux in 21 Days--I know there are better) states that "static member functions cannot access any non-static member variables". However, this doesn't seem entirely correct. It also doesn't mention whether static member functions can access protected and private member data and methods (and I couldn't spot this in the FAQ). I have a class row<Row> which derives from...
1
2027
by: Seb | last post by:
Is this efficient for a header file and a class? Does a 'static local' variable get created for each instance or include of the header file? Also, 'return _type().ToString();' does not seem to work. The error states: Object.h(16): error C2662: 'DataLib::Object::_type' : cannot convert 'this' pointer from 'const DataLib::Object' to 'DataLib::Object &' namespace DataLib
8
1824
by: SJ | last post by:
Hi: I have a class which has a static member function. The function implements something common to all instances. How can the static member function know all of the (Get access to the instances' handles) instances? Thanks in advance for any help
15
6577
by: Samee Zahur | last post by:
Question: How do friend functions and static member functions differ in terms of functionality? I mean, neither necessarily needs an object of the class to be created before they are called and either has access only to static members of the class (ie. assuming no object of the class is in scope - neither by arguments recieved nor by local declarations). Any static member function like this: //accessing static member i static void...
5
651
by: Tony Johansson | last post by:
Hello experts! Why is not possible to have virtual static members Many thnakn //Tony
7
3021
by: jon wayne | last post by:
Hi I'm a little confused here about the lifetime of a static pointer to member function, Say, I declare,define & initialize a static ptr to mem function in the header file of a class(the class is a helper Singleton class, and is created & deleted as and when required) - Where does the static pointer point to when every single instance of the class is deleted. I presume it'll be dangling pointer- as the code seg to which it's
11
3814
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you experts. I would like to produce Javascript classes that can be "subclassed" with certain behaviors defined at subclass time. There are plenty of ways to do this through prototyping and other techniques, but these behaviors need to be static and...
3
4442
by: chsalvia | last post by:
In generic programming, static member functions and functors seem to be very useful. I've noticed that some libraries take the approach of using templated static member functions to provide generic functionality, while others use functors. Take for example the std::string char_traits class. Here, a static member "eq" is defined to serve as a generic binary comparison function. My implementation defines it as; static bool eq(const...
0
8638
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8365
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8505
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7196
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6125
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5574
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1811
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1511
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.