Time and memory performance of C versus C++

  • Thread starter Generic Usenet Account
  • Start date
I

Ian Collins

Malcolm said:
If you treat arays as "objects" it becomes natural for an access to
cause code to be executed. C++ even allows overloading of the subscript
operator to permit this. You can have bounds checking in non-OO
languages, but it implies special compiler tricks and / or hidden
variables.
Exactly, in OO languages, you can opt to bounds check or not to bounds
check, or even both for debug/non-debug builds. With non-OO languages,
you don't have that choice. So the former gives the programmer the
choice of safety or speed, throwing away cycles is an option, not
something imposed by the language.

It's the programmer's choice to "throw away cycles", not the language's.
 
C

CBFalconer

Ian said:
What does any of the above have to do with OO? The first language
I used that uses automatic array bounds checking was Pascal. C++,
even when used for OO programming, does not perform array bounds
checking.

The better Pascal systems will do very little run-time checking
WHEN THINGS ARE PROPERLY TYPED. That typing sets limits for all
variables, and that knowledge can be used to ensure that range
calculations are only performed when the run-time calculation could
create an error. This has been shown to reduce run-time check time
by about 80%.

This is not available for C, because there is no sub-range typing
available.
 
I

Ian Collins

CBFalconer said:
The better Pascal systems will do very little run-time checking
WHEN THINGS ARE PROPERLY TYPED. That typing sets limits for all
variables, and that knowledge can be used to ensure that range
calculations are only performed when the run-time calculation could
create an error. This has been shown to reduce run-time check time
by about 80%.
I wonder how often this (correct typing) is used? One can do the same
in C++, but I seldom see the technique used.
 
R

RoS

In data Fri, 09 Nov 2007 12:48:59 -0800, (e-mail address removed) scrisse:
These days with 3ghz computers with more than 1 gbyte RAM what is so
important about elegant, efficient code?

because short code is beautiful
 
F

Flash Gordon

Ian Collins wrote, On 11/11/07 08:18:
I wonder how often this (correct typing) is used? One can do the same
in C++, but I seldom see the technique used.

In the Pascal code I have been involved in it is used a lot. The reason
for using it was not efficiency or run-time checking, it was
documentation. If someone sees a variable of field defined as 0..8 then
without looking further they know what the valid range is, if it is
defined as an integer they don't.
 
S

Sherman Pendley

RoS said:
In data Fri, 09 Nov 2007 12:48:59 -0800, (e-mail address removed) scrisse:


because short code is beautiful

Elegant code does tend to be short, but brevity shouldn't be a goal in
and of itself - that way lies obfuscation and maintenance nightmares.

True code beauty is a balance of brevity and clarity.

sherm--
 
J

James Kanze

[...]
The better Pascal systems will do very little run-time checking
WHEN THINGS ARE PROPERLY TYPED. That typing sets limits for all
variables, and that knowledge can be used to ensure that range
calculations are only performed when the run-time calculation could
create an error. This has been shown to reduce run-time check time
by about 80%.
This is not available for C, because there is no sub-range typing
available.

I believe that such compilers are, or at least have been
available. They aren't used in production because of the
enormous overhead you have in order to do it in C: every pointer
must include not only the address, but also all relevant bounds
information. And it's hard to optimize, because typically, the
bounds have been set in another compilation unit, and aren't
known to the compiler.
 
J

James Kanze

On 2007-11-10 04:31:02 -0500, "(e-mail address removed)"
<[email protected]> said:
If an object needs to manage resources, then it needs to manage
resources. In C++ that's what the constructor and destructor do. In C
that's done explicitly by calling init and cleanup functions. Either
way, the speed of correct code is in large part determined by what has
to be done, not by the language that's being used to do it.

When I first shifted to C++, I found that my programs became a
lot bigger (although not necessarily slower). They also did a
lot more---features that represented a lot of work in C were
often very simple to implement in C++, and so got added, where
they'd never gotten added to the C program.
 
C

Chris Dollin

These days with 3ghz computers with more than 1 gbyte RAM what is so
important about elegant, efficient code?

/My/ code can be as inefficient as it likes -- if I don't like
it, I change it, and I'm the one paying the cost & receiving
the benefit of the "inefficiency".

/Your/ code, running on /my/ machine, had better be as efficient
as you & your compiler can make it, because those are /my cycles/
you're burning, and I have better things to do with them than
make life easier for /you/.

Now boil with lots of symmetry.
 
P

Pete Becker

But object-oriented languages often throw away cycles for the
convenience of the programmer. For instance frequently you know from
the program logic that an array bounds error cannot happen. However
many languages will check each access anyway. Since the vast majority
of code is not in the inner loop, and thus not time critical, that's
often a reasonable trade off. But not always.

Regardless of what "many languages will check", C++ does not check
array access. Array access does not affect the speed or memory usage of
C++ versus C.
 
P

Pete Becker

Regardless of what "many languages will check", C++ does not check
array access. Array access does not affect the speed or memory usage of
C++ versus C.

That is, C++ does not require checking array access. Nor does C.
 
C

CBFalconer

Ian said:
CBFalconer wrote:
.... snip ...


I wonder how often this (correct typing) is used? One can do the
same in C++, but I seldom see the technique used.

