Archive

Posts Tagged ‘.NET’

C++ vs Managed Code

November 27, 2010 Leave a comment

This blog discusses the virtues of using lower-level languages such as C++ vs. higher-level, managed code such as Microsoft’s C# or Java in a business application atmosphere.

Disclaimer: Although I have roots in C++ and have used Java, I have been using the .NET stack from its inception and tend to gravitate towards this technology. For this reason, it is written with C# in mind. One could make many of the same observations from a Java point of view.

Perhaps the key concern for most would be the speed in which managed code such as C# compares against the performance of C++. Studies have shown that given the same algorithms, C++ has an advantage in this category. It would not be out of line to say that C++ can provide a 10-20% increase in performance.

Java and .NET provide for memory management, hence ‘Managed Code’, whereas the C++ developer must provide their own memory management facility. As with most levels of abstraction, Managed Code must generally consider the problem at hand and as such, lose some optimizations that can be otherwise enjoyed.

I would be remiss if I did not state the obvious: C++ needs a compiler which in turn, is its own level of abstraction. If one was a purist, they would have to consider writing native machine code! Therefore, reasonable people can begin to discuss what level of abstraction is the right one for the job at hand, not the virtues of abstraction itself.

Portability is another issue that should be considered. I don’t know of a computing platform that does not have a C++ compiler. Similarly, Java is easily ported to many operating systems and through the Mono Project, so is .NET.

Proponents of managed code would say that it is more-scalable given this portability issue. Java and .NET for the most part, are alleviated from worrying about the specifics of Linux vs. Windows as their code will just ‘run’.

I often sometimes hear from some industry experts that they need absolute speed. What exactly does that mean? How fast do they need to be and at what cost? There are many ways to achieve speed, especially with current day hardware and software advancements, parallel programming and perhaps the ultimate in scalability, cloud computing.

I would argue that in almost every case, there is so much more to the argument than which platform will provide a faster sorting algorithm! Performance optimization always comes at a price and it is argued on almost every level of computing.

Architects have to consider many application needs, platform cost, development time and cost, code portability and scalability, and more before they embark on a road that will lead them down a particular path.

I believe that using unmanaged code is a form of optimization and as such should be scrutinized carefully before implementation. One can theorize that there is a direct relationship between optimization, abstraction and resources (time and money to develop, test and deploy). In short, the further we go away from abstraction into optimization, performance increases but so do resources. Conversely and generally, performance and resources descrease when tipping the scales in the other direction towards abstraction.

If one subscribes to this theory, the argument can then be reduced to the level of performance gain per unit of resource expended.

It is further my belief that well architected abstraction such as memory management decreases performance minimally while achieving great benefits in resource expenditure.

Moreover, one could argue that if a developer spends their time having to manage memory as is the case with C++ they spend fewer precious brain cycles with the solution that they are attempting to implement. Managed code can have the effect of freeing the mind to worry about the poetry of the code vs. the perils of stepping on ones pointers. The increase that one might gain could easily dissipate if the algorithm is not elegantly constructed in the first place.

I have always been under the belief that if you’re writing an application such as a compiler, C++ is the place to be, but if you’re writing business applications, C# or Java should be considered. I have recently begun to question my theory after I heard Anders Hejlsberg, the architect of C# and the original author of Delphi, announce that the next version of the C# compiler will be written in managed code.

In summary, C++ is alive and well and will be for years and most likely, decades to come. The usage window however, is shrinking as managed code becomes more popular and efficient than ever before.

Categories: Uncategorized Tags: , , ,
%d bloggers like this: