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.

    OriginalGriffO Offline
    OriginalGriffO Offline
    OriginalGriff
    wrote on last edited by
    #2

    (I haven't followed the link yet - it's Saturday, and I'm feeling lazy.) It's probably rather like the old C / Assembler debate. Yes, a skilled assembler programmer can produce faster, more compact code than a skilled C programmer purely because he can tell the machine exactly what he wants it to do rather than adding a layer of "interpretation" via a compiler. But ... it'll take a lot longer to code, and an unskilled assembler programmer can still make a serious dogs dinner of the same job! 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. Don't get me wrong, C++ is a good language, I used it for many years - but C# produces good code as well which is often a lot more readable and less prone to silly and avoidable bugs.

    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!

    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

    M R L D 4 Replies 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.

      M Offline
      M Offline
      Munchies_Matt
      wrote on last edited by
      #3

      How do you find the Audio performance on Windows though, do you notice the lag through the kernel? (For example to run recording studio type stuff you need to bin it all and put in ASIO)

      R 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        (I haven't followed the link yet - it's Saturday, and I'm feeling lazy.) It's probably rather like the old C / Assembler debate. Yes, a skilled assembler programmer can produce faster, more compact code than a skilled C programmer purely because he can tell the machine exactly what he wants it to do rather than adding a layer of "interpretation" via a compiler. But ... it'll take a lot longer to code, and an unskilled assembler programmer can still make a serious dogs dinner of the same job! 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. Don't get me wrong, C++ is a good language, I used it for many years - but C# produces good code as well which is often a lot more readable and less prone to silly and avoidable bugs.

        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!

        M Offline
        M Offline
        Munchies_Matt
        wrote on last edited by
        #4

        I like the simple readability of structured C :)

        OriginalGriffO 1 Reply Last reply
        0
        • M Munchies_Matt

          How do you find the Audio performance on Windows though, do you notice the lag through the kernel? (For example to run recording studio type stuff you need to bin it all and put in ASIO)

          R Offline
          R Offline
          Rob Philpott
          wrote on last edited by
          #5

          No not really. I think Microsoft likes to occasionally upload your entire browsing history so it can 'improve your windows experience' and if that were to happen the same moment as you're filling your buffers you may come unstuck. Ultimately, we're all slaves to the thread scheduler. But, that's just windows. I've mucked about with thread and process priorities, but it doesn't seem to make much difference. In terms of the driver, I am using ASIO. And so far it's all single threaded too.

          Regards, Rob Philpott.

          M 1 Reply Last reply
          0
          • M Munchies_Matt

            I like the simple readability of structured C :)

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #6

            Yes, and I wrote a lot of embedded C, often with assembler for the time critical bits. But you have to pay attention, and it's far too easy to create nasty bugs if you don't know what you are doing:

            int* GetMemory(int n)
            {
            int arr[n];
            return arr;
            }

            Things get nasty real quick with that ... :laugh: Even if you do it right:

            int* GetMemory(int n)
            {
            int* arr = (int*) malloc(n * sizeof(int));
            return arr;
            }

            It's still far to easy to leave a memory leak because the caller is responsible for freeing the memory and unless he looks at the function he may not be aware of 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!

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            R M 2 Replies Last reply
            0
            • OriginalGriffO OriginalGriff

              (I haven't followed the link yet - it's Saturday, and I'm feeling lazy.) It's probably rather like the old C / Assembler debate. Yes, a skilled assembler programmer can produce faster, more compact code than a skilled C programmer purely because he can tell the machine exactly what he wants it to do rather than adding a layer of "interpretation" via a compiler. But ... it'll take a lot longer to code, and an unskilled assembler programmer can still make a serious dogs dinner of the same job! 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. Don't get me wrong, C++ is a good language, I used it for many years - but C# produces good code as well which is often a lot more readable and less prone to silly and avoidable bugs.

              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!

              R Offline
              R Offline
              Rob Philpott
              wrote on last edited by
              #7

              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 1 Reply Last reply
              0
              • OriginalGriffO OriginalGriff

                Yes, and I wrote a lot of embedded C, often with assembler for the time critical bits. But you have to pay attention, and it's far too easy to create nasty bugs if you don't know what you are doing:

                int* GetMemory(int n)
                {
                int arr[n];
                return arr;
                }

                Things get nasty real quick with that ... :laugh: Even if you do it right:

                int* GetMemory(int n)
                {
                int* arr = (int*) malloc(n * sizeof(int));
                return arr;
                }

                It's still far to easy to leave a memory leak because the caller is responsible for freeing the memory and unless he looks at the function he may not be aware of 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!

                R Offline
                R Offline
                Rob Philpott
                wrote on last edited by
                #8

                This is the trouble. Twenty years of living in the .NET nanny state with the parental controls maxed out means you forget (well I have) the myriad of ways you can kill yourself in the unmanaged world. I'm not going out there!

                Regards, Rob Philpott.

                1 Reply Last reply
                0
                • R Rob Philpott

                  No not really. I think Microsoft likes to occasionally upload your entire browsing history so it can 'improve your windows experience' and if that were to happen the same moment as you're filling your buffers you may come unstuck. Ultimately, we're all slaves to the thread scheduler. But, that's just windows. I've mucked about with thread and process priorities, but it doesn't seem to make much difference. In terms of the driver, I am using ASIO. And so far it's all single threaded too.

                  Regards, Rob Philpott.

                  M Offline
                  M Offline
                  Munchies_Matt
                  wrote on last edited by
                  #9

                  Rob Philpott wrote:

                  I am using ASIO

                  Good, you have fixed 99% of your problems right there. :) I did some audio work, it was a service that had to open a pin on two sound cards and send the data between them, so it used the KS API. It was OK, ish, but much better when I put it in the kernel (same KS API, so pretty easy to move it over). (Since it used KS it did the same thing as ASIO, it kicks the windows audio mixer off the hardware and give you pretty much real time IO)

                  1 Reply Last reply
                  0
                  • OriginalGriffO OriginalGriff

                    Yes, and I wrote a lot of embedded C, often with assembler for the time critical bits. But you have to pay attention, and it's far too easy to create nasty bugs if you don't know what you are doing:

                    int* GetMemory(int n)
                    {
                    int arr[n];
                    return arr;
                    }

                    Things get nasty real quick with that ... :laugh: Even if you do it right:

                    int* GetMemory(int n)
                    {
                    int* arr = (int*) malloc(n * sizeof(int));
                    return arr;
                    }

                    It's still far to easy to leave a memory leak because the caller is responsible for freeing the memory and unless he looks at the function he may not be aware of 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!

                    M Offline
                    M Offline
                    Munchies_Matt
                    wrote on last edited by
                    #10

                    Returning a stack variable? Tut tut! Re that and memory leaks though, you just dont write code that bad. And that is it really, you write 100% tight, very simple code (in fact the simpler it is the easier it is to get 100% tight).

                    OriginalGriffO 1 Reply Last reply
                    0
                    • M Munchies_Matt

                      Returning a stack variable? Tut tut! Re that and memory leaks though, you just dont write code that bad. And that is it really, you write 100% tight, very simple code (in fact the simpler it is the easier it is to get 100% tight).

                      OriginalGriffO Offline
                      OriginalGriffO Offline
                      OriginalGriff
                      wrote on last edited by
                      #11

                      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!

                      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                      J K 2 Replies 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.

                        M Offline
                        M Offline
                        Member 14331076
                        wrote on last edited by
                        #12

                        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:

                        L J 2 Replies 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:

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #13

                          British footsteps sound like an old man shuffling along a rough track. German footsteps sound like an army goosestepping along a wide autobahn (to the sound of an oompah band). :laugh:

                          OriginalGriffO 1 Reply Last reply
                          0
                          • L Lost User

                            British footsteps sound like an old man shuffling along a rough track. German footsteps sound like an army goosestepping along a wide autobahn (to the sound of an oompah band). :laugh:

                            OriginalGriffO Offline
                            OriginalGriffO Offline
                            OriginalGriff
                            wrote on last edited by
                            #14

                            And French footsteps just get quieter and quieter... :laugh:

                            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!

                            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                            R 1 Reply Last reply
                            0
                            • OriginalGriffO OriginalGriff

                              (I haven't followed the link yet - it's Saturday, and I'm feeling lazy.) It's probably rather like the old C / Assembler debate. Yes, a skilled assembler programmer can produce faster, more compact code than a skilled C programmer purely because he can tell the machine exactly what he wants it to do rather than adding a layer of "interpretation" via a compiler. But ... it'll take a lot longer to code, and an unskilled assembler programmer can still make a serious dogs dinner of the same job! 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. Don't get me wrong, C++ is a good language, I used it for many years - but C# produces good code as well which is often a lot more readable and less prone to silly and avoidable bugs.

                              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!

                              L Offline
                              L Offline
                              Lost User
                              wrote on last edited by
                              #15

                              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 1 Reply Last reply
                              0
                              • OriginalGriffO OriginalGriff

                                (I haven't followed the link yet - it's Saturday, and I'm feeling lazy.) It's probably rather like the old C / Assembler debate. Yes, a skilled assembler programmer can produce faster, more compact code than a skilled C programmer purely because he can tell the machine exactly what he wants it to do rather than adding a layer of "interpretation" via a compiler. But ... it'll take a lot longer to code, and an unskilled assembler programmer can still make a serious dogs dinner of the same job! 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. Don't get me wrong, C++ is a good language, I used it for many years - but C# produces good code as well which is often a lot more readable and less prone to silly and avoidable bugs.

                                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!

                                D Offline
                                D Offline
                                dandy72
                                wrote on last edited by
                                #16

                                Agreed wholeheartedly. If performance can be improved just by throwing more hardware at it, it probably makes more sense to have an average developer write good, maintainable code than having a rock star developer write code that only *he* has the ability to read and maintain, even if that code manages to squeeze every little bit of performance out of the hardware. Simply because developer time is a lot more expensive than hardware.

                                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.

                                  G Offline
                                  G Offline
                                  GuyThiebaut
                                  wrote on last edited by
                                  #17

                                  I used unmanaged methods with C# to do some image processing. For fun I was writing something to detect hand-drawn rectangles and circles - so that you can turn a hand-drawn diagram into vectors. I found that the unmanaged methods took around three times less time than using managed code - so it does make a bit of a difference.

                                  “That which can be asserted without evidence, can be dismissed without evidence.”

                                  ― Christopher Hitchens

                                  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.

                                    L Offline
                                    L Offline
                                    Lost User
                                    wrote on last edited by
                                    #18

                                    System.Numerics is better than nothing (I recommend it over "doing nothing" at least), but it's really difficult. By which I mean more difficult than using SIMD intrinsics in C++. The API is full of holes (no right shift?? no shuffle? no special operations?) and landmines (you'd think that eg multiplying a vector by a scalar is equivalent to broadcasting that scalar and then doing a vector multiply, but no, you have to do that manually and ban the `scalar*vec` from your code). Of course programming is an exercise in "poking the code until the asm looks good" either way, but the System.Numerics API makes it harder to get it right. Sometimes impossible. The newer SIMD API in .NET Core 3 (System.Runtime.Intrinsics) is more complete and probably will be better. Still tricky to get good codegen in all cases. For example it's not so easy to force it to "broadcast from memory" and avoid the "load-then-broadcast" anti-pattern (this costs a shuffle µop on p5 in addition to the load, broadcast from memory only costs a load).

                                    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.

                                      P Offline
                                      P Offline
                                      Pete OHanlon
                                      wrote on last edited by
                                      #19

                                      If you like that, have a look at what you can do with Span.

                                      This space for rent

                                      1 Reply Last reply
                                      0
                                      • OriginalGriffO OriginalGriff

                                        And French footsteps just get quieter and quieter... :laugh:

                                        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!

                                        R Offline
                                        R Offline
                                        RickZeeland
                                        wrote on last edited by
                                        #20

                                        And the Dutch, the Dutch with their woody clutter !

                                        M 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.

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

                                          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

                                          R J 2 Replies 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