Which one is faster ? Thank you very much...

J

Just that

Q: 1> if (M_only) do_abc
else do_xyz
2> (M_only)?do_abc:do_xyz;
supplementary question: in case the if-then structure gets more
complicated, between readability and speed which one would be
preferred and in what stage ? Be as clear as you can.

A:
 
D

davidrubin

Just said:
Q: 1> if (M_only) do_abc
else do_xyz
2> (M_only)?do_abc:do_xyz;
supplementary question: in case the if-then structure gets more
complicated, between readability and speed which one would be
preferred and in what stage ? Be as clear as you can.

A:

Generally speaking, one form is not faster than the other. I prefer to
use form 2 in assignments and for return values:

int messageType = (d_useOldMessagesFlag)
? MessageTypes::TYPE1
: MessageTypes::TYPE2;

or

return (stream) ? 0 : -1;

However, if you find yourself using the ?: operator a lot, you should
consider whether your design is too general. For example, a method like
'processMessage' that has a lot of branches in it for handling new and
old messages might be better served by two methods, 'processNewMessage'
and 'processOldMessage', or by a virtual function.

/david
 
S

Scott McPhillips [MVP]

Just said:
Q: 1> if (M_only) do_abc
else do_xyz
2> (M_only)?do_abc:do_xyz;
supplementary question: in case the if-then structure gets more
complicated, between readability and speed which one would be
preferred and in what stage ? Be as clear as you can.

A:

They are equivalent. You should expect the compiler to produce equally
efficient code either way. It probably will produce identical code
either way.
 
E

Evan

Answeres:

1. Probably neither will be faster by any noticable amount. However,
you can try each version and look at the assembly to see. I compiled
the following two programs on a Sparc with SunOS 5.9 and GCC 3.3 with
the command line 'gcc -S <filename>':

#include <stdlib.h>

int main()
{
if(rand() > 1)
{
return 0;
} else {
return 1;
}
}

and

#include <stdlib.h>

int main()
{
return (rand() > 1) ? 0 : 1;
}

In the first version, both execution paths had 14 instructions. In the
second, one path had 14 instructions and the other had 15.

However, this is probably highly dependent on the compiler
optimizations applied, and even more dependent on the platform you're
running on.

2. Don't worry about it. There will not be much of a difference in any
case. In general, you should not worry about microoperations like that
until you have working code that you have profiled and determined
problem location. Even so, you wouldn't want to change that unless it
was in a loop that was executed probably about a million times. ALWAYS
favor readability unless you are in that situation. (Or possibly in an
embedded environment where every bit of memory counts.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top