Welcome Guest! To access all forums & features, please register an account or sign-in. → Why register?




Photo - - - - -

The mythical "twice as efficient" application

I watched several talks from Build 2012 this week – fascinating event. I was pleasantly surprised to see the people behind MonoGame and Unity there, as well as Brandon Bray’s talk on the future of .NET. For all the fear-mongering there has been around Microsoft abandoning .NET in favor of Javascript, C++, or both, the future is in fact looking brighter than ever for .NET. Yes, Hejlsberg is still working on C#! (phew!)

There was much talk about C++ as well, as Microsoft is investing heavily in supporting C++ and improving its compilers and tools for that language. Herb Sutter gave a very interesting talk about the future of C++, devoid of any of the dubious statements about .NET he made last year.
But then, there was this interview (I can't find it anymore) with the guy who did all the interviews and whose name I can’t remember at the moment, and Herb Sutter started to talk about .NET. A lot. Here are some excerpts (not nearly everything) of what he said about C++ vs .NET (the quotes are long, but at least you can’t say I’m quoting out of context):

Quote

“Windows Phone, for example, now supports C++, which means C++ is supported on every major mobile OS, every major desktop operating system, every major server operating system, it is THE portable language, always has been. Along with C, but it gives you the abstraction, and still the efficiency, which is its big differentiator from C.”

“(…) One size doesn’t fit all. If you have your toolbox, you ask the question “why do you have a screwdriver and a saw, and not just a hammer? You should just have hammers!” That would be silly. The same thing is true of programming languages. They’re each designed for specific kinds of usages. Use the right tool for the right part of your job – very often that’s C++. “

“What’s being pointed out is, the pendulum has swung too far, we’re now getting more balance. We don’t want to get unbalanced the other way, and now say “oh you don’t really need all that managed stuff”. Of course you do, there’s great benefits to that, you should use that tool. But the balance is to, especially where you care about power efficiency, getting the most performance per watt, getting those performance per transistor on mobile devices, and having a twice as efficient app, run twice as long on battery. Have a data center where there’s a twice as efficient app, runs with half the cost of your data center. And remember, the server cost in power is 88% percent of the cost of running a data center. You run an app that’s twice as efficient, it is directly almost twice as cost-effective. Costs half as much. That is really important.”

“And there is still a place where you want to optimize for programmer productivity. [If] programmer time is your biggest cost, like you’re developing an internal IT application, or for some other reason programmer time’s your big cost, you should optimize for that. That’s what many languages are good for. If reach is your biggest thing, that’s where a web-based solution is going to play well. If you want local apps, (…) and you want efficiency, then consider an efficient language. Because C++ has always been about efficient abstractions. To get classes and templates, generics, all that kind of stuff, but having it compiled away to almost nothing. You don’t pay for what you don’t use. So, it’s one of your tools. “

“(…) more and more, especially in mobile and in data centers, yes C++ is being used more and more. Because you want your twice as efficient application to consume twice as less resources and run twice as long on the same battery. People love that. And that’s why you’re seeing native code. And you can write the rest of your application in some other language, often the framework you use will be implemented in native code, or use native code for at least the compute-intensive parts. But C++ is getting so easy to use, modern C++, which is not like it was a few years ago, is getting so easy to use that often you can just write the whole thing in C++ and you can whatever tool you want to… the right tool for the job.”

My main problem with Sutter’s statements, and the reason I decided to write this, is this “twice as efficient application” he keeps referring to. Just what is it? Is it the average C++ application compared to the average C# application? Is it some utopian scenario, given access to the very best developers and infinite development time? Is it implied that by simply choosing C++ as your implementation language rather than C#, your application will be twice as efficient? It’s a vague, caricatural and misleading picture, yet he keeps repeating this as if it bore some profound truth.

He then goes on to say “if you want efficiency, consider an efficient language” – that is, in context, consider C++ instead of C#. So what does that make C#, an inefficient language? I guess if C++ gives you “twice as efficient” apps, then sure, C# must be pretty darn inefficient. Maybe that's not what he meant, but the way he put it makes it hard to think otherwise.

Except that’s wrong. If anything, given the same development time, C++ gives you less efficient apps, because C++ makes it entirely up to you to make things efficient (and makes it very difficult at that), while .NET provides you with a bunch of services and libraries that are already well-optimized. Chances are .NET’s GC is way faster than whatever manual technique you’re using to allocate and free memory in C++ - unless you really know what you’re doing and you’re optimizing very specifically for the application you’re building. That’s why Frostbite 2 is coded in C++ - and that’s also why you don’t need C++ for your typical Windows Store or even Desktop application.

C# and C++ are in fact very similar at a low level. They’re both imperative, C-based languages that compile to very efficient native code. They support much of the same structures which do very much the same thing. Here’s a rather striking example: this is code that compiles as both C# and C++, and its corresponding assembly for both languages at their highest respective levels of optimization. Can you tell which is which?
for (int i = 0; i < size; ++i) {
  if (i % modulus == 0) {
   arr[i] = i * i;
  }
}
// for (int i = 0; i < size; ++i) {
00F91319  xor		 ecx,ecx
00F9131B  test		edi,edi
00F9131D  jle		 0F9133Fh
//  if (i % modulus == 0) {
00F9131F  mov		 esi,dword ptr [esp+10h]
00F91323  mov		 eax,ecx
00F91325  cdq
00F91326  idiv		eax,dword ptr [esp+1Ch]
00F9132A  test		edx,edx
00F9132C  jne		 0F91336h
// arr[i] = i * i;
00F9132E  mov		 eax,ecx
00F91330  imul		eax,ecx
00F91333  mov		 dword ptr [esi+ecx*4],eax
// for (int i = 0; i < size; ++i) {
00F91336  inc		 ecx
00F91337  cmp		 ecx,edi
00F91339  jl		  0F91323h 
// for (int i = 0; i < size; ++i) {
0000000d  xor		 esi,esi
0000000f  test		ebx,ebx
00000011  jle		 0000002A
// if (i % modulus == 0) {
00000013  mov		 eax,esi
00000015  cdq
00000016  idiv		eax,ecx
00000018  test		edx,edx
0000001a  jne		 00000025
// arr[i] = i * i;
0000001c  mov		 eax,esi
0000001e  imul		eax,esi
00000021  mov		 dword ptr [edi+esi*4+8],eax
// for (int i = 0; i < size; ++i) {
00000025  inc		 esi
00000026  cmp		 esi,ebx
00000028  jl		  00000013

Given up yet?  The first one is C++ and the second is C# . This kind of tight loop is the domain where C++ excels, and yet this simple code illustrates that even there, and in common cases, C# can yield the same results, while offering the guarantee of defined behavior in case of out-of-bounds array access (here, the optimizer eliminated the bounds check through static analysis).

Yes, use the right tool for the job – to me, that means don’t use a complicated, unsafe, poorly tooled language unless you have very specific needs only that language can satisfy. No, that’s certainly not “very often” – most applications certainly don’t need highly tuned custom memory allocation schemes, or to take advantage of every platform-specific CPU instruction. And besides these cases, I don’t see what C++ really gives you to offset a massive decrease in productivity and the corresponding effect on the team’s morale.

Lastly I’d like to address this blanket statement – that C++ is “THE portable language, always has been”. That depends on what your definition of portable is. If by portability, you mean the ability to use the same language on many different platforms, then sure, C++ is one of the most portable languages. A more useful definition of portability, at least from my perspective as a developer, is the ability to re-use the same code with little to no change on many different platforms – and here, C++’s low level nature and barebones runtime runs contrary to this goal. The very point of using C++ is getting closer to the machine – but the closer your code is to the machine, the more machine-specific, and thus, less re-usable it becomes. If you’re going to use C++ for what it excels at, that is, making platform-specific optimizations, then you’ll have to redo that work for every machine you’re targeting. And that’s putting aside the various discrepancies between compilers since, as Herb Sutter said himself, there is no standards-compliant implementation of C++, and the varying quality of standard library implementations.

To conclude, I’m happy to see Microsoft invest in a variety of languages, including C++ which will continue being relevant and necessary. Many existing and upcoming applications will require the level of power and control only C++ provides, and Microsoft's commitment to push this language forward across all their platforms is great news. That said, besides dubious statements such as Herb Sutter’s here, I find the level of emphasis put on C++ out of place and worrying. For games in particular, Microsoft’s current advice is simply misleading. Their “Developing games” for Windows Store documentation starts with the following: “If you want to use advanced game development technologies, such as DirectX Graphics and Gaming, you must know Windows and C++ programming” (emphasis mine). That’s simply not true; with SharpDX providing an up-to-date, lightweight wrapper over the entire DirectX 11 API, all .NET languages can be used to develop Windows Store games – and it’s certainly a favorable option over C++ for everything but the most demanding games. Also, while XNA seems abandoned for the time being, MonoGame makes it very easy to port existing XNA games to the Windows Store. “Using the right tool for the job” begins with being aware of the existing tools.



I fully agree with you. Herb Sutter statements are usually a call for trolling when it comes to the greatness of C++ and the poorness of managed languages...

Still, I have some pending concerns with C# JIT, the lack of static AOT, the lack of SIMD or most of a time a code a but less eficient when it comes to more complex loop with floating point calculations. The fact is that CLR team has not been working a lot for the past few years to provide a better optimized code in C# (or options to generate it), and this is an issue when you want to entirely leverage on C# and you want to achieve almost a 1-1 perf against C++. Unlike H.Sutter, I strongly believe that with a tightly optimized JIT or AOT, some other allocator scheme integrated into the language (other than pure GC), more aggressive unsafe, a proper SIMD integration, C# generated code would be on par or way ahead C++.

For example, we had a simple algorithm with a tight consuming loop that would require CPU cache pre-fetch to access next elements of an array. Without cache pre-prefetch, the loop was running around 1.3ms, with pre-fetch, it was running around 0.5ms (and in a game, this kind of factor is quite significative). The problem in C# is that it is almost impossible, when working with stock managed arrays to perform some cache-prefetch (using directly specific CPU instructions), as the JIT, no matter what you are trying to do, is always trying to access the element of an array even if you are not using it (using by ref for example), so It is not possible to pre-fetch elements into a managed array (unless going through some nasty direct access of the managed array).

Of course, these cases are not common for a casual C# or even C++ developer, but when it comes to optimization, C# is a bit more tricky to optimize as we have less opportunities or more restrictions on the kind of optim we can perform.

Anyway, this whole "Going Native C++" trend is wrong, the futre is absolutely not in such a cluttered language (and I believe that software industry and innovation has more suffered from C++ pitfalls than its supposed greatness). Would love to see how far they have been with projects like Midori and their C# like language fully integrated into the OS, rumors where saying that It was damn efficient...

xoofx, on 04 November 2012 - 13:58, said:

Still, I have some pending concerns with C# JIT, the lack of static AOT, the lack of SIMD or most of a time a code a but less eficient when it comes to more complex loop with floating point calculations. The fact is that CLR team has not been working a lot for the past few years to provide a better optimized code in C# (or options to generate it), and this is an issue when you want to entirely leverage on C# and you want to achieve almost a 1-1 perf against C++.
Thanks for the great comment, yes it's frustrating to think C# could be so much more if there was the will behind it. Instead of making C# everything it could be, and pushing C++ to be point of irrelevancy, Microsoft is choosing to rely on C++ for high-performance assembly and to keep the CLR just "good enough for most apps". Mind you, it's pretty awesome that the CLR is good enough for most apps, but there's definitely a lack of vision there. For instance Mono showed you could get SIMD instructions in .NET with Mono.Simd; there's no reason you couldn't get some OpenCL-like API (granted, there are some third-party bindings for that) or have the JIT automatically emit MMX instructions when possible. I don't fully understand Microsoft's strategy there, perhaps it's just a lack of funds.

xoofx, on 04 November 2012 - 13:58, said:

snipped
By the way, are you the same xoofx who's behind SharpDX? If so, mad props!

Dr_Asik, on 04 November 2012 - 22:53, said:

By the way, are you the same xoofx who's behind SharpDX? If so, mad props!
Ahah, thanks, yes himself :), your post went under my radar with the keyword "SharpDX", and as the subject of your post also resonates with my personal experience (I even wrote a post recently "Going Native 2.0: The Future of WinRT" that had some similar thoughts about it), I ended up here! ;)
Greetings
Great post!
There is a big problem with the ".NET can be faster than C++" argument, however. WPF and its relatives (Silverlight, WinRT XAML) are pits of failure when it comes to performance. Unless you do things in a very specific way, it's very likely that you'll end up with a poorly-performing app. The most ridiculous example: the default WinRT XAML app project template contains some "common" XAML styles & templates. But these templates violate Microsoft's own performance guidelines on ARM hardware because there are too much layers to achieve 60 FPS. Brandon Bray correctly describes C# as a pit of success in the "Evolution of .NET" talk, but that cannot be said of all .NET.

The new "could compilation" stuff in WP8 is related to the work they've been doing on Midori, so let's hope performance will go up when Midori finally comes. The fun part is that C++ performance might go down because the system has to virtualize "unsafe" programs. Take that, Herb Sutter.
IMO, the issue isn't so much the codegen as the typical way of doing things in managed land. People are actively taught to not think about resource management, and the frameworks and stuff like LINQ encourage creating lots of ephemeral objects which require garbage collection. None of this comes for free. JIT tends to be noticeable in large apps as well, and ngen is often not practical or particularly effective.

However, the big deal is that the frameworks that are tightly coupled with the language are generally not engineered for performance. They have the classic Java complexity malaise that comes with trying to make Generic Components which more often than not end up being extremely average at everything, especially in terms of performance.

In short, in managed land you "hide" lots of important things from the programmer and that usually means that they don't think about them. When you work in a more explicit environment like C or C++ everything is more visible and therefore performance problems tend to be more easily identified in my experience. It's also slightly harder to write code, so you end up with less.


I'll probably be labeled "dinosaur" for saying these things, but the truth is I've been burned by the so-called promises of managed code. Managed code is good for a large class of apps, but if you value performance you must go native and think about performance from day one.
Nice try with that sample code. It looks like you were searching for quite some time to find a code that is efficient in C# as in C++.

