468,511 Members | 1,524 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,511 developers. It's quick & easy.

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

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:
Jul 23 '05 #1
3 997

Just that wrote:
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

Jul 23 '05 #2
Just that wrote:
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.

--
Scott McPhillips [VC++ MVP]

Jul 23 '05 #3
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.)

Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by Niyazi | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.