473,320 Members | 2,052 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,320 software developers and data experts.

Blocks, or not?

Rob
Functionally, the two following pieces of code are the same:

if(p == NULL) {

break;
}

if(p == NULL)
break;

However, I am wondering whether there are any performance implications
in choosing one over the other. Is there any extra overhead created by
defining a block? Will a compiler optimize the former code to the
latter? Does it even need to?

The reason I ask is, I'd much prefer to go with the former code, simply
because I prefer the appearance.

Nov 15 '05 #1
18 1100
"Rob" <io*********@hotmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Functionally, the two following pieces of code are the same:

if(p == NULL) {

break;
}

if(p == NULL)
break;

However, I am wondering whether there are any performance implications
in choosing one over the other. Is there any extra overhead created by
defining a block?
The former may take a few microseconds longer to compile, but I doubt that
is important.
Will a compiler optimize the former code to the latter? Does it even need
to?
I would be surprised to find any compiler which generated different code for
the alternatives above.
The reason I ask is, I'd much prefer to go with the former code, simply
because I prefer the appearance.


FWIW, I would probably write:

if (!p) break;

Again, I would not expect any meaningful difference in compilation time, and
still the same code generated as for the above alternatives.

Alex
Nov 15 '05 #2
Rob wrote on 21/08/05 :
Functionally, the two following pieces of code are the same:

if(p == NULL) {

break;
}

if(p == NULL)
break;

However, I am wondering whether there are any performance implications
in choosing one over the other.


No difference.

I personnaly recommend the use of the first form, that is convenient
for debug and extensions.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

I once asked an expert COBOL programmer, how to
declare local variables in COBOL, the reply was:
"what is a local variable?"
Nov 15 '05 #3
Rob wrote:

Functionally, the two following pieces of code are the same:

if(p == NULL) {

break;
}

if(p == NULL)
break;

However, I am wondering whether there are any performance implications
in choosing one over the other. Is there any extra overhead created by
defining a block? Will a compiler optimize the former code to the
latter? Does it even need to?

The reason I ask is,
I'd much prefer to go with the former code, simply
because I prefer the appearance.


I prefer to use braces with all ifs and elses and loops.
It can make things simpler when it's time to modify the code.

Once, when modifying code, I made a mistake that could have
been avoided if I had originally written the code with braces.
I can't remember what the mistake was.

--
pete
Nov 15 '05 #4
pete wrote on 21/08/05 :
Once, when modifying code, I made a mistake that could have
been avoided if I had originally written the code with braces.
I can't remember what the mistake was.


I remember this (well, sort of) :

if (c == 'a')
gotyxy(1,2); cprintf ('X');

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
-- Jerry Coffin in a.l.c.c++
Nov 15 '05 #5
"Rob" <io*********@hotmail.com> wrote:
# Functionally, the two following pieces of code are the same:
#
# if(p == NULL) {
#
# break;
# }
#
# if(p == NULL)
# break;
#
# However, I am wondering whether there are any performance implications
# in choosing one over the other. Is there any extra overhead created by
# defining a block? Will a compiler optimize the former code to the
# latter? Does it even need to?

A compiler using block level addressing instead of procedure level
could insert a few instructions to save and restore stack size on
block entry and exit. But I don't know if any such compilers still
exist, and even if they do, even moderate optimisation could elide
the instruction if it is a compound statement instead of a block.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
We found a loophole; they can't keep us out anymore.
Nov 15 '05 #6
Rob wrote:
Functionally, the two following pieces of code are the same:

if(p == NULL) {

break;
}

if(p == NULL)
break;

However, I am wondering whether there are any performance implications
in choosing one over the other. Is there any extra overhead created by
defining a block? Will a compiler optimize the former code to the
latter? Does it even need to?
It makes no difference in compiled code. The compiler uses the braces to
understand where every block starts and ends... Nothing more.
The reason I ask is, I'd much prefer to go with the former code, simply
because I prefer the appearance.


Many say that you should use the former, since it is easier to read and
less error-prone in case you want to add something.

Even if it saves you only once from a mistake I believe it worths the
cost of typing 2 braces in every block...
--
one's freedom stops where others' begin

Giannis Papadopoulos
http://dop.users.uth.gr/
University of Thessaly
Computer & Communications Engineering dept.
Nov 15 '05 #7
SM Ryan wrote:
A compiler using block level addressing instead of procedure level
could insert a few instructions to save and restore stack size on
block entry and exit.


Huh, probably not if no variable local to the block is declared.
I doubt you can find so dumb a compiler...
Nov 15 '05 #8
Rob wrote:
Functionally, the two following pieces of code are the same:

if(p == NULL) {

break;
}

if(p == NULL)
break;

However, I am wondering whether there are any performance implications
in choosing one over the other. Is there any extra overhead created by
defining a block? Will a compiler optimize the former code to the
latter? Does it even need to?


This wins the Grand Prize for worrying about the
wrong things.

Let me put it this way: Any possible performance
difference between these two forms would be utterly
negligible. If you were concerned about differences
on this scale, you would not be writing C. Heck, you
would not be writing assembler, nor even machine code:
You would be designing your own custom chip with your
program burned into its silicon (and none of that wasteful
microcode, either).