Now, just modify "size" to be a parameter instead of a constant. This is when C#'s "defined out-of-bounds behavior" adds additional overhead as it has to ensure that "i" won't fall out of the bounds of the array.

Now let's make it even more tricky. Create an additional array that will be used to source the indices that are used for the original array. Obviously, this second array should be non-constant, otherwise the compiler can optimize it out.

Practically, try to check the disassembly of the following function in C++ and C#:


void simple_func(int arr[], int ind[], int size)
for
(int i = 0; i < size; ++i) {
arr[ind[i]] = i * i;
}
}




You don't accept the talk of Sutter, because he didn't name what applications he is talking about, and you came up with a too simple example that you think proves that C# is, or can be as efficient as C++. It doesn't.

aqnuep, on 05 November 2012 - 20:33, said:

You don't accept the talk of Sutter, because he didn't name what applications he is talking about, and you came up with a too simple example that you think proves that C# is, or can be as efficient as C++. It doesn't.
If you absolutely wanted to get performance on that kind of loop, you could use unsafe code.
That way, the performance-critical code can be optimized while the rest of the app is written in a modern language.

Some people have written F# algorithms that beat manually optimized FORTRAN reference implementations. Tell me how you beat that in C++...

aqnuep, on 05 November 2012 - 20:33, said:

