Is C/C++ worth it?
lemire.me
lemire.me
Pretty much all unixes are written in C, and using the language of the OS means you can talk to it the same way the developers do, rather than through an FFI; even the best FFIs suffer from the fact that there will be some impedance mismatch between languages.
C++ is more esoteric, significantly harder to learn, and not as portable, but it's still better than most and runs wickedly fast if implemented according to best practices.
This is not for "average devs", not in a world where there are dozens of first-class languages better suited to general problem solving, but some domains it is still imperative to go either C, C++ or some combination.
Do average devs write kernels? Device drivers? 3D game engines? Fluid dynamics simulations? Image transformation algorithms? Network routing stacks? No, but people do, and it's a good thing they have C or C++ to solve their problems.
>C++ is more esoteric, significantly harder to learn, and not as portable....
Does that make C++ the Sanskrit of computer programming, then? :)
On a more serious note, I just found myself upvoting every single post that acknowledged the difference between C and C++. Why? Because they're very different (you can't even say C++ is a superset of C anymore), and using C++ competently requires knowing the differences. It always makes me think these posts that compare the writer's favorite language to this mythical "C/C++" language are written by people who don't know either C or C++ very well.
Edit for clarity: I note that the actual title of the post is "Is C++ worth it?" so the author doesn't seem to be making this mistake, except in one instance where he refers to "C/C++ programmers". In that case, he might actually be referring to "C programmers or C++ programmers," so I can forgive him the abuse of language.
For instance, in the scientific computation that I do, most code will be used in finished form on the order of 10s of datasets at most. Occasionally, we step out from R or Matlab or Python and use C/C++ if a) we know we'll use a function thousands of times in multiple contexts, b) we're writing code to be used by other people, or c) we're working in a time/resource constrained environment, like a supercomputer or shared cluster with many users, where we pay for time.
I imagine the "modest" 2x improvement would not matter at all if: a) your users are going to batch process a bunch of files just a couple times, but want the most up-to-date features possible, so you optimize for production speed, not performance, or b) you are writing the code for yourself, so letting it run in the background or on a cluster for 2 days instead of 1 doesn't make a huge difference.
On the other hand, if I were writing a photo-editing program where users call the same feature over and over and over to get the photo looking just right, I would definitely prioritize performance, and a 2x improvement is a very noticeable speedup over a competitor's. A lot of products I use every day are actually like this.
When it comes to performance, you either require it or you don't. There is no place in between. When performance is a requirement, C++ can be a very good option. It's a great balance between performance and high level feature set.
With experience, we (programmers) become more pragmatic and get better at picking the right tool for the job.
C is a vibrant, separate language from C++ that has wonderful applications separate from it.
C++ doesn't need to be called C as well, that's what the first character of it's name means. It's also has nothing like a ABI, so is far from C as can be in that regard.
I think it's a shame that the client side of the web is basically limited to JS, because seriously, that's the only reason so many people started coding in it in the first place. I believe that if we could, many devs would prefer other languages.
The same thing happened when (non-GC) HLLs started to overtake assembly language. And the discussions will continue to be equally context-free. And I'm sure that just like the last time around, the most vociferous participants in the debate will be people who don't have to work with terribly high performance demands anyway. On account of the people who do usually being way too overexposed to the mountains of nuance (and aware that the only reliable way to find the best option in any particular case is to do some measuring for that particular case) to feel comfortable making the kinds of categorical statements that make for good blogging.
1. Using raw pointers as opposed to vectors gives 2x the performance on my machine.
2. This is a microbenchmark with extremely simple array indexing pattern. JVMs can optimize away the array bounds checks penalty in this case but those become hard to do once your access patterns become anything other than "i" or "i-1". For example, even more general linear subscripts are hard to correctly optimize for array bounds checks, let alone anything non-linear. How do I know? Compiler researcher here :)
However, still impressive to see JVMs advance.
This isn't to say that major strides haven't been made in the performance of high level VMs, interpreters and compilers. But for some classes of applications, it's arguably not worth trying to avoid C or C++. Audio software is bound by very stringent realtime requirements (you absolutely cannot miss a sample vector -- dropouts are bad bad bad), such that GC becomes problematic in many cases. If you're writing a raster effects chain, a sensible thing to do in C would be to pass a pointer to your frame buffer through a chain of function pointers operating on the data in place and avoiding copying at all costs.
Point being, the more constraints like these that pile up, the harder time a compiler will have guessing what kind of performance characteristics you're looking for, and though I'm sure there are ways to get around these problems in higher level environments, you may end up finding that the time you spend tuning things to get it just right would have been better spent getting your hands dirty with some C. But as with everything, YMMV.
If you actually wanted this code to go fast you'd use SSE, of course - and make a big mess of prefetches and intrinsics and so on. On a good day you might get the same result out of icc.
That being said, the guy is right - most of the time. A range of performance differences extending up to 2-5x between (say) Java and C++ (as shown on, say, the Programming Languages Shootout) shouldn't be decisive to most programmers who aren't writing code that's CPU bound.
Of course, while this argument can be made in favor of Java over C++ (not my personal taste, but still), it could be equally made for Python or Ruby over either. :-) This is especially true where the 'low performance' scripting language is just driving highly optimized math routines done over large chunks of data.
.. Surprise! It's all in C++. It uses libSDL for input and audio, and openGL for graphics. With that framework I also released an app for iOS. Everything under the sun has a SDL port.
The downside was I had to write my own UI widget framework and cannot do any native features. The upside is "write once, port to everywhere".
If you have specific reasons for starting with C or C++, great. Go for it. Have fun.
One the other hand, if you are picking either because, for example, you have a vague sense you want performance and/or low-level control... It's probably worth looking at your alternatives and thinking about your tradeoffs.
1) why use a class for the timer?
2) It's not clear if the order of operations ensures that the timer is stopped before the first print operation happens. Thus your C++ may be including some part of the cout print operation
get time
do it
get time again
print timing info
The C++ code had a line of the form cout << .... << some_method_which_stops_time_and_returns << ...
So unless G++ is doing some sort of whole program analysis, it would have to do the cout << ... first and then evaluate the method and then print out the return valueFwiw, I just ran it after fixing the time call, and it doesn't seems to significantly change the result on my (low spec) machine.
Even if they're doing whole program analysis, the fact that oeprator<<() and WallClockTimer.split() are side-effectful and I believe that means that it would be invalid for the compiler to find the current time before the first section of the print statement is finished.
cout << "asdf:" << b()
is equivalent to (cout << "asdf:") << b()
, which is equivalent to cout.operator<<("asdf:").operator<<(b())
`b()` can certainly be evaluated before the first `operator<<` is evaluated. Hell, `b()` can be evaluated before "asdf;" is evaluated. Consider the following code: int f() {
static int a=0;
return a++;
}
cout << f() << " " << f() << endl;
This prints "0 1" in Clang (at all levels of optimisation), and it prints out "1 0" in gcc (again, at all levels of optimisation). If they're differing on something as simple as this it's clearly allowable by the standard. cout << "smarter sum " << N/(1000.0*time.split()) << endl;
Is semantically equivalent to: operator<<(endl,
operator<<(N/1000.0*time.split(),
operator<<(cout, "smarter sum ")
)
);
The code with the timer in it can be called after the first call to operator<<; all parameters to functions have to be evaluated before the function is called, but there's no order the parameters themselves have to be evaluated in.To a large extent, it depends on the target platform. If you developing an application to work across the widest possible range of desktop and mobile platforms, C++ is your best bet.
Using C++, you can have a single code base that can be compiled for Windows, OSX, Linux, iOS, Android and WinMo. There is no other language that gives you that.
The UI code is not portable in any case, but that would be a common problem regardless of language.
Aren't we missing something essential though? C and C++ give you the possibility to make self-contained, lean applications - using the right frameworks you can also target as many platforms as you want. Java, python etc. have in common that they all require a runtime environment, including all the compatibility and update problems.
The big question is, is that information ever worth it? Does the cost of examining the running system completely overshadow any possible benefit?
So far, no. it's not worth it. It'll be really interesting to see what happens with massive parallelism though.
See http://en.wikipedia.org/wiki/Profile-guided_optimization
IIRC gcc already links in a runtime system for recovering runtime type information about dynamic cast. It's not hard to imagine C++x20 (or whatever) including some kind of runtime optimizer.
It uses run time information to modify its own code to perform optimally. It was incredibly fast, but the difficulty in using SMC far outweighs the benefits in most applications.
size_t size = data.size(); for (size_t i = 1; i != size; ++i) { data[i] += data[i - 1] ; }
It is down for me, too. You can read the article via Google's search results preview but it isn't linkable and there is no Google Cache link.
Searching for cache:URL often works even when the search results don't link to the cached version for whatever reason.
Maybe call it C+ since that's the grade I'd give that kind of half-baked code.
Do a similar example where you want to replace the values in situ and find that Java is making a copy of the entire array every time. At that point you are stuck, there is no way out. C may be difficult but it lets you do anything you want.
note: yes Java may have a flag that says do int array in place, in this case - but you know what I mean.