It's like a 100-kilo dieter worrying about a weight
gain of three micrograms -- no, more like three quarks.

Get, as they say, a life.

--
Eric Sosman
es*****@acm-dot-org.invalid
Nov 15 '05 #9
pete wrote:
Rob wrote:

Functionally, the two following pieces of code are the same:

if(p == NULL) {

break;
}

if(p == NULL)
break;

However, I am wondering whether there are any performance
implications in choosing one over the other. Is there any extra
overhead created by defining a block? Will a compiler optimize
the former code to the latter? Does it even need to?
.... snip ...
I prefer to use braces with all ifs and elses and loops.
It can make things simpler when it's time to modify the code.

Once, when modifying code, I made a mistake that could have
been avoided if I had originally written the code with braces.
I can't remember what the mistake was.


You avoid that problem if you write the braceless versions as:

if (condition) action();

i.e. in one line. You also save vertical space and make your code
more readable.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson

Nov 15 '05 #10
>> Once, when modifying code, I made a mistake that could have
been avoided if I had originally written the code with braces.
I can't remember what the mistake was.


You avoid that problem if you write the braceless versions as:

if (condition) action();

i.e. in one line. You also save vertical space and make your code
more readable.


You can just as well make the mistake:

if (condition) action(); action2();

and I'll disagree that the code is more readable if either condition
or action() takes up much more horizontal space than shown above.
In other words:

if (color != NULL && color[0] != '\0') printf("A color (%s) has already been assigned to %s\n", color, objectname);

is better written on multiple lines.

Gordon L. Burditt
Nov 15 '05 #11

"Rob" <io*********@hotmail.com> wrote

Functionally, the two following pieces of code are the same:

if(p == NULL) {

break;
}

if(p == NULL)
break;

However, I am wondering whether there are any performance implications
in choosing one over the other. Is there any extra overhead created by
defining a block? Will a compiler optimize the former code to the
latter? Does it even need to?

The reason I ask is, I'd much prefer to go with the former code, simply
because I prefer the appearance.

The cost is two extra characters in your source, plus extra space. There
will be no difference in the compiled code.
So as far as the narrow technical issues are concerned, go with the former.

However you want to consider whether you are writing beginner's C. For a
leaner, code set out with lots of braces, lots of whitespace, and lots of
comments is easier to read, because he doesn't really know the syntax very
well. To the expert this becomes annoying.

I would write

if(!p)
break;

not to save typing or ACSII source, but because the break statement must
already be nested within a block, and to introduce an extra level of curly
braces just confuses things.
Nov 15 '05 #12
Gordon Burditt wrote: (and failed to preserve attributions)

.... snip ...

You avoid that problem if you write the braceless versions as:

if (condition) action();

i.e. in one line. You also save vertical space and make your code
more readable.


You can just as well make the mistake:

if (condition) action(); action2();

and I'll disagree that the code is more readable if either condition
or action() takes up much more horizontal space than shown above.


If any line exceeds about 72 chars it should be split. The above
error should not happen, because there is no indentation to lull
the revisor. I.e. having:

if (condition)
action();

is more likely to be fouled on revision. However if you need:

if (long_winded_excruciating_conditional_expression) {
action();
}

is better written with the braces just to keep the line length
down.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson

Nov 15 '05 #13
Hello,

CBFalconer <cb********@yahoo.com> wrote:
You avoid that problem if you write the braceless versions as:

if (condition) action();

i.e. in one line. You also save vertical space and make your code
more readable.


Obviously, you never had to deal with 3rd party environments like the
Windows DDK, where many 'function calls' are actually macros (which are
not "guarded" via "do { ... } while (0)"), do you? ;)

To add to the confusion, the "what-is-a-macro, what-is-a-function"
properties are subject to change between different version of the DDK,
and even between different compile options of the DDK.

Even if you do not need this environment yourself, you or a collegue of
your might be required to take the code you wright today and use it in
the future with such an environment. I'd like to tell: Have fun! :)

Regards,
Spiro.

--
http://www.trikaliotis.net/
Nov 15 '05 #14
Spiro Trikaliotis wrote on 22/08/05 :
Obviously, you never had to deal with 3rd party environments like the
Windows DDK, where many 'function calls' are actually macros (which are
not "guarded" via "do { ... } while (0)"), do you? ;)


Good point !

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

..sig under repair
Nov 15 '05 #15

Rob wrote:
Functionally, the two following pieces of code are the same:

if(p == NULL) {

break;
}

if(p == NULL)
break;

However, I am wondering whether there are any performance implications
in choosing one over the other. Is there any extra overhead created by
defining a block? Will a compiler optimize the former code to the
latter? Does it even need to?

The reason I ask is, I'd much prefer to go with the former code, simply
because I prefer the appearance.


I would be surprised (nay, astonished) if there were a runtime
performance difference between the two, but I've learned to never say
never when it comes to things like this. I wouldn't worry about it,
though.

My personal bias is to put everything in a compound statement, whether
it actually contains more than one statement or not, just because I've
been bitten too many times in the past by the