Nice try with that sample code. It looks like you were searching for quite some time to find a code that is efficient in C# as in C++.Now, just modify "size" to be a parameter instead of a constant. This is when C#'s "defined out-of-bounds behavior" adds additional overhead as it has to ensure that "i" won't fall out of the bounds of the array.
You look way too far into that example. In context, it's meant to show that C# and C++ are similar to the point where identical code can be valid in both languages, and sometimes even compile to the same assembly. Of course C++ will usually do better in tight loops like this, which is, notice, what I actually said.

While C++'s optimizer is undoubtedly better than .NET's, performance doesn't boil down to compiler optimizations. Most applications do not in fact spend most of their time in busy loops that can be optimized with SIMD instructions. Things like memory management, I/O libraries, facilities for asynchrony etc. all make a big difference. Because .NET makes these facilities much easier and more accessible, given the same development time, choosing .NET is likely to give you a better performing application. Also, because .NET is a multi-language environment, it lets you choose the language that is the best fit, performance-wise, for the type of application you're building. F# for instance is much more fit to large-scale multi-threaded data processing than imperative languages with poor support for immutability like C++ or C#. There are many success stories of projects having been rewritten in F# instead of C++ and achieving huge performance gains, simply because the style lends itself to more parallelizable code.

Dr_Asik, on 05 November 2012 - 21:58, said:

