472,961 Members | 1,538 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 1047

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number....
65
by: Skybuck Flying | last post by:
Hi, I needed a method to determine if a point was on a line segment in 2D. So I googled for some help and so far I have evaluated two methods. The first method was only a formula, the second...
19
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number....
13
by: Niyazi | last post by:
Hi I have a report that I have to run it monthly in my machine. My code in VB.NET and I access AS400 to get data, anaysie it and send into pre formated Excel sheet. The data consist of 9000...
16
by: John Salerno | last post by:
My initial feeling is that concatenation might take longer than substitution, but that it is also easier to read: def p(self, paragraph): self.source += '<p>' + paragraph + '</p>\n\n' vs. ...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.