Searching \ for 'methods for programming' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/microchip/devprogs.htm?key=programming
Search entire site for: 'methods for programming'.

Truncated match.
PICList Thread
'methods for programming'
1999\03\08@164417 by John Payson

flavicon
face
|                                As long as you
|understand C++ it's possible to create software as tight or even tighter
|than C (this is obviously compiler dependent...). However, it's mighty
|tempting to use virtual functions in some applications and then C++ will
|appear to use more RAM/ROM then C. I say appear because the functionality
|achieved with the virtual function takes as much overhead if the same
|operation is implemented the same way in C. However, most C programmers
|don't write in this abstract manner (nor do they need to for relatively
|small microcontroller based projects) and consequently always 'know' where
|their 'virtual' functions are pointing. The point is, that some of the
|elegance C++ offers can use more ROM and RAM if the programmer is not
|aware of the implementation details.

One of the advantages that C has vs some higher-level languages is
that it is fairly easy to predict what code will be generated by any
particular construct; if I see something like:

               woozle++;

I can pretty welll guess that the compiler's going to code either a
straight increment instruction (if suitable), a long-increment instr-
uction sequence (e.g. on the PIC:

               incfsz  woozle,f
                goto           $+6
               incfsz  woozle+1,f
                goto           $+4
               incfsz  woozle+2,f
                goto           $+2
               incf    woozle+3,f

or
               movlw           woozle
               call            inc_long

or something similar), or else a floating-point load/add/store sequence.
I can also expect that "woozle++" and "++woozle" will produce the same
code on a decent compiler.  In C++, however, none of those assumptions
hold.  Whereas I could put a reasonable upper bound on the time required
for "woozle++" in C, it may well be in C++ that woozle is a complex heap-
allocated structure and that incrementing it could require doing a gar-
bage collection on a 16MB heap.

One major problem that can stem from this disconnection between source
code and actual execution is that it's no longer at all clear what the
tradeoffs are between:

               foo += boz;
        foo += boo;
               bar += boz;
               bar += boo;
and
               foo += (boz+boo);
               bar += (boz+boo);
and
               zoo = (boz+boo);
               foo += zoo;
               bar += zoo;
and
               zoo = boz;
               zoo += boo;
               foo += zoo;
               bar += zoo;

nor any of dozens of similar similar groups of possiblities.  While in C,
the differences between the above are probably small, in C++ it would
not be uncommon for there to be an order of magnitude speed differences
among the above constructs (with any of the three being the fastest!).
Not only can the coder not tell which of the above constructs to use, but
even a compiler smart enough to recognize their equivalence with integers
would be unable to assume their equivalence with more complex types.

Perhaps someone can tell me what--other than running lots of initiali-
zation routines for data structures--my computer is doing when it takes
many seconds to fire up MS-Word, MSIE, or other software?  The software
is bloated, to be sure, but even reading the whole thing off the hard
drive wouldn't take THAT long...

1999\03\08@181107 by spare

flavicon
face
> or something similar), or else a floating-point load/add/store sequence.
> I can also expect that "woozle++" and "++woozle" will produce the same
> code on a decent compiler.  In C++, however, none of those assumptions
> hold.  Whereas I could put a reasonable upper bound on the time required
> for "woozle++" in C, it may well be in C++ that woozle is a complex heap-
> allocated structure and that incrementing it could require doing a gar-
> bage collection on a 16MB heap.

Right... but only if woozle refers to a class, for which the ++
operator has been overloaded - which is something the user would have
done deliberately anyway. The only difference is that, for convenience,
it's written woozle++, and not WoozleIncrement(), as it would be in C.

The only time you won't know exactly what woozle++ is doing is if
you're using someone else's classes, and you don't have the source -
a problem that you would still run into with function calls in C.

C++ is really just a convenient notation for providing objects in C.
In terms of the code-generation side of things, the compilers are
pretty much identical!

Cheers,

Ben

1999\03\08@190938 by Gerhard Fiedler

picon face
At 15:45 03/08/99 -0600, John Payson wrote:
>I can also expect that "woozle++" and "++woozle" will produce the same
>code on a decent compiler.

i hope not on mine ;-)

>it may well be in C++ that woozle is a complex heap-
>allocated structure and that incrementing it could require doing a gar-
>bage collection on a 16MB heap.

i guess that's where comes in what they mean when they say "as long as you
know what you're doing."

>One major problem that can stem from this disconnection between source
>code and actual execution is that it's no longer at all clear what the
>tradeoffs are between:
[...]
>nor any of dozens of similar similar groups of possiblities.  While in C,
>the differences between the above are probably small, in C++ it would
>not be uncommon for there to be an order of magnitude speed differences
>among the above constructs (with any of the three being the fastest!).

if the variables are scalar types, i guess c compilers are not much more
predictable in their optimizations and use of temporary variables than c++
compilers are. why should they be? and if they are complex types, it's back
to rule #1: "as long as you know what you're doing" :)


>Perhaps someone can tell me what--other than running lots of initiali-
>zation routines for data structures--my computer is doing when it takes
>many seconds to fire up MS-Word, MSIE, or other software?

i suspect it's somehow collecting money for microsoft, or making your
computer addicted to ms software, or whatever... there must be =something=
behind it.

ge

1999\03\08@194056 by Gerhard Fiedler

picon face
At 15:06 03/08/99 -0800, Ben Stragnell wrote:
>C++ is really just a convenient notation for providing objects in C.
>In terms of the code-generation side of things, the compilers are
>pretty much identical!

yes, i think so too -- with the exception of advanced features like
exceptions, virtual functions, multiple inheritance and the like. but you
wouldn't use them (or even have them) anyway.

ge

More... (looser matching)
- Last day of these posts
- In 1999 , 2000 only
- Today
- New search...