Most applications do not in fact spend most of their time in busy loops that can be optimized with SIMD instructions.
Exactly, but nobody argued about that C# or Java is not suitable if development time is your bottleneck and you don't care about that 1%-500% speed difference that you can potentially have between the two. Both languages have their target audiences, where performance matters more than development time or when you have people with sufficient C++ experience to work fast with it, C++ still remains a better alternative.

Dr_Asik, on 05 November 2012 - 21:58, said:

Also, because .NET is a multi-language environment, it lets you choose the language that is the best fit, performance-wise, for the type of application you're building.
Don't forget that you also have several programming languages that output native code. .NET is a technology, thus if you talk about .NET you should compare it to native code in general. You can do native code in C, C++, Pascal, Delphi, FORTRAN, Objective C (though this one has some overhead compared to other native languages when it comes to method dispatch), etc.

Dr_Asik, on 05 November 2012 - 21:58, said:

F# for instance is much more fit to large-scale multi-threaded data processing than imperative languages with poor support for immutability like C++ or C#. There are many success stories of projects having been rewritten in F# instead of C++ and achieving huge performance gains, simply because the style lends itself to more parallelizable code.
That rather depends on the coding style. F# is suitable for large-scale multi-threaded data processing because the language explicitly requires you to define your algorithm in functional way. But don't forget that no matter if you use C, C++, Pascal, or any other native language, you can still design your algorithms procedural, object oriented, data oriented, functional, or whatever. It's up to you as you have all the low level tools to achieve any of these.

aqnuep, on 06 November 2012 - 13:39, said:

Exactly, but nobody argued about that C# or Java is not suitable if development time is your bottleneck and you don't care about that 1%-500% speed difference that you can potentially have between the two. Both languages have their target audiences, where performance matters more than development time or when you have people with sufficient C++ experience to work fast with it, C++ still remains a better alternative.
There's always a limit on development time. Every project has a finite budget. The question of performance is not therefore "which language is the fastest" but rather "which language will give me, given my finite budget, the best overall application performance". Sutter paints a misleading picture by referring to C++ as the language that gives you twice as efficient applications, or the one you should consider if efficiency is important to you. The fact is that efficiency is always a concern, and development time is also always a concern. Depending on your constraints, C++ may actually give you slower applications, because it requires much more investment in manual optimisation, and unless you have very specific performance requirements and a large amount of resources to invest in that, it's unlikely you'll do better than what .NET gives you out of the box.

Quote

Don't forget that you also have several programming languages that output native code.
Yes but the poor interop story and lack of shared libraries make it difficult to combine them. With .NET I can literally write every type of my application in a different language if I want to and it all works seamelssly, and they all use the same framework and services. Besides Sutter was specifically referring to C++ vs .NET languages in general.

Quote

But don't forget that no matter if you use C, C++, Pascal, or any other native language, you can still design your algorithms procedural, object oriented, data oriented, functional, or whatever. It's up to you as you have all the low level tools to achieve any of these.
That's not entirely true. C++ destructors get in the way of tail call optimization, for instance, so you can't write loops using recursion in general like you can in F#. C++ also has practially no support for immutability, so not only does it make it incredibly difficult for you to reason about program correctness, it also makes it impossible for the compiler to optimize away object copies the way it's done in functional languages.
I think you are misreading the full context. The problem isn't that C# or other languages can't be efficient. It's that their abstraction penalty is higher (and not 2x higher, usually in the 1-10% range). This is something the C++ community takes very seriously and measures quite a bit (and it is non-zero for C++). Normally, you don't spend a lot of time writing out low level code: you build out abstractions so that your solution can be simpler. Those abstractions tend to be layered on top of each other, producing a significant cumulative effect (it takes surprisingly little).

