Which method below requires less processor time?

class obj{
public:
void DoSomeThing(){};
};


int main() {

obj *p = new obj;
p->DoSomeThing();

// OR

obj theObj;
theObj.DoSomeThing();

return 0;
}

Thanks for your help.
W.Johnson

"Premature optimization is the root of all evil"
--Donald Knuth

Why do you ask?

I am writing a program that runs a loop making quick function calls to simple objects (nodes in a SANN).
I better question is:
Which approach below is faster?

class obj{
public:
void DoSomeThing(){};
};

typedef obj* p;

int main() {

vector<obj> theVector;
for(short i=0;i<10;i++) theVector.push_back(obj);
for(short i=0;i<10;i++) theVector[i++].DoSomeThing();

//OR

vector<p> pVector;
for(short i=0;i<10;i++) pVector.push_back(new obj);
for(short i=0;i<10;i++) pVector[i++]->DoSomeThing();

return 0;
}

Is there even a difference?

My program already runs using "vector.push_back(obj);"
But I could make it simpler with "vector.push_back(new obj);"

thanks for your help
W.Johnson

obj theObj;
theObj.DoSomeThing();

return 0;
}

In my opinion....

Hello,

Perhaps your environment has what is called a profiler. When I was doing coding, I would run it through from time to time to see how long the system stayed within the functions.

Christian

> Which approach below is faster?
Generally, if you're working through a pointer, there's an extra level of indirection. Therefore, in theory, going indirectly though a pointer is slower. In reality, if the compiler doesn't optimize the difference away, it'll be infintesimally small. So you should use whatever more clearly shows your intentions.

http://www.daniweb.com/techtalkforums/post98186-19.html
Check out Rob Pike's rule #2.

What is the:

2. Use the ``telephone test'' for readability.

Rob Pike

2 Measure. Don’t tune for speed until you’ve measured, and even then don’t unless one part of the code overwhelms the rest.

But in regard to the question you asked, I think that the "telephone test" is that you should be able to describe code over the phone. If it's too complicated to do that, it's too complicated.

[edit]I found this perhaps better explanation.

The telephone test is simply asking whether someone could understand your code when read over the telephone.

So I'll just write the code the clear simple way, then test to see if there is a significant difference in speed.

Thanks for your help.
W.Johnson

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.