Live data from Hacker News

Comparing C and C++ usage and performance with a real world project

nibblestew.blogspot.com

31–40 of 140 posts

Re: Comparing C and C++ usage and performance with a real world project

#31

If you ignore the protection mechanisms and the class heirarchy built into C++, then a C++ class is like a C struct that can contain function pointers. For many programs this is all that's needed, and the amount of overhead involved in using such an approach to creating objects is obviously lower. So there's no question C will always be faster. It's only when you need protection and class heirarchies that C++ benefit…

> It's only when you need protection and class heirarchies that C++ benefits you. Well, need is a strong word. You might not need better correctness while still coming out way ahead by using C++.

Protection implies a much more complex structure to represent an object and class heirarchies and inheritance imply the need for a runtime. Both these overheads come at a cost. It's the nature of the program you are wriitng that determines whether you will come out ahead. If you were writing a codec, say, you would not use C++.

Re: Comparing C and C++ usage and performance with a real world project

#32

I'm going to be the first to point out the one major flaw in this comparison: "plain C using GLib" is not comparable to "C++ standard library only" --- what should be compared is "C++ standard library only" and "C standard library only". Reimplementing pkg-config in pure C without GLib would be necessary for that. As for the "memory leaks" --- I haven't looked at the source, but something whose runtime is very short-…

Never freeing is sloppy, it means resource handling was likely not thought through. I would be concerned about non-memory resource handling in particular.

If one must use this "trick", it's better to think things through, add the appropriate release calls and then somehow replace the release function with a no-op.

Anyway, you're welcome to take a C++ project and translate it to a fast C project with fewer dependencies and lower memory consumption. Then we can discuss facts instead of your personal opinions.

Re: Comparing C and C++ usage and performance with a real world project

#35
FWIW, Donald Knuth was a proponent of using C over C++ at the time it first came out. He equated C++ with the use of frameworks in writing programs which he thought were a bad idea for the profession as it would dumb it down. C++ does make code reuse a lot easier.

Re: Comparing C and C++ usage and performance with a real world project

#36

The minor discussion on the C version's memory leaks reminded me of a neat trick. If you're developing a short lived application, like pkg-config, you can opt to never deallocate. i.e. leak everything. In lightweight, short lived applications there's usually not a lot of incentive to deallocate; your application will never use much memory anyway and the deallocations waste time. You can think of it like treating C as…

I remember one of those sysadmin stories, where a multi-terabyte `cp` command had seemingly completed all of its work, but was sticking around for days; slowly and pointlessly free()ing the 17GB hash table of hardlinks that it had built up; when it could have just exited and let the OS reclaim the memory.

https://lists.gnu.org/archive/html/coreutils/2014-08/msg0001...

Re: Comparing C and C++ usage and performance with a real world project

#37

I knew C++ compilation was slow but 30x slower? I'm sure the compile-time memory usage will also show a similar trend. It would be interesting if somebody could explain the reason for this disparity.

It's a well known fact. Just by including some headers from the standard library the compiler has to go over huge chunks of library template code, and it usually needs quite a few complicated phases to slowly morph those templates into executable code.

Re: Comparing C and C++ usage and performance with a real world project

#38

I knew C++ compilation was slow but 30x slower? I'm sure the compile-time memory usage will also show a similar trend. It would be interesting if somebody could explain the reason for this disparity.

Not exactly a fair comparison when the C program is using 1.5MB of pre-compiled dependencies while the C++ program reimplements all of the functionality from the dependencies in the code being compiled.

Re: Comparing C and C++ usage and performance with a real world project

#39
post #9

according to dan saks, who apparently to some people is famous c++ is faster than c (well in the test setup he describes below) https://accu.org/content/conf2015/DanSaks-Embedded%20Program... Language Design Implementation Relative Performance either any inline 1 (fastest) C++ polystate non-inline 1.56 x fastest C++ bundled non-inline 1.65 x fastest C polystate non-inline 1.70 x fastest C bundled non-inline 1.79 x fa…

> "if you're using C++ as a better C you're doing it wrong" As far as correctness and safety goes, this is still true. It's difficult to scale systems-level programming to large teams. C++ gives the opportunity for more explicit semantics and more aggressive compile-time checks. C can scale well and can be used safely, but you need to do a lot more through convention (always call xyz_Create and xyz_Destroy in pairs!)…

> D, Rust, OCaml, and a few other projects are interesting in this space since they provide some of the same benefits as C++ with respect to correctness and safety.

Can you name a correctness and safety benefit that C++ has that these programming languages do not?

Re: Comparing C and C++ usage and performance with a real world project

#40
post #26
post #7

Earlier quoted context omitted.

Reminds me of this anecdote I came across on the internets one time: https://groups.google.com/forum/message/raw?msg=comp.lang.ad...

I have heard of calculating a "memory budget" and pre-allocating that, but calculating a "leak budget" and doubling that doesn't seem like hygienic programming.

Doesn't have to be hygienic, it's enough that fixing it doesn't justify the time/money costs for the programmer.
Post reply on HN