.NET Forum / Languages / C# / March 2006
C# vs C++
|
|
Thread rating:  |
Greg - 10 Dec 2005 23:04 GMT Is there a difference of opinion between C# users and C++ users as to whether C# is generally a replacement for C++? I asked this in the C++ thread and most people people who responded say that C# is not a replacement for C++.
Do C# promoters have a different view?
 Signature Greg McPherran www.McPherran.com
John Timney ( MVP ) - 10 Dec 2005 23:21 GMT Its not an replacement but I would say for many projects its certainly an alternative to traditional C++, and typically a lot faster to develop with in the .NET IDE.
The resulting exeecutables are not as fast as native C or C++ (if you know how to write C/++ of course) but its a very fast language to pick up and run with, thus it has many advantages over coding in C++.
 Signature Regards
John Timney ASP.NET MVP Microsoft Regional Director
> Is there a difference of opinion between C# users and C++ users as to > whether > C# is generally a replacement for C++? I asked this in the C++ thread and > most people people who responded say that C# is not a replacement for C++. > > Do C# promoters have a different view? Greg - 11 Dec 2005 02:15 GMT Thank you, yes it seems like C# is a great language to RAD prototype something iteratively and then one can always convert it to C++.
 Signature Greg McPherran www.McPherran.com
> Its not an replacement but I would say for many projects its certainly an > alternative to traditional C++, and typically a lot faster to develop with [quoted text clipped - 10 lines] > > > > Do C# promoters have a different view? Lloyd Dupont - 11 Dec 2005 04:25 GMT > Thank you, yes it seems like C# is a great language to RAD prototype > something iteratively and then one can always convert it to C++. If it is to convert to MC++ there is no reason (MC++ use MSIl as much as C#). If it is to convert to plain old C++, good luck... you should say: "one could always spend Aeons convert it to C++"
>> Its not an replacement but I would say for many projects its certainly an >> alternative to traditional C++, and typically a lot faster to develop [quoted text clipped - 15 lines] >> > >> > Do C# promoters have a different view? Mark Rae - 11 Dec 2005 08:59 GMT > and then one can always convert it to C++. Why???
Scherbina Vladimir - 11 Dec 2005 22:46 GMT > Thank you, yes it seems like C# is a great language to RAD prototype > something iteratively and then one can always convert it to C++. It will take a big amount of time. At least because c# has different syntax, and in c++ you should watch for commiting / releasing of memory, etc. Yes, for a small project it would not be diffcult to convert C# code to C++, but imagine corporate application that has a thousands of lines of code. You will need many men-moon and time.
 Signature Vladimir
Greg - 21 Dec 2005 19:11 GMT Regarding performance, I would hope that floating point (i.e. float, double) math operations in .NET will still use the math hardware in the CPU. I.e. the runtime layer, even though not native, will still access hardware facilities as needed?
Jon Skeet [C# MVP] - 21 Dec 2005 19:25 GMT > Regarding performance, I would hope that floating point (i.e. float, double) > math operations in .NET will still use the math hardware in the CPU. I.e. the > runtime layer, even though not native, will still access hardware facilities > as needed? It will certainly still do the operations in hardware - but whether the JIT has long enough to work out whether it can parallelise operations using SSE or the like is a different matter. I wouldn't like to speculate on whether it does or not, but that's an area a static compiler *might* get an advantage in. (That's not to say that any such advantage will stay around forever, of course.)
 Signature Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too
Greg - 21 Dec 2005 20:38 GMT Thank you.
> It will certainly still do the operations in hardware... Isn't this another example of why the fuss about even native C++ performance over C# is overrated. I.e. it still comes down to hardware for a lot of the number crunching and graphics display.
 Signature Greg McPherran www.McPherran.com
> > Regarding performance, I would hope that floating point (i.e. float, double) > > math operations in .NET will still use the math hardware in the CPU. I.e. the [quoted text clipped - 7 lines] > compiler *might* get an advantage in. (That's not to say that any such > advantage will stay around forever, of course.) Jon Skeet [C# MVP] - 21 Dec 2005 20:51 GMT > > It will certainly still do the operations in hardware... > > Isn't this another example of why the fuss about even native C++ performance > over C# is overrated. I.e. it still comes down to hardware for a lot of the > number crunching and graphics display. Well, not entirely. If a native compiler can use parallelism in a way that the JIT can't, the performance difference could be enormous. It's unlikely to affect most applications, however.
 Signature Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too
Lloyd Dupont - 22 Dec 2005 06:13 GMT Theorically speaking JITed software could even be faster because: 1. they are, after all, fully compiled (by the JIT) as well 2. the JIT could optimize the code for the current hardware.
It's not true because because some CPU/Memory hungry optimization are not done by the JIT while they are done by the native compiler. But, in fact, unoptimized native C++ could be much slower than C# code. For reference look at that Article by Richard Grimes http://www.grimes.demon.co.uk/dotnet/man_unman.htm
 Signature Regards, Lloyd Dupont
NovaMind development team NovaMind Software Mind Mapping Software <www.nova-mind.com>
> Thank you. > [quoted text clipped - 20 lines] >> compiler *might* get an advantage in. (That's not to say that any such >> advantage will stay around forever, of course.) Richard Grimes - 11 Dec 2005 18:02 GMT > The resulting exeecutables are not as fast as native C or C++ (if you > know how to write C/++ of course) but its a very fast language to > pick up and run with, thus it has many advantages over coding in C++. Done any comparisons? Try it, you might be surprised.
I did some comparisons and was surprised and showed them to someone on the C++ team and he told me that they were just what he expected.
The problem is that when most people think of .NET they think 'interpreter', or 'Java', or godforbid, Visual Basic. And so they think 'bad performance'. They shouldn't.
I think of the JIT compiler as being the back end of a C++ compiler (it was written by people from the C++ team). If you discount the time that the JIT compiler takes then for fully trusted code there is no reason why it should be significantly slower than code generated by the unmanaged C++ compiler. The only possible issue may be garbage collection.
Of course, if you know of other areas of the .NET runtime which are significantly slower than the equivalent in Win32 then you should post them here so that we all can avoid using them <g>.
Richard
 Signature http://www.grimes.demon.co.uk/workshops/fusionWS.htm http://www.grimes.demon.co.uk/workshops/securityWS.htm
John Timney ( MVP ) - 11 Dec 2005 21:05 GMT I think the average figure I saw being spoken about was to expect up to 20% slower. Not significant, and certainly with the increased productivity something that can be worked on in budget if it was an issue. Personally, I've not done a comparison to the levels you would have done Richard.
I know that some of the work I have been invoved in recently using native C would have been unworkable in .NET as performance was the driving factor. For me - as long as complete end to end performance was not the driving factor I would choose c# over c/++ anytime.
 Signature Regards
John Timney ASP.NET MVP Microsoft Regional Director
>> The resulting exeecutables are not as fast as native C or C++ (if you >> know how to write C/++ of course) but its a very fast language to [quoted text clipped - 20 lines] > > Richard Richard Grimes - 12 Dec 2005 15:21 GMT >I think the average figure I saw being spoken about was to expect up to >20% slower. Not significant, and certainly with the increased >productivity something that can be worked on in budget if it was an >issue. Personally, I've not done a comparison to the levels you would >have done Richard. I don't know where you got that figure from, but it is nowhere near the figure I was told. Here are my comparisons for v2.0:
http://www.grimes.demon.co.uk/dotnet/man_unman.htm
For code that is not optimized or code that is optimized for space managed C++ is *quicker* than unmanaged code (native code takes 138% the time of that managed code takes). In the case of optimized for speed, the managed code takes 4% more time, so it is only slightly slower than managed code.
I performed these measurements two years ago for v1.1 and I got a figure of 6% longer for speed optimised managed C++ over speed optimized native C++. I showed this to the guy responsible for performance on the C++ team and his comment was 'that looks right'. No one mentioned a figure of 20%, and as my measurements for a computationally intensive routine show, a figure of 20% is far too high.
In the best case native code has a mere 4% advantage over managed code.
> I know that some of the work I have been invoved in recently using > native C would have been unworkable in .NET as performance was the > driving factor. If it involves direct memory manipulation (which is what C was designed to do) then I agree, but only because .NET makes a point of trying to prevent you from performing direct memory access. If it uses manipulation of arrays of values then C is not likely to be significantly faster than a managed language. Furthermore, your C# development will take a lot less time than the C development, which is the whole point of .NET - it is now being marketed as a RAD solution.
> For me - as long as complete end to end performance was not the > driving factor I would choose c# over c/++ anytime. I think you should take 'performance' out of your criteria because it really is irrelevant in this discussion. .NET is NOT significantly slower than native code. There are places where it is inappropriate (for example device drivers and IMO services), but in terms of performance you'll not notice a 4% difference.
Richard
 Signature Fusion Tutorial: http://www.grimes.demon.co.uk/workshops/fusionWS.htm Security Tutorial: http://www.grimes.demon.co.uk/workshops/securityWS.htm
John Timney ( MVP ) - 12 Dec 2005 20:00 GMT Cant remember where the figure came from but I'm sure it was at a Redmond presentation, anyway - I'm glad you enlightened me with research Richard. The figures you quote will be useful in the future.
In my case 4% for 2 billion computations does count very much and they are extremely large numbers involved in intense mathematical computations. In this instance I have no choice but to consider those minor percentages. I do however entirely agree with your point though, typically 4% is really negligable, especially when you weight up the benefits of turnaround time for development.
 Signature Regards
John Timney ASP.NET MVP Microsoft Regional Director
>>I think the average figure I saw being spoken about was to expect up to >>20% slower. Not significant, and certainly with the increased [quoted text clipped - 43 lines] > > Richard Bruce Wood - 12 Dec 2005 20:29 GMT I'm impressed by your benchmarks... really. However, keep in mind that you're comparing managed C++ versus unmanaged C++, but Redmond has always claimed that managed C++ is faster than managed C# due to better optimizing done by the C++.NET compiler.
That said, the last word I heard is that starting with .NET 2.0 they would be concentrating their efforts on introducing better optimization at the JIT stage, so any differences between the two managed languages should diminish in the future.
Jim Holmes - 11 Dec 2005 12:50 GMT > Is there a difference of opinion between C# users and C++ users as to > whether C# is generally a replacement for C++? I asked this in the C++ > thread and most people people who responded say that C# is not a > replacement for C++. > > Do C# promoters have a different view? Check out Kate Gregory's blog at http://www.gregcons.com/KateBlog/default.aspx. She's a passionate promoter of of C++ and her thoughts are well worth reading.
Bob Powell [MVP] - 11 Dec 2005 13:18 GMT It takes me less than half the time to develop a program in C# than it does to develop the equivalent functionality in C++. I still can't write a low level driver in C# tough...
Horses for courses.
 Signature Bob Powell [MVP] Visual C#, System.Drawing
Ramuseco Limited .NET consulting http://www.ramuseco.com
Find great Windows Forms articles in Windows Forms Tips and Tricks http://www.bobpowell.net/tipstricks.htm
Answer those GDI+ questions with the GDI+ FAQ http://www.bobpowell.net/faqmain.htm
All new articles provide code in C# and VB.NET. Subscribe to the RSS feeds provided and never miss a new article.
> Is there a difference of opinion between C# users and C++ users as to > whether > C# is generally a replacement for C++? I asked this in the C++ thread and > most people people who responded say that C# is not a replacement for C++. > > Do C# promoters have a different view? Richard Grimes - 11 Dec 2005 18:05 GMT > It takes me less than half the time to develop a program in C# than > it does to develop the equivalent functionality in C++. I still can't > write a low level driver in C# though... You still cannot (should not) in managed C++ either <g>.
But I am not sure if the OP is referring to C++ in general, or to managed C++.
> Horses for courses. ... and for glue when they are knackered ;-)
Richard
 Signature http://www.grimes.demon.co.uk/workshops/fusionWS.htm http://www.grimes.demon.co.uk/workshops/securityWS.htm
Marc Noon - 11 Dec 2005 19:53 GMT A good litmus test would be to write a prime number generator.
Then after 10000 or so just see how long it took in c# and c++.
Marc N.
 Signature In all the universe, and in all of space, resides a time and a place, when you blink, let it be, in a sequence of events.
> It takes me less than half the time to develop a program in C# than it > does to develop the equivalent functionality in C++. I still can't write a [quoted text clipped - 9 lines] >> >> Do C# promoters have a different view? Jon Skeet [C# MVP] - 11 Dec 2005 21:08 GMT > A good litmus test would be to write a prime number generator. > > Then after 10000 or so just see how long it took in c# and c++. Well, that's a good litmus test if your application is going to be processor intensive in a maths kind of way. I suspect that most applications don't actually have a bottleneck like that. I suspect most applications fall into one or more categories:
1) No real bottleneck - performance is perfectly acceptable 2) Database bottleneck 3) Network bottleneck 4) Graphics bottleneck (which may be processor related, but in a more framework dependent way, eg using a framework which doesn't have hardware acceleration)
 Signature Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too
Jon Skeet [C# MVP] - 11 Dec 2005 21:32 GMT > Well, that's a good litmus test if your application is going to be > processor intensive in a maths kind of way. I suspect that most [quoted text clipped - 7 lines] > framework dependent way, eg using a framework which doesn't have > hardware acceleration) I missed one I'd thought of before: memory bottlenecks, where you have to control the memory allocation really, really tightly. (Embedded systems etc.)
 Signature Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too
Richard Grimes - 12 Dec 2005 13:40 GMT > 1) No real bottleneck - performance is perfectly acceptable > 2) Database bottleneck > 3) Network bottleneck > 4) Graphics bottleneck (which may be processor related, but in a more > framework dependent way, eg using a framework which doesn't have > hardware acceleration) Actually, in most cases the bottleneck is the user <g>. The application has to wait for user imput. But I agree with you and this is why I jumped on John for saying that .NET is slower, because 1) it isn't (I will post results later today to show this) and 2) in many cases the slight differences between .NET and unmanaged code is insignificant anyway.
The one thing that might make a difference is memory allocation. Allocation is a lot quicker in .NET than in unmanaged code, but when the GC kicks in the .NET code gets a big hit.
Richard
 Signature Fusion Tutorial: http://www.grimes.demon.co.uk/workshops/fusionWS.htm Security Tutorial: http://www.grimes.demon.co.uk/workshops/securityWS.htm
Richard Grimes - 12 Dec 2005 11:33 GMT > A good litmus test would be to write a prime number generator. > > Then after 10000 or so just see how long it took in c# and c++. I did it with a Fast Fourier Transform, compiled as managed C++ and native C++. I ran the managed C++ library once before running it through a timing loop (this was to discount the effect of JIT compilation).
Somewhere I have the results, but they varied depending on the optimization switches I used. The fastest time was for one of the managed C++ builds, but only just.
Richard
 Signature Fusion Tutorial: http://www.grimes.demon.co.uk/workshops/fusionWS.htm Security Tutorial: http://www.grimes.demon.co.uk/workshops/securityWS.htm
Willy Denoyette [MVP] - 12 Dec 2005 15:52 GMT >> A good litmus test would be to write a prime number generator. >> [quoted text clipped - 9 lines] > > Richard Richard,
Note that the topic is C# vs. C++, if you run your FFT using C# you will notice a performance drop compared to managed C++ (I guess you are refering to C++/CLI) . This is the result of a slightly better optimization done by the managed C++ compiler.
Willy.
Richard Grimes - 12 Dec 2005 17:51 GMT > Note that the topic is C# vs. C++, if you run your FFT using C# you > will notice a performance drop compared to managed C++ (I guess you > are refering to C++/CLI) . This is the result of a slightly better > optimization done by the managed C++ compiler. OK, if you insist, however, I don't thjink it is necessary because I think the performance is determined by the runtime and not by the Microsoft .NET compilers.
I have converted the managed C++ FFT routine over to C# and I have repeated the tests. These results show that the not optimised C# library is just slower than the not optimised C++ library, but the optimised C# is slightly faster than the optimised managed C++ library. The difference in the optimised librarys is very small. This reinforces the point in the article that the *runtime* does the optimizations, and that the optimizations provided by the .NET compilers are marginal.
http://www.grimes.demon.co.uk/dotnet/man_unman.htm
Please don't suggest I repeat the test with VB.NET, I refuse to touch that language. <g>
Richard
 Signature Fusion Tutorial: http://www.grimes.demon.co.uk/workshops/fusionWS.htm Security Tutorial: http://www.grimes.demon.co.uk/workshops/securityWS.htm
Willy Denoyette [MVP] - 12 Dec 2005 21:20 GMT >> Note that the topic is C# vs. C++, if you run your FFT using C# you >> will notice a performance drop compared to managed C++ (I guess you [quoted text clipped - 4 lines] > the performance is determined by the runtime and not by the Microsoft .NET > compilers. Agreed, the differences between C# and managed C++ (I've compiled to managed code using /clr:safe) are extremely small, here it's C# which is the fastest ~5% compared with managed C++.
[my results using v2.0] Average for native code: 9780,000 µseconds Average for managed C++ code: 10820 µseconds Average for managed C# code: 10272 µseconds
Other benchmarks I ran show the same pattern, sometimes C# is faster sometimes it's slower, but the delta's remain within the +/-5% ranges. It's also great to see the difference between native and managed getting smaller from v1.x and v2.
> I have converted the managed C++ FFT routine over to C# and I have > repeated the tests. These results show that the not optimised C# library [quoted text clipped - 3 lines] > article that the *runtime* does the optimizations, and that the > optimizations provided by the .NET compilers are marginal. Agreed.
> http://www.grimes.demon.co.uk/dotnet/man_unman.htm > > Please don't suggest I repeat the test with VB.NET, I refuse to touch that > language. <g> LOL.
Willy.
Greg - 29 Dec 2005 19:40 GMT > > I have converted the managed C++ FFT routine over to C# and I have > > repeated the tests. These results show that the not optimised C# library [quoted text clipped - 5 lines] > > Agreed. Bingo! This tells me right here that for .NET there is no good reason to use C++ if one is more productive in C#. Thank you will Richard and Willy.
riscy@onetel.com - 13 Dec 2005 08:14 GMT Hi Richard
I'm working on FFT project (from strach) for education purpose, I just create nice time-domain waveform window and spectrum window and running DFT at the moment. I would be interested to learn more of your FFT experience in C# world.
Have you used FFTW (fastest fourier transform in the west)?, I was wondering how you managed to setup interop service for the which is written in C.
Is there way of keep track of the message, it does not seem to have forward email if new message is added?.
Regards
> > A good litmus test would be to write a prime number generator. > > [quoted text clipped - 13 lines] > Security Tutorial: > http://www.grimes.demon.co.uk/workshops/securityWS.htm Richard Grimes - 13 Dec 2005 09:35 GMT > I'm working on FFT project (from strach) for education purpose, I just > create nice time-domain waveform window and spectrum window and > running DFT at the moment. I would be interested to learn more of > your FFT experience in C# world. Not very much <g> If you asked me that 12 years ago I would be able to tell you *everything* about the routine and the theories current at the time. But now, all I know is that it is a computationally intensive algorithm.
> Have you used FFTW (fastest fourier transform in the west)?, I was > wondering how you managed to setup interop service for the which is > written in C. I haven't used it. But the procedure is not a problem. The project on my site shows three approaches.
1) Identify the 'main' entry point in the library and add extern "C" __declspec(dllexport) to it, and then compile the library as a DLL. Then use platform invoke to call it from C#.
2) Identify the 'main' entry point make this a public static method of a public type and compile it as managed C++. Access the assembly as you would access any other assembly from C#.
3) Convert the entire code to C#. This is perhaps the least best option, but if the code does not use the CRT (maths routines excepted), nor uses pointers, and only uses array syntax then it should not be too difficult. The conversion of the project on my site took about 15 minutes. Pay particular attention to long - in C this is 32 bits, in C# it is 64 bits.
Richard
 Signature Fusion Tutorial: http://www.grimes.demon.co.uk/workshops/fusionWS.htm Security Tutorial: http://www.grimes.demon.co.uk/workshops/securityWS.htm
