Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. High performance c#

High performance c#

Scheduled Pinned Locked Moved The Lounge
csharpc++performancevisual-studiolinq
41 Posts 21 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Rob Philpott

    There seems to be a general view that if you want to do some serious compute bound work C++ will always win over C# because of its unmanagedness. This may be the case, but then there's an alternative argument that goes if you sidestep garbage collection, and use unsafe constructs you get close, and because your code gets JITed, there may be a chance to hone the code for the actual CPU it will run on which could actually make .NET faster over a generalised native binary. There seems to be better scope for this if you use the SIMD enabled System.Numerics. I've been mucking around with sound synthesis in .NET recently, and by using unmanaged memory (Marshal.AllocHGlobal) and unsafe pointers the performance is good enough. In fact I've got it so there are virtually no garbage collections at all (beware of Linq - it creates enumerators all over the place). GCs are terrible news for audio, because a delay can mean a buffer not being ready in time and you get nasty clicks. It means a different approach to coding, but it's still miles preferable to header files and linking libs and all that 32/64 bit nastiness you get with C++. Then I stumbled on this: [https://www.bepuentertainment.com/\](https://www.bepuentertainment.com/) This is a stunning masterclass in what C# can do. I recommend having a look at the video, and reading the bits about GPU vs. CPU and 'a different kind of c#' entries. I'm fairly hard to impress these days, but I've downloaded the code built it and played with it and just... wow. Moreover, I'd argue its the last nail in the coffin of the argument that C# is not a viable choice for high performance compute bound work.

    Regards, Rob Philpott.

    N Offline
    N Offline
    Nand32
    wrote on last edited by
    #25

    The modern C++ is trying to get less cryptic* & the modern C# is trying to get more performant. Someday both meet at a point and people get to choose the project template as easy as picking a green apple or a red apple. :) *a long way to go

    D J 2 Replies Last reply
    0
    • N Nand32

      The modern C++ is trying to get less cryptic* & the modern C# is trying to get more performant. Someday both meet at a point and people get to choose the project template as easy as picking a green apple or a red apple. :) *a long way to go

      D Offline
      D Offline
      Dean Roddey
      wrote on last edited by
      #26

      I'm not sure at all that I'd agree that modern C++ is less cryptic. The crazy templatization of modern C++ can make it extremely cryptic.

      Explorans limites defectum

      N 1 Reply Last reply
      0
      • D Dean Roddey

        I'm not sure at all that I'd agree that modern C++ is less cryptic. The crazy templatization of modern C++ can make it extremely cryptic.

        Explorans limites defectum

        N Offline
        N Offline
        Nish Nishant
        wrote on last edited by
        #27

        Only if you are authoring libraries. If you are a consumer, it's not that bad.

        Nish Nishant Consultant Software Architect Ganymede Software Solutions LLC www.ganymedesoftwaresolutions.com

        D 1 Reply Last reply
        0
        • N Nish Nishant

          Only if you are authoring libraries. If you are a consumer, it's not that bad.

          Nish Nishant Consultant Software Architect Ganymede Software Solutions LLC www.ganymedesoftwaresolutions.com

          D Offline
          D Offline
          Dean Roddey
          wrote on last edited by
          #28

          If you are writing pretty substantial applications, there's still a lot of grunt work infrastructure code in such things, so you'd still end up doing a lot of this stuff at your level as well.

          Explorans limites defectum

          N 1 Reply Last reply
          0
          • R Rob Philpott

            Dean Roddey wrote:

            If you end up having to do lots of unsafe C#, you sort of give up the biggest advantages of C# without getting the real benefits of C++.

            I kind of agree, but I think there's a middle-ground there. For me going back to c++, working with header files drives me mad. And the way that .NET assemblies are self-describing so you can just reference them and use them without all those header files and linking is glorious. For me, these are some of the biggest advantages and are still there regardless of going unsafe.

            Regards, Rob Philpott.

            R Offline
            R Offline
            Rick York
            wrote on last edited by
            #29

            I have noticed the issues you are describing often these days. Actually, I have noticed them for a while but I have figured out ways to simplify them and now I notice when other people don't do those things.

            "They have a consciousness, they have a life, they have a soul! Damn you! Let the rabbits wear glasses! Save our brothers! Can I get an amen?"

            1 Reply Last reply
            0
            • R Rob Philpott

              There seems to be a general view that if you want to do some serious compute bound work C++ will always win over C# because of its unmanagedness. This may be the case, but then there's an alternative argument that goes if you sidestep garbage collection, and use unsafe constructs you get close, and because your code gets JITed, there may be a chance to hone the code for the actual CPU it will run on which could actually make .NET faster over a generalised native binary. There seems to be better scope for this if you use the SIMD enabled System.Numerics. I've been mucking around with sound synthesis in .NET recently, and by using unmanaged memory (Marshal.AllocHGlobal) and unsafe pointers the performance is good enough. In fact I've got it so there are virtually no garbage collections at all (beware of Linq - it creates enumerators all over the place). GCs are terrible news for audio, because a delay can mean a buffer not being ready in time and you get nasty clicks. It means a different approach to coding, but it's still miles preferable to header files and linking libs and all that 32/64 bit nastiness you get with C++. Then I stumbled on this: [https://www.bepuentertainment.com/\](https://www.bepuentertainment.com/) This is a stunning masterclass in what C# can do. I recommend having a look at the video, and reading the bits about GPU vs. CPU and 'a different kind of c#' entries. I'm fairly hard to impress these days, but I've downloaded the code built it and played with it and just... wow. Moreover, I'd argue its the last nail in the coffin of the argument that C# is not a viable choice for high performance compute bound work.

              Regards, Rob Philpott.

              J Offline
              J Offline
              jschell
              wrote on last edited by
              #30

              Rob Philpott wrote:

              There seems to be a general view that if you want to do some serious compute bound work C++ will always win over C# because of its unmanagedness

              Same view is true for Java. However.... First that claim supposes that the person doing the programming does in fact have enough experience in C++ to create something faster in that language. In my experience such claims have originated from people that just prefer that language or some other that that they claim is 'better' based on subjective terms rather than objective terms. (Might be relevant to note that I have at least 15 years of C/C++ experience.) Second business programming in terms of programming is never about algorithmic performance. And I am not a person that throws absolutes around, but in terms of statistical significance the probability that a the algorithm in the business is the bottleneck is so far below 1% that it is effectively zero. What actually impacts performance is architecture and design. And business processes, since a faster application might be possible if one refactored 15 years of legacy code but that just isn't cost effective for most businesses. Third if there is in fact a single algorithmic bottleneck and one can find the expertise then obviously implementing it that way might not be the advantage one thinks. I consider it very unlikely that code (vs design decisions at the code level) are going to provide a 1000% speed up or even a 100% speed up. So what are the cases where a 10% speed up, with no other design/architecture changes (which can be done in any language) are going to provide that much actual real benefit to the business?

              1 Reply Last reply
              0
              • OriginalGriffO OriginalGriff

                No, I don't. And you don't. But ... we've both seen it done, and sometimes in production code. :omg: I think we have to accept that developers are not what they used to be (and "Hoorah!" for that in some cases), projects generally are a lot larger and more complex than they were, and that we have to change to languages which facilitate that.

                Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

                J Offline
                J Offline
                jschell
                wrote on last edited by
                #31

                OriginalGriff wrote:

                But ... we've both seen it done, and sometimes in production code.

                I have seen it many times in production code. And would have been more except that I started reviewing the code of others and catching it. I worked at a place with formal code reviews and I was still the only one finding them. Not to mention that these sorts of problems impact the server far more.

                OriginalGriff wrote:

                I think we have to accept that developers are not what they used to be

                But applications now are not the same either. When I started I wrote the UI and the back end. Now I can't even effectively review the UI code and the developers cannot write the back end code.

                1 Reply Last reply
                0
                • L Lost User

                  OriginalGriff wrote:

                  I suspect that an average C# coder will produce code that is less efficient than a skilled C++ coder to do the same job - but I also suspect that he'll produce it in less time, and it'll be more easily maintainable by an average developer. And with the performance of modern machines that's a critical factor in most cases. Additionally, I suspect that a skilled C# developer will produce better, faster code than an average C++ dev, and get it out the door quicker as well.

                  by the same token with newer machines having so much more memory than before you could argue garbage collection / free-ing unused memory can also be ignored in short running programs or where only alloc-ing space for relatively small buffers/structures. [which could also have dramatic results for performance.] OTOH, using the excuse: "todays machines are faster / bigger memory... so optimisation and/or garbage collection matters little" is when people copy or extend that code into small/short running programs into big / long running ones. OP's "sound" app may work fine in optimised C# [and/or without cleaning up unused memory] but when added to a video editing suite would that still be true? On my bicycle I can match (if not beat) the bus on a 10 mile commute and get away without refueling on the way, but let's make that 100 miles. right tools for the job.

                  Message Signature (Click to edit ->)

                  J Offline
                  J Offline
                  jschell
                  wrote on last edited by
                  #32

                  Lopatir wrote:

                  but let's make that 100 miles.

                  Nice analogy. But most business programming these days is more akin to a trip to china or even the moon.

                  1 Reply Last reply
                  0
                  • D Dean Roddey

                    C++ is really a system's language, for large systems, where lots of incremental performance gains (or losses) adds up. And of course the thing is, if you looked under the hood of the C# libraries, I'm guessing there's probably a lot of C++ down in there. Languages like C# are good if you are sitting on the top of the food chain. If your code needs to live from the metal up to the UI, then something like C++, with all it's issues, is likely more practical. It's one of those languages that maybe doesn't do anything one thing the absolute best but it does a broad spectrum of things well. If the code base has to cover that broad spectrum, it adds up. If you end up having to do lots of unsafe C#, you sort of give up the biggest advantages of C# without getting the real benefits of C++.

                    Explorans limites defectum

                    J Offline
                    J Offline
                    jschell
                    wrote on last edited by
                    #33

                    Dean Roddey wrote:

                    And of course the thing is, if you looked under the hood of the C# libraries, I'm guessing there's probably a lot of C++ down in there.

                    The C# compiler and most of the libraries are written in C#.

                    1 Reply Last reply
                    0
                    • D Dean Roddey

                      If you are writing pretty substantial applications, there's still a lot of grunt work infrastructure code in such things, so you'd still end up doing a lot of this stuff at your level as well.

                      Explorans limites defectum

                      N Offline
                      N Offline
                      Nish Nishant
                      wrote on last edited by
                      #34

                      That's a fair point.

                      Nish Nishant Consultant Software Architect Ganymede Software Solutions LLC www.ganymedesoftwaresolutions.com

                      1 Reply Last reply
                      0
                      • R Rob Philpott

                        Dean Roddey wrote:

                        If you end up having to do lots of unsafe C#, you sort of give up the biggest advantages of C# without getting the real benefits of C++.

                        I kind of agree, but I think there's a middle-ground there. For me going back to c++, working with header files drives me mad. And the way that .NET assemblies are self-describing so you can just reference them and use them without all those header files and linking is glorious. For me, these are some of the biggest advantages and are still there regardless of going unsafe.

                        Regards, Rob Philpott.

                        D Offline
                        D Offline
                        Dean Roddey
                        wrote on last edited by
                        #35

                        While I wouldn't defend C++ that much on this front, I think you are making C# a little more straightforward than it really is. Someone I've been helping with a C# project has jumped through endless hoops with versioning of things and all the many variations of .Net, dealing with client vs. server side blazor and such. I look in the .csproj files of some of those projects and they are very complicated. I would also say that not all C++ is as bad either. My stuff is nothing like what most C++ is wrt to build infrastructure and includes and such. Each library has a single public include file that brings in anything needed for the public interfaces of that library and exposes all the public interfaces, and a private header that brings in stuff only needed internally (which includes the public header). Each program has a main include file that brings anything needed within that program, including its own other headers. So each library cpp file usually includes just its own internal header. And each program cpp file typically just includes its own public header. There are some occasional exceptions, but generally that's it.

                        Explorans limites defectum

                        1 Reply Last reply
                        0
                        • R Rob Philpott

                          There seems to be a general view that if you want to do some serious compute bound work C++ will always win over C# because of its unmanagedness. This may be the case, but then there's an alternative argument that goes if you sidestep garbage collection, and use unsafe constructs you get close, and because your code gets JITed, there may be a chance to hone the code for the actual CPU it will run on which could actually make .NET faster over a generalised native binary. There seems to be better scope for this if you use the SIMD enabled System.Numerics. I've been mucking around with sound synthesis in .NET recently, and by using unmanaged memory (Marshal.AllocHGlobal) and unsafe pointers the performance is good enough. In fact I've got it so there are virtually no garbage collections at all (beware of Linq - it creates enumerators all over the place). GCs are terrible news for audio, because a delay can mean a buffer not being ready in time and you get nasty clicks. It means a different approach to coding, but it's still miles preferable to header files and linking libs and all that 32/64 bit nastiness you get with C++. Then I stumbled on this: [https://www.bepuentertainment.com/\](https://www.bepuentertainment.com/) This is a stunning masterclass in what C# can do. I recommend having a look at the video, and reading the bits about GPU vs. CPU and 'a different kind of c#' entries. I'm fairly hard to impress these days, but I've downloaded the code built it and played with it and just... wow. Moreover, I'd argue its the last nail in the coffin of the argument that C# is not a viable choice for high performance compute bound work.

                          Regards, Rob Philpott.

                          S Offline
                          S Offline
                          Stuart Dootson
                          wrote on last edited by
                          #36

                          This [article](https://blogs.unity3d.com/2019/02/26/on-dots-c-c/) might be of interest to you - Unity have developed a compiler called [Burst](https://docs.unity3d.com/Packages/com.unity.burst@0.2/manual/index.html). This uses Roslyn as a frontend, but they've developed a performance oriented IL -> object code translator with LLVM. They've also done [work with the garbage collector](https://blogs.unity3d.com/2018/11/26/feature-preview-incremental-garbage-collection/), to make its performance more predictable and less spiky.

                          Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                          1 Reply Last reply
                          0
                          • R Rob Philpott

                            I did a little experiment a while ago - calculating prime numbers in C++ and a handcrafted assembly version, same algorithm obviously. The C++ one ran quicker, never found out why. CPUs are so damn complicated these days. In the glory days you could look at your instruction and know how many clock cycles it took. I think the on chip caches change all that. In my assembly days you'd get everything into registers and keep it there as long as possible. Perhaps these days the cache is good enough. Reluctantly I concede it's best left to the compiler. The main thing in the C#/C++ for me though is that the C++ you see is so damn cryptic. I don't think it needs to be, but usually just is. Maybe its the mindset.

                            Regards, Rob Philpott.

                            G Offline
                            G Offline
                            Gerardo Orozco
                            wrote on last edited by
                            #37

                            Indeed, many of today's optimization techniques revolve around hitting the caches as much as possible. The amount of cycles lost when performing a main memory read are mind boggling. :omg:

                            1 Reply Last reply
                            0
                            • R Rob Philpott

                              There seems to be a general view that if you want to do some serious compute bound work C++ will always win over C# because of its unmanagedness. This may be the case, but then there's an alternative argument that goes if you sidestep garbage collection, and use unsafe constructs you get close, and because your code gets JITed, there may be a chance to hone the code for the actual CPU it will run on which could actually make .NET faster over a generalised native binary. There seems to be better scope for this if you use the SIMD enabled System.Numerics. I've been mucking around with sound synthesis in .NET recently, and by using unmanaged memory (Marshal.AllocHGlobal) and unsafe pointers the performance is good enough. In fact I've got it so there are virtually no garbage collections at all (beware of Linq - it creates enumerators all over the place). GCs are terrible news for audio, because a delay can mean a buffer not being ready in time and you get nasty clicks. It means a different approach to coding, but it's still miles preferable to header files and linking libs and all that 32/64 bit nastiness you get with C++. Then I stumbled on this: [https://www.bepuentertainment.com/\](https://www.bepuentertainment.com/) This is a stunning masterclass in what C# can do. I recommend having a look at the video, and reading the bits about GPU vs. CPU and 'a different kind of c#' entries. I'm fairly hard to impress these days, but I've downloaded the code built it and played with it and just... wow. Moreover, I'd argue its the last nail in the coffin of the argument that C# is not a viable choice for high performance compute bound work.

                              Regards, Rob Philpott.

                              Z Offline
                              Z Offline
                              zezba9000
                              wrote on last edited by
                              #38

                              I've been working on a project to get a C# subset with a modified type system as fast as C: GitHub - reignstudios/CS2X: Transpiles C# subset to non .NET languages. (Powered by Roslyn)[^] After I have more working will be using it for game engine and script code.

                              1 Reply Last reply
                              0
                              • OriginalGriffO OriginalGriff

                                No, I don't. And you don't. But ... we've both seen it done, and sometimes in production code. :omg: I think we have to accept that developers are not what they used to be (and "Hoorah!" for that in some cases), projects generally are a lot larger and more complex than they were, and that we have to change to languages which facilitate that.

                                Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

                                K Offline
                                K Offline
                                Kirk 10389821
                                wrote on last edited by
                                #39

                                Oh, I had a programmer returning stack pointers. The third time he claimed it worked fine when he tested it (inside the debugger, duh), I fired him. The fact that he could not comprehend that a stack is temporary on invocation of the function... And that is part of the problem. People understand less and less. Coding used to be understand first, analysis, and development. Now it feels more like "crank out code"... And nobody realizes the how deep the problems are. Then the often hidden fact: Development is only 20% of the budget. Maintenance is 80% over the long haul. So SIMPLE, CLEAN Code that is easier to maintain wins. When you need performance. Do that. Do it right with the right tools, and keep it clean and simple. And if it is unmanaged code, then you must run a memory analyzer/leak finder. Or your code is not properly tested! Having worked on a project with 250K lines of code. They said it had a memory leak. A quick review of the code and I labelled it a Memory Sieve! The design guaranteed a leak! Nothing would be freed as children referenced their parents!

                                1 Reply Last reply
                                0
                                • M Member 14331076

                                  Quote:

                                  "See, it's easy," you begin, but are interrupted by the sound of rapid British-sounding footsteps approaching the doorway.

                                  I have to ask, British footsteps sound different from, say, German footsteps? :confused:

                                  J Offline
                                  J Offline
                                  James Lonero
                                  wrote on last edited by
                                  #40

                                  Or footsteps of a person looking down at their phone?

                                  1 Reply Last reply
                                  0
                                  • N Nand32

                                    The modern C++ is trying to get less cryptic* & the modern C# is trying to get more performant. Someday both meet at a point and people get to choose the project template as easy as picking a green apple or a red apple. :) *a long way to go

                                    J Offline
                                    J Offline
                                    James Lonero
                                    wrote on last edited by
                                    #41

                                    It seems that the modern C# is becoming more cryptic. See the changes being proposed for C#-9

                                    1 Reply Last reply
                                    0
                                    Reply
                                    • Reply as topic
                                    Log in to reply
                                    • Oldest to Newest
                                    • Newest to Oldest
                                    • Most Votes


                                    • Login

                                    • Don't have an account? Register

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • World
                                    • Users
                                    • Groups