473,406 Members | 2,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Class efficiency question

I have a general performance question related to some code I am writing.
Consider the following class:

class FuncClass {
public:
FuncClass(unsigned short FunctionCode);
~FeuronClass();
double GetResult();
void Eval(double In1, double In2);

private:
unsigned short FunctionCode;
double Result;
};

FuncClass::FuncClass(unsigned short FunctionCodeIn) {
Result = 0;
FunctionCode = FunctionCodeIn;
}

FuncClass::~FuncClass() {
}

double FuncClass::GetResult() {
return(Result);
}

void FuncClass::Eval(double In1, double In2) {
switch (FunctionCode) {
case 0: // Addition
Result = In1 + In2;
break;
case 1: // Subtraction
Result = In1 - In2;
break;
case 2: // Multiplication
Result = In1 * In2;
break;
default: // Division
if (In2 != 0) {
Result = In1 / In2;
}
} // Imagine there are actually hundreds of different functions like the
four above
}

Every instance of this class is assigned a FunctionCode according to the
function that it needs to perform. The FunctionCode for an instance never
changes for the entire life of the instance. The question I have is whether
a compiler will allocate more memory than necessary for this instance to
accomodate all the functions that the instance _can_ do instead of just
allocating the memory for the one function that it _will_ do. Would it be
more efficient to have many derived classes each with a virtual Eval
function that does only one function so that I would just instantiate the
specific class for the function I want to do? (Really this just means
moving the switch statement out of the class and into the instantiating
code).

Also, would there be any speed benefit in changing my approach, or just a
reduction in memory (or neither) ??
Jul 19 '05 #1
2 1595

"Victor Hannak" <vi***********@nospam.kodak.com> wrote in message news:bk**********@news.kodak.com...
I have a general performance question related to some code I am writing.
Consider the following class:

Member functions are shared accross all class isntances. No matter how many FuncClass
objects you create, there is only one Eval function. It's only the non-static data members
you need to worry about.

However, having a switch on some "object type" field generally defeats the whole
purpose of OO. Having derived classes for each operation using a virtual Eval
method would be more OO. Of course, there may be reasons you may not want
to do that (like you want to keep these things in an array or such).

Jul 19 '05 #2

"Victor Hannak" <vi***********@nospam.kodak.com> wrote in message
news:bk**********@news.kodak.com...
Every instance of this class is assigned a FunctionCode according to the
function that it needs to perform. The FunctionCode for an instance never
changes for the entire life of the instance. The question I have is whether a compiler will allocate more memory than necessary for this instance to
accomodate all the functions that the instance _can_ do instead of just
allocating the memory for the one function that it _will_ do. Would it be
Functions are not allocated per instance like that. If I have classes A1
and A2 that contain the same amount of data, then usually sizeof(A1) ==
sizeof(A2) even if A1 has a hundred functions and A2 has none.
Also, would there be any speed benefit in changing my approach, or just a
reduction in memory (or neither) ??


How do you know you need to worry about time or memory at this point? Have
you profiled the code to make sure?

Since you're using C++, you should assume OO (which was generally intended
for this kind of thing) is best here, unless you can show (preferably with a
profiling tool) that the performance on your intended platform would be much
better using a switch statement - this is rather unlikely since polymorphism
is _usually_implemented_ in a conceptually similar way, except objects store
virtual table pointers instead of enumerations, and the function is found by
applying an offset to the virtual pointer instead of finding an offset by
way of a jump table.
Jul 19 '05 #3

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

Similar topics

66
by: Mike Stenzler | last post by:
I am new to Template programming and I would like to create an array of user-defined class objects using MFC CArray. Normally I would use linked list processing - create an object class and then an...
6
by: Julie | last post by:
What would be the primary motivation to make a class 'sealed' (meaning that you can't derive from it) in C++? (I understand that there is currently no sealed keyword in C++, but that there are...
13
by: Bryan Parkoff | last post by:
I have created three classes according to my own design. First class is called CMain. It is the Top Class. Second class and third class are called CMemory and CMPU. They are the sub-classes....
7
by: Wellu Mäkinen | last post by:
Hi all, let's say I have a need to store 4 int values in somekind of a structure. I have atleast two options: class A { public: int a, b, c, b;
15
by: Steven T. Hatton | last post by:
The following may strike many of you as just plain silly, but it represents the kind of delelima I find myself in when trying to make a design decision. This really is a toy project written for...
11
by: dimension | last post by:
If my intentions are to create objects that encapsulates data rows in a table, is it better to use a Struct or Class? Based on what i read, if my objects will simply have get and set methods,...
1
by: Diffident | last post by:
Hello Guys, I have a question with regards to the efficiency and best practices in .NET framework. We are currently using Webservices as normal classes. The way we are invoking methods in...
17
by: wackyphill | last post by:
If you have a function that basically creates an object/struct that holds data only in a function and wish to return the object/struct from the function which is faster a class object or struct? ...
1
by: Tomás | last post by:
dynamic_cast can be used to obtain a pointer or to obtain a reference. If the pointer form fails, then you're left with a null pointer. If the reference form fails, then an exception is thrown....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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...
0
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,...

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.