0

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

5
Contributors
10
Replies
11
Views
12 Years
Discussion Span
Last Post by johnson9000
0

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?

0

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

0

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

0

> 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.

0

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.

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.