Probably not too often. How many C users actually use the full
abilities of the language?
 
T

Tor Rustad

Malcolm said:
Little 8-bit embedded processors outsell others by a wide margin.


Pray tell, first you try to educate us on financial calculations, now
the time has come to embedded processors?! <g>


Has Malcolm McLean ever worked professionally with (8-bit) embedded
processors?
 
C

Charles Richmond

CBFalconer said:
Probably not too often. How many C users actually use the full
abilities of the language?

"Real Programmers know every nuance of every instruction
and use them *all* in every Real Program."
-- "Real Programmers Don't Write Specs" ;-)


http://ifaq.wap.org/computers/realprogrammers.html


Thinking back, the only ability of C that I have *not*
used is the "onexit()" callback facility. I am gratified
to say that once I actually found a way to use "setjmp/longjmp"
profitably. (Well, some of the new features introduced
in C99 I have *not* used *yet*.)
 
I

Ian Collins

Flash said:
Ian Collins wrote, On 11/11/07 08:18:

In the Pascal code I have been involved in it is used a lot. The reason
for using it was not efficiency or run-time checking, it was
documentation. If someone sees a variable of field defined as 0..8 then
without looking further they know what the valid range is, if it is
defined as an integer they don't.

I can see that, but even though the language gives the illusion of
access checking free indexing, the underlying mechanics of a sub-range
type must perform some form of checking on assignment to or modification
of a sub range type variable. In Pascal, this happens under the hood,
while in C++, it happens out in the open.

As the old saying goes, there's no such thing as a free lunch.
 
R

Richard

Charles Richmond said:
"Real Programmers know every nuance of every instruction
and use them *all* in every Real Program."
-- "Real Programmers Don't Write Specs" ;-)


http://ifaq.wap.org/computers/realprogrammers.html

Heh. That page is a real throwback to old Dinosaurs who would have a
hard time getting a job in the modern SW world. Think the two nerds in
War Games. There's only one which rings true ...

,----
| Real Programmers never work 9 to 5. If any real programmers are around
| at 9 AM, it's because they were up all night.
`----

:-;
 
M

Malcolm McLean

Tor Rustad said:
Pray tell, first you try to educate us on financial calculations, now
the time has come to embedded processors?! <g>


Has Malcolm McLean ever worked professionally with (8-bit) embedded
processors?
Malcom is not a professional. However he knows almost everything about
almost everything. Alas, not how to land the contract for a little embedded
parking meter that he prepared, but it was probably just as well - they
weren't nice people and we were probably better off in the long run without
it.

So sort of. I used to do games programming which is non-hosted but not
exactly embedded. Most embedded processors do very simple jobs that are well
within their design specifications.
 
F

Flash Gordon

Malcolm McLean wrote, On 11/11/07 23:29:
Malcom is not a professional. However he knows almost everything about
almost everything.

That sounds like satire, only the rest of your post suggests you
actually believe it.
Alas, not how to land the contract for a little
embedded parking meter that he prepared, but it was probably just as
well - they weren't nice people and we were probably better off in the
long run without it.

Ah, so your experience of 8 bit processors is failing to win a job. That
hardly qualifies you.
So sort of. I used to do games programming which is non-hosted but not
exactly embedded.

Well, the best games tend to push the HW fairly hard so I don't think
that qualifies you either.
Most embedded processors do very simple jobs that are
well within their design specifications.

So where are your statistics to back that up? I have actually done
embedded programming on 8 bit processors (although not in C) and most of
the ones I or my colleagues worked on were pretty busy, so busy that we
needed several of them in each device.
 
M

Malcolm McLean

Flash Gordon said:
So where are your statistics to back that up? I have actually done
embedded programming on 8 bit processors (although not in C) and most of >
the ones I or my colleagues worked on were pretty busy, so busy that we
needed several of them in each device.
Oddly enough the car park meter was also a bit underpowered, one reason we
didn't get the contract.

However, looking about me, I've got two calculators on my desk. They've got
about a tenth of a second, or just noticeable delay, to calcuate and
expression and display it.

I've got an alarm clock. That updates a display every second. There's lots
of stuff associated with the computer, and it's hard to be sure exactly what
it is doing. The mouse is obviously not stressed. The wireless link I'm not
so sure about - I'd guess that that's actually doing quite heavy work. The
pockidrive -
dunno how they work - obviously the throughput of bytes has to be at full
capacity. I forgot my watch. Again, it basically updates a display every
second, though it will do more than that as in sychs in with the quartz
crystal.

So five unstressed processors, one probably working hard, and one I'm not
sure about.
 
T

Tor Rustad

Malcolm said:
Malcom is not a professional. However he knows almost everything about
almost everything.

"To myself I am only a child playing on the beach, while vast oceans of
truth lie undiscovered before me."
-Isaac Newton
So sort of. I used to do games programming which is non-hosted but not
exactly embedded.

In your own view, this is a field we should expect clueful posts
from Malcolm McLean then.
Most embedded processors do very simple jobs that are
well within their design specifications.

What does *most* mean? Is *most* less than *typically*?

To be specific, let say there is 1 million people, how many "few
counter-examples" do you expect?

1 million? 2 million? 3 million?

or far less?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top