Re: simple increment operator question.

From: The Real OS/2 Guy (os2guy_at_pc-rosenau.de)
Date: 12/22/03


Date: Mon, 22 Dec 2003 17:31:28 +0000 (UTC)

On Mon, 22 Dec 2003 06:19:22 UTC, Jeff Schwab <jeffplus@comcast.net>
wrote:

> Usually, it's a good idea to avoid the post-increment operator, in favor
> of something like this:

That's a wrong advise. Learn C right. If you don't know how an
operator works you can't use C anyway.
 
> m_pVertices[ i_ArrayIndex ] = a;
> ++i_ArrayIndex;

Following your advise it must be:

i_ArrayIndex = iArrayIndex + 1 * 1;
 
> Or, initialize i to -1 instead of 0, and do this:
>
> m_pVertices[ ++iArrayIndex ] = a;

That is wrong! Foolowing the guides you would declare
size_t iArrayIndex;

as it should be an index ending up at lest with the size of the array.
For that the type size_t exists. Misleading by negative index is
confusing. An index in C starts always with 0, not a negative number.
Using postincrement when ever possible results in
- better readability
- often in more optimal code.

Using preindex can even be useful - but not when you have to use
tricks as you do to get it working.

You have to learn how the operators in works. You gets confising
yourself and other by not knowing it exactly.

> The reason is that some overloaded post-increment operators have to make
> copies of the objects being incremented.

In contrast some overloaded post-increment gives better optimised
code. Don't stink against the compiler! Use post or preincrement as
you have to make thinks clean - not to destroy the abilities of your
compiler to make optimasions right. A primitive compiler without good
optimisation ability will produce better code for
x[i++] = y[z++] as when you writes
x[i] = y[z], i++, z++;
A compiler with an better optimiser will even equal code for both. But
you yob is NOT to optimise on microlevel as you tries - but to write
good code, good readable for humans.
 
> If you're not sure of the meaning of a line involving only built-in
> types, you might find it useful to look at the assembly code. If you're
> on a Unix-like system, try the -S flag to your compiler, and look for a
> file with a .s extension.

Wrong advise. I've never seen assember code for more than 15 years -
because there was absolutely no need for. Hand optimising for one
comiler makes the next one (even the next subversion of the same
generating more bad code than using only C.

There is no real cause for an C programmer to learn the about 3.498
assember OP codes with all its modifications a modern processor
understunds - except you have to write an code generator that has to
generate mshine code based on the C sources.

Learn C right to write C programs, learn assember of a particular
mashine right to write assember on that, learn another programming
language right when you have to write code in that language. But ever
you have to learn the language right and completely to write correct
and error free code.

When you means that the code the compiler generates is too slow don't
botch on single statements - user another algorithm to tune the work
up.

-- 
Tschau/Bye
Herbert
Visit http://www.ecomstation.de the home of german eComStation


Relevant Pages

  • Re: Performance Q: java hotspot vs. native code
    ... certain types of automatic optimisation possible which are impossible to apply ... Java" then yes you are right, but then you are discussing which language has come further along in their development of optimised code. ... are such that the compiler cannot perform automatic loop unrolling, ...
    (comp.lang.java.programmer)
  • Re: Resources for a Compilers course.....
    ... I am a compiler virgin but from all that I have heard about that book, ... tree, optimizing, or code generation, i. e. ... converting the AST into the target language, ... Synthesis (intermediate code generation, optimisation and code ...
    (comp.lang.scheme)
  • Re: Java or C++?
    ... Fortran vs C on some array benchmarks is probably a good bet. ... compilers often do a lot of platform-specific cache-based optimisation that ... compiler will generate efficient code for you. ... That language isn't C/C++. ...
    (comp.programming)
  • Re: Flushes
    ... Isn't that more to do with the implementation of the compiler, ... the problem is actually in the language. ... The first rule of optimisation: ... The second rule of optimisation: "I told you, ...
    (rec.gambling.poker)
  • Re: RAD vs. performance
    ... abstractions that will be there, whereas my compiler can optimize them ... Usually this doesn't matter, though. ... I'd say the same thing about your premature optimisation ...
    (comp.lang.misc)

Loading