Scherbina Vladimir - 11 Dec 2005 22:43 GMT > It takes me less than half the time to develop a program in C# than it > does to develop the equivalent functionality in C++. I still can't write a > low level driver in C# tough... Each task has it's instruments. an interesting article about .net applications: http://www.sysinternals.com/blog/2005/04/coming-net-world-im-scared.html
 Signature Vladimir
Lloyd Dupont - 12 Dec 2005 11:09 GMT Another comparison. I wrote my own rich text editor, not derived from TextBoBase, but from Control, and handling international character through uniscribe.
If I copy paste 400Kb of text It takes roughly the same amount of time (~1second) to do all the layouting and drawing and 20% more memory than MS-Word.
I could tell now that I no longer believe this bullshit about .NET being inneficient.
The certainly profile their code in C, do the same in C# and you would yield similar results!
>> It takes me less than half the time to develop a program in C# than it >> does to develop the equivalent functionality in C++. I still can't write [quoted text clipped - 3 lines] > applications: > http://www.sysinternals.com/blog/2005/04/coming-net-world-im-scared.html Richard Grimes - 12 Dec 2005 15:27 GMT >> It takes me less than half the time to develop a program in C# than >> it does to develop the equivalent functionality in C++. I still [quoted text clipped - 3 lines] > applications: > http://www.sysinternals.com/blog/2005/04/coming-net-world-im-scared.html Thanks, that is an excellent article and it fits in with what a lot of people have been telling me about the usage of .NET in Vista (it uses too much memory, so Microsoft are keeping the .NET usage in Vista as low as they can, and certainly they are keeping it out of anything that matters like the shell and system services).
Here's an article I have written about .NET usage in Vista:
http://www.grimes.demon.co.uk/dotnet/vistaAndDotnet.htm
Richard
 Signature Fusion Tutorial: http://www.grimes.demon.co.uk/workshops/fusionWS.htm Security Tutorial: http://www.grimes.demon.co.uk/workshops/securityWS.htm
yuje101 - 15 Mar 2006 00:50 GMT Anyone can answer my question?
-- In C#, one can use new Object [,] {{"name","=","Yuje"}, {"ID","=","1"}} to represent "where" in SQL select, But what about in C++
Can we say
Object *obj[] ={{"name","=","Yuje"}, {"ID","=","1"}} ? What was the corresponding of C++ to C# in this case.
 Signature Yuje Peng
Yuje Peng
> Is there a difference of opinion between C# users and C++ users as to whether > C# is generally a replacement for C++? I asked this in the C++ thread and > most people people who responded say that C# is not a replacement for C++. > > Do C# promoters have a different view?
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|