Now, the counter argument to this is notion of doing hotpsot analysis and tuning the small percent of your code that is imposing most of the performance cost of your application. I think that is a very viable argument, but the down side is that most of the time, in most languages, you *do* end up stripping through those abstractions in order to get that hotspot efficient. C++ gives you the ability to have the abstractions be efficient, and particularly if you are proficient with it, this tends to make it a logical choice for anything where you care about efficiency.

cbsmith, on 06 November 2012 - 22:39, said:

I think you are misreading the full context. The problem isn't that C# or other languages can't be efficient. It's that their abstraction penalty is higher (and not 2x higher, usually in the 1-10% range). This is something the C++ community takes very seriously and measures quite a bit (and it is non-zero for C++). Normally, you don't spend a lot of time writing out low level code: you build out abstractions so that your solution can be simpler. Those abstractions tend to be layered on top of each other, producing a significant cumulative effect (it takes surprisingly little).Now, the counter argument to this is notion of doing hotpsot analysis and tuning the small percent of your code that is imposing most of the performance cost of your application.
I think that is a very viable argument, but the down side is that most of the time, in most languages, you *do* end up stripping through those abstractions in order to get that hotspot efficient. C++ gives you the ability to have the abstractions be efficient, and particularly if you are proficient with it, this tends to make it a logical choice for anything where you care about efficiency.
The efficiency ceiling is higher for C++, for the reasons you mention and others, but how close an application needs to be relative to that ceiling, and how close you'll likely to get depending on your budget, can often turn the argument against C++. So, I disagree with the idea that if you care about efficiency, C++ is necessarily a logical choice.

Dr_Asik, on 06 November 2012 - 19:22, said:

There's always a limit on development time. Every project has a finite budget. The question of performance is not therefore "which language is the fastest" but rather "which language will give me, given my finite budget, the best overall application performance".
Yes, however, while productivity of an averagely skilled programmer can be way higher if he/she uses C# compared to C++, more skilled developers don't really benefit that much from using C# over C++. Java and C# were designed with this in mind.
Not to mention that sometimes performance is more important than development time. If the software will run on clusters of servers 24/7 then you simply cannot afford to spare on development time as you'll have to pay more for the additional equipment and their power utilization afterall.
Also, sometimes you simply have a very strict performance budget. Just think about embedded devices or game consoles. In these scenarios you have to sacrifice development time for those additional processor cycles you spare. It's not a coincidence that game development companies working with consoles even write targeted machine assembly code for each platform for the most critical parts.

Dr_Asik, on 06 November 2012 - 19:22, said:

Depending on your constraints, C++ may actually give you slower applications, because it requires much more investment in manual optimisation, and unless you have very specific performance requirements and a large amount of resources to invest in that, it's unlikely you'll do better than what .NET gives you out of the box.
.NET doesn't give you everything out of the box, there is no one-fits-all solution. Also, I don't get your requirement of "manual optimization", as I'm pretty sure in no case a C++ code requires manual optimization to reach the performance of an equivalent C# application. These are just myths.

Dr_Asik, on 06 November 2012 - 19:22, said:

Yes but the poor interop story and lack of shared libraries make it difficult to combine them. With .NET I can literally write every type of my application in a different language if I want to and it all works seamelssly, and they all use the same framework and services.
So you can combine only .NET based applications. Wow! What about all the other ones?
Also, if a compiler cares enough about standard formats and conventions, they do provide compatible ABIs. So actually you can, in fact, have a program that includes shared or static libraries written in C, C++, Pascal, Obj-C, etc.

Dr_Asik, on 06 November 2012 - 19:22, said:

C++ also has practially no support for immutability, so not only does it make it incredibly difficult for you to reason about program correctness, it also makes it impossible for the compiler to optimize away object copies the way it's done in functional languages.
What are you talking about? Every reasonably well written C++ compiler can eliminate most if not all of the object copies. Also, I would recommend you to take a look at some design patterns on immutability and then come back saying it's not possible in C++.

Quote

Yes, however, while productivity of an averagely skilled programmer can be way higher if he/she uses C# compared to C++, more skilled developers don't really benefit that much from using C# over C++. Java and C# were designed with this in mind.
You mean they were designed to benefit average programmers mainly? I don't know what to say, this is just silly. I'll introduce you to very skilled programmers in both C++ and .NET who can tell you they're 10 times more productive in .NET any day if you want. Skill can offset the deficiencies of C++ up to some point, but time is always better invested designing features than maintaining header files.


Quote

Not to mention that sometimes performance is more important than development time. If the software will run on clusters of servers 24/7 then you simply cannot afford to spare on development time as you'll have to pay more for the additional equipment and their power utilization afterall.

Also, sometimes you simply have a very strict performance budget. Just think about embedded devices or game consoles. In these scenarios you have to sacrifice development time for those additional processor cycles you spare. It's not a coincidence that game development companies working with consoles even write targeted machine assembly code for each platform for the most critical parts.
That's true, but there is software that runs on clusters 24/7 and was designed in Java or C#, and is extremely efficient, and no one's talking about rewriting it in C++ for efficiency. I don't disagree that there are times where C++ makes sense, especially on platforms that have limited support for other languages (game consoles for instance), just that it's definitely not obvious that writing something in C++ rather a managed language will give you better performance.

Quote

.NET doesn't give you everything out of the box, there is no one-fits-all solution. Also, I don't get your requirement of "manual optimization", as I'm pretty sure in no case a C++ code requires manual optimization to reach the performance of an equivalent C# application. These are just myths.
Everything .NET does for you has to be done by hand in C++. .NET decides where and how to allocate objects for effiency, that's something you have to figure out on an object-per-object basis in C++. Heap allocations are way faster in .NET unless you implement something like what the CLR does with heap management in C++ (some memory pooling mechanism for instance). .NET also makes it as simple calling .Parallel() on a container to parallelize a computation. It uses a fast thread pool behind the scenes without even you having to know. The standard I/O facilities are way faster than <iostream>. etc.

Writing C++ you're always writing optimizations. Every time you create an object you have to ask yourself where to put it in memory for fastest performance. Every time you create a function signature you have to wonder which parameters can be passed by reference to avoid copies (and on modern CPU architectures it's really hard to tell which is more expensive between a copy and an indirection). The philosophy of the language is to put you in total control of what's going at a low level, but that means you're constantly dealing with that. And if you ignore it you're going to destroy your performance quickly. So yes, to reach the level of performance of a C# application, you're going to have to do, manually, at least as good as the .NET framework runtime.



Quote

So you can combine only .NET based applications. Wow! What about all the other ones?
Also, if a compiler cares enough about standard formats and conventions, they do provide compatible ABIs. So actually you can, in fact, have a program that includes shared or static libraries written in C, C++, Pascal, Obj-C, etc.
You can also very easily use native code from .NET (P/Invoke, C++/CLI, C++/CX)... Yes there is some interop between native code, but can you use template libraries in any language but C++? Can you use an Objective-C class as a C++ class in C++? Or the opposite? How hard is it to call into managed code from C++? What happens then?

Quote

What are you talking about? Every reasonably well written C++ compiler can eliminate most if not all of the object copies. Also, I would recommend you to take a look at some design patterns on immutability and then come back saying it's not possible in C++.
There's no immutability in C++ because all your objects are just blobs of memory and I can always figure out a way to get a pointer to that memory and modify it, and that's almost always valid (save for compile-time constants that can be placed in ROM or just plain inlined). The C++ compiler knows this, so it has much more limited alias analysis. More type-safe languages that use opaque references instead of numerical pointers provide the compiler with richer information and thus more opportunities for eliminating objects.

The C++ compiler can eliminate copies that it can determine with absolute certainty to be unused, but that's mainly limited to temporaries. In a language where objects are immutable (like F#), most object copies can be avoided entirely because there's no difference between two opaque references to the same object and two opaque references to different objects that have the same value. If your language has pointer arithmetic that assumption goes out of the window.

May 2013

S M T W T F S
   1234
567891011
12131415161718
19 20 2122232425
262728293031 

Categories