if (a)
b; c;

bug. And I prefer my code to have a little vertical breathing space;
it just makes it easier for *me* to read.

Nov 15 '05 #16

In article <mn***********************@YOURBRAnoos.fr>, "Emmanuel Delahaye" <em***@YOURBRAnoos.fr> writes:
Spiro Trikaliotis wrote on 22/08/05 :
Obviously, you never had to deal with 3rd party environments like the
Windows DDK, where many 'function calls' are actually macros (which are
not "guarded" via "do { ... } while (0)"), do you? ;)


Good point !


Unfortunately, it is equivalent to the thesis "You may encounter
problems if you use headers written by idiots", which ought to be
patently obvious; and as such is not particularly informative or
useful.

Clearly, if you are working in an environment where inobvous macros
with nasty syntatic or semantic side effects have been defined,
pretty much any coding practice could be dangerous.

--
Michael Wojcik mi************@microfocus.com

An intense imaginative activity accompanied by a psychological and moral
passivity is bound eventually to result in a curbing of the growth to
maturity and in consequent artistic repetitiveness and stultification.
-- D. S. Savage
Nov 15 '05 #17
Rob wrote:
Functionally, the two following pieces of code are the same:

if(p == NULL) {

break;
}

if(p == NULL)
break;

However, I am wondering whether there are any performance implications
in choosing one over the other. Is there any extra overhead created by
defining a block? Will a compiler optimize the former code to the
latter? Does it even need to?

The reason I ask is, I'd much prefer to go with the former code, simply
because I prefer the appearance.


As others have pointed out, the difference in terms of efficiency is
negligible.

As far as I know, all languages in the Pascal tradition after Pascal do
require explicit termination of control statements (we're talking 1970's
here). What seemed to be a good idea at first -- the distinction between
statements and statement sequences in control statements (e.g. Algol,
Pascal and C) -- turned out not to be, so I see no reason why ANSI/ISO
never decided to make braces mandatory in C.

As I see it, the advantages of explicit control statement termination are:

* Bugs are avoided (multiple statements on one line, the classical
else-if situation, wrong indentation...)

* If you use braces only when necessary, you have to decide if they are
required or not at every control statement you write. If you add one
statement to a single statement you have to add the braces, and if you
remove one of two statements you probably want to remove the braces as
well to achieve consistency throughout the code. This requires some
extra editing. Since all choices in programming are distracting, the
irrelevant ones should be removed.

* With explicit control statement termination the language gets less
complicated and more regular.
Disadvantage:

* The code gets somewhat more cluttered and slightly less readable in
case of short control statements.
August
Nov 15 '05 #18
Eric Sosman <es*****@acm-dot-org.invalid> writes:
Rob wrote:
Functionally, the two following pieces of code are the same:

if(p == NULL) {

break;
}

if(p == NULL)
break;

However, I am wondering whether there are any performance implications
in choosing one over the other. Is there any extra overhead created by
defining a block? Will a compiler optimize the former code to the
latter? Does it even need to?


This wins the Grand Prize for worrying about the
wrong things.

Eric, please consult the Awards Committee. In this case it
seems more appropriate to award the Grand Prize for
wondering whether to worry about the wrong thing. That's
not nearly so prestigious an award.

Nov 15 '05 #19

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

Similar topics

20
by: Doug Holton | last post by:
Is there any metaclass trick or something similar to allow anonymous code blocks? I'd like to be able to let users do something like this fictitious example: b = Button() b.OnClick =: print...
4
by: grs | last post by:
Can a class library have a app.config file. Reason for asking is that the microsoft application blocks all read from myApp.exe.config. How can you use the application blocks if you do not have an...
14
by: J.S. | last post by:
In a Windows Form application, which is the better method to concatenate large blocks of code? 1. Reading the text from text files. 2. Adding the text to the VB file itself? Thanks! J.S. ...
11
by: efialtis | last post by:
Hi to all, I am creating a program which plays a game. The game is played on a NxN square board. In every position of the board we may have a ball or not. Take for example the following 5x5...
3
by: craig | last post by:
I was just wondering if anyone else may have incorporated the original Microsoft Exception Management Application Block (EMAB) or Data Access Application Block (DAAB) into one of their applications...
26
by: brenocon | last post by:
Hi all -- Compared to the Python I know and love, Ruby isn't quite the same. However, it has at least one terrific feature: "blocks". Whereas in Python a "block" is just several lines of...
1
by: Achim Domma | last post by:
Hi, I have an ASCX control which himself contains other ASCX controls. The main control is used in a Asp.Net 1.1 application and works fine. The control is also used in a sharepoint web part...
2
by: flyzone | last post by:
Goodmorning people :) I have just started to learn this language and i have a logical problem. I need to write a program to parse various file of text. Here two sample: --------------- trial...
3
by: GaryDean | last post by:
I have just been through the docs on the Data Access Application blocks and it seems that they complicate things more than make things simple. To me it seems that there is nothing more simple and...
12
by: Michael7 | last post by:
Hi Everyone, I've been trying to get two blocks of text to be aligned, one left, one right, on the same line. What I'm trying to mimic is what I did with tables here: ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.