473,378 Members | 1,469 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Order of statements using if

Hi, everybody:

A quick question regarding how to write if/then blocks.

Do C++ compilers care in terms of execution (i.e., efficiency) which
block in an if/then/else loop is executed? That is, will going to the
"else" block be less efficient than using the "then" block?

My question comes because I want to know if there is any advantage in
writing the test condition to favor one block over the other. (For the
code I'm working with, one case is definitely preferred over the
other--at least 2 to 1.)

Thanks,

--AEI

Sep 19 '07 #1
9 2124
Hi,

I believe the x86 type of procesors expect to fall through (thus if compiled
'litterally' the if condition being true).

However if your compiler does or doesn't optimize for that one can only
guess. I would recommend to just try it (and still then it might depend on
the condition).
Regards, Ron AF Greve

http://www.InformationSuperHighway.eu

<ae******@gmail.comwrote in message
news:11**********************@k35g2000prh.googlegr oups.com...
Hi, everybody:

A quick question regarding how to write if/then blocks.

Do C++ compilers care in terms of execution (i.e., efficiency) which
block in an if/then/else loop is executed? That is, will going to the
"else" block be less efficient than using the "then" block?

My question comes because I want to know if there is any advantage in
writing the test condition to favor one block over the other. (For the
code I'm working with, one case is definitely preferred over the
other--at least 2 to 1.)

Thanks,

--AEI

Sep 19 '07 #2
On Sep 19, 9:32 pm, aeism...@gmail.com wrote:
Hi, everybody:

A quick question regarding how to write if/then blocks.

Do C++ compilers care in terms of execution (i.e., efficiency) which
block in an if/then/else loop is executed? That is, will going to the
"else" block be less efficient than using the "then" block?

My question comes because I want to know if there is any advantage in
writing the test condition to favor one block over the other. (For the
code I'm working with, one case is definitely preferred over the
other--at least 2 to 1.)
The exact implementation of if-else construct is not specified by the
standard, and hence the answer would vary depending on the platform
and implementation. In other words, trying to write if-else clause
based on certain heuristic might not help if you are writing code that
runs across platforms and implementations.

-N

Sep 19 '07 #3
Another thing to consider is that modern CPUs perform branch
prediction, so it might not really matter that much whether your
compiler optimizes in a certain particular way. From what I
understand the branch prediction in hardware is quite good these
days. Check out this article for more info:

http://en.wikipedia.org/wiki/Branch_prediction
Sep 19 '07 #4
Eric Johnson <er***********@gmail.comwrites:
Another thing to consider is that modern CPUs perform branch
prediction, so it might not really matter that much whether your
compiler optimizes in a certain particular way.
Yep, when code is run for the first time processor has no data about
execution of code and branches taken previously so it can only guess.
In x86 the guess is as follows: If the branch is forward it is guessed
not to be taken and if the branch is backward it is guessed to be taken.

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>---<jid:mina86*chrome.pl>--ooO--(_)--Ooo--
Sep 19 '07 #5
On Sep 19, 10:51 am, Eric Johnson <eric.eerri...@gmail.comwrote:
Another thing to consider is that modern CPUs perform branch
prediction, so it might not really matter that much whether your
compiler optimizes in a certain particular way. From what I
understand the branch prediction in hardware is quite good these
days. Check out this article for more info:

http://en.wikipedia.org/wiki/Branch_prediction
If there is code following both the "if" and the "else" case, there
has to be a branch to the common following code from the "if"
alternative and a branch "to" the conditional code for the "else"
alternative (maybe no branch to the common code, however).

If, however, there are a number of tests, the most common would be
most efficiently first.

So, if there is code following and only two alternatives and the the
excecution paths rejoin, there is likey to be no difference at all.

Sep 19 '07 #6
Michael Angelo Ravera <ma******@prodigy.netwrites:
On Sep 19, 10:51 am, Eric Johnson <eric.eerri...@gmail.comwrote:
>Another thing to consider is that modern CPUs perform branch
prediction, so it might not really matter that much whether your
compiler optimizes in a certain particular way. From what I
understand the branch prediction in hardware is quite good these
days. Check out this article for more info:

http://en.wikipedia.org/wiki/Branch_prediction

If there is code following both the "if" and the "else" case, there
has to be a branch to the common following code from the "if"
alternative and a branch "to" the conditional code for the "else"
alternative (maybe no branch to the common code, however).
But the branch in the "if part" is unconditional and as such does not
need a branch prediction.
If, however, there are a number of tests, the most common would be
most efficiently first.

So, if there is code following and only two alternatives and the the
excecution paths rejoin, there is likey to be no difference at all.
There is, as conditional jumps are those that make the difference.

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>---<jid:mina86*chrome.pl>--ooO--(_)--Ooo--
Sep 19 '07 #7
On Sep 19, 6:32 pm, aeism...@gmail.com wrote:
A quick question regarding how to write if/then blocks.
Do C++ compilers care in terms of execution (i.e., efficiency) which
block in an if/then/else loop is executed? That is, will going to the
"else" block be less efficient than using the "then" block?
Why? Does your profiler say that this is causing a bottleneck
in your code?
My question comes because I want to know if there is any advantage in
writing the test condition to favor one block over the other. (For the
code I'm working with, one case is definitely preferred over the
other--at least 2 to 1.)
The usual rule is to write the shorter block first, provided
that this doesn't make the conditional expression harder to
read. Worrying about performance in this sort of thing is a
total waste of time.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Sep 20 '07 #8
<ae******@gmail.comwrote in message
news:11**********************@k35g2000prh.googlegr oups.com...
Hi, everybody:

A quick question regarding how to write if/then blocks.

Do C++ compilers care in terms of execution (i.e., efficiency) which
block in an if/then/else loop is executed? That is, will going to the
"else" block be less efficient than using the "then" block?

My question comes because I want to know if there is any advantage in
writing the test condition to favor one block over the other. (For the
code I'm working with, one case is definitely preferred over the
other--at least 2 to 1.)
consider a large if.. else block
if ( a )
// do
else if ( b )
// do
else if ( c)
// do
else if ( d )
// do
else
// do

Normally they are quite fast, but if cycles are tight you want to ensure
that condition a will happen more than b, b more than the rest, etc...

For the program to check condition d, it will have had to check a,b and c.
If the conditional check is trivial it usually doesn't matter, a cycle or
two. If it is a more complicated conditional check then it can matter a
lot. A switch statement can be more effecient since it becomes pretty much
just a jump table, but then you have to be able to have an ordinal value to
check, which is not always possible.

Usually, however, people don't need to concern themselves with the
effecientcy of if else blocks as they don't take that much time. If you've
found that this is your bottle neck, the first thing to do is make sure the
conditions that happen most are at the top.
Sep 20 '07 #9
ae******@gmail.com writes:
Hi, everybody:

A quick question regarding how to write if/then blocks.

Do C++ compilers care in terms of execution (i.e., efficiency) which
block in an if/then/else loop is executed? That is, will going to the
"else" block be less efficient than using the "then" block?
Some compilers have an option to optimize the code depending on how
often particular branches are taken. In g++, you can use the
"-fprofile-arcs" option to instrument your code, run it on some sample
data to generate statistics, then re-compile with
"-fbranch-probabilities" to give the compiler a hint about how often
the various branches are taken. I haven't tried this, but I've heard
it gives a speedup in some cases. Perhaps your compiler has similar
options you could try.

Good luck!

---Scott.
Sep 21 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: ian justice | last post by:
Before i post actual code, as i need a speedyish reply. Can i first ask if anyone knows off the top of their head, if there is a likely obvious cause to the following problem. For the moment i've...
39
by: Nicolas Fleury | last post by:
In the following example: class MyMetaclass(type): pass class MyBaseType(object): __metaclass__ = MyMetaclass class MyType(MyBaseType): x = 4 y = 5 z = 6 Is there any way to modify...
10
by: Chris Gordon-Smith | last post by:
I am currently revisiting the code of a C++ application and restructuring the code where appropriate to make it consistent with the overall logical design. As part of this, I am looking at the...
1
by: Tom Schindl | last post by:
Hi, the following Statement worked on MySQL 4.0 but after upgrading to 4.1.12 on win32 the order is not working any more. Is this a known problem or is our SQL simply not useable on 4.1 or is...
10
by: sqlgoogle | last post by:
Hi I'm trying to update a db based on the select statement which has ORDER BY in it. And due to that I'm getting error which states that Server: Msg 1033, Level 15, State 1, Line 13 The ORDER...
2
by: champ.supernova | last post by:
Hi, I was hoping someone could help me with what I'm sure is a very simple problem...I just can't seem to find the syntax! I'm wanting to update the rows in 'tbl_consolidate' from 'tbl_hold',...
104
by: Beowulf | last post by:
I have the view below and if I use vwRouteReference as the rowsource for a combo box in an MS Access form or run "SELECT * FROM vwRouteReference" in SQL Query Analyzer, the rows don't come through...
7
by: Arnold | last post by:
Greetings Gurus, In a mainform's header, I have a combobox named comboStudents. The rowsource for this combobox is: SELECT -999 As StudentID, "<Add New Student>" As FullName, "aaa" As...
29
by: pb648174 | last post by:
I have the following basic statements being executed: Create a temp table, #TempPaging Insert Into #TempPaging (Col1, Col2) Select Col1, Col2 From SomeOtherTable Order By Col2, Col1 Select...
16
by: mdh | last post by:
May I ask the group the following: (Again, alas , from K&R) This is part of a function: while ( ( array1 = array2 ) != '\0' ); /* etc etc */ Is this the order that this is evaluated? ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.