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. General Programming
  3. .NET (Core and Framework)
  4. Why .NET CLR so slow compared to JVM or Dart or V8 ? Call for help

Why .NET CLR so slow compared to JVM or Dart or V8 ? Call for help

Scheduled Pinned Locked Moved .NET (Core and Framework)
csharpjavajavascriptquestionc++
29 Posts 6 Posters 6 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.
  • A Antonino Porcino

    I'm struggling to understand the cause of the poor performances of the .NET virtual machine when compared to Java (JVM), DartVM or even JavaScript (V8) in this Raytracer demo. In brief: I've written a ray tracer program in various languages: C#, Dart, TypeScript and Java, that I compile both for native virtual machines (CLR, JVM, DartVM) and browsers (thanks to JavaScript compilation). Now when comparing them all, C# running natively is astonishingly slow. What Java calculates in 5 seconds, .NET takes 40 seconds, that is 8 times slower! 3x slower than Dart or JavaScript. Not only, what is really embarrassing is that C# compiled to JavaScript and running in Chrome is 2.5x faster than native C#! How is it that possible? I tried all possible compile switches but nothing seem to be able to cut the execution times. Do you have some possible explanation? Is .NET really that slow? If you like, please have a look at the Github repo and run the tests independently. There must be a flaw somewhere that I can't see. Please help me find it out.

    D Offline
    D Offline
    Dave Kreskowiak
    wrote on last edited by
    #2

    Without analyzing your code (I don't have the time right now) it's impossible to say. One possible explanation is that your implementation is generic and does not play to the strengths of C# and the .NET Framework. It may be that your implementation is the one that is inefficient. One way to test this would be to compare your ray tracing implementation against the implementations written by others in C#.

    A guide to posting questions on CodeProject

    Click this: Asking questions is a skill. Seriously, do it.
    Dave Kreskowiak

    A 1 Reply Last reply
    0
    • D Dave Kreskowiak

      Without analyzing your code (I don't have the time right now) it's impossible to say. One possible explanation is that your implementation is generic and does not play to the strengths of C# and the .NET Framework. It may be that your implementation is the one that is inefficient. One way to test this would be to compare your ray tracing implementation against the implementations written by others in C#.

      A guide to posting questions on CodeProject

      Click this: Asking questions is a skill. Seriously, do it.
      Dave Kreskowiak

      A Offline
      A Offline
      Antonino Porcino
      wrote on last edited by
      #3

      I avoided to use optimizations, the basic idea was to keep it generic so to make comparison between languages more meaningful. Also, I can't think of any specific optimization that applies only to C#--the code is rather plain-vanilla, classes, lists and floating point math.

      D 1 Reply Last reply
      0
      • A Antonino Porcino

        I avoided to use optimizations, the basic idea was to keep it generic so to make comparison between languages more meaningful. Also, I can't think of any specific optimization that applies only to C#--the code is rather plain-vanilla, classes, lists and floating point math.

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #4

        There is really no way to make a "meaningful" comparison of speed between any language runtimes. You will always run into situations and implementations that will favor one runtime over another. Make some changes to the code and the favor can easily fall the other way. Really, what do you do with that data? I know of no one who picks C# or JavaScript for a project because of some "benchmark". You write code that plays the strengths of what you're using.

        A guide to posting questions on CodeProject

        Click this: Asking questions is a skill. Seriously, do it.
        Dave Kreskowiak

        1 Reply Last reply
        0
        • A Antonino Porcino

          I'm struggling to understand the cause of the poor performances of the .NET virtual machine when compared to Java (JVM), DartVM or even JavaScript (V8) in this Raytracer demo. In brief: I've written a ray tracer program in various languages: C#, Dart, TypeScript and Java, that I compile both for native virtual machines (CLR, JVM, DartVM) and browsers (thanks to JavaScript compilation). Now when comparing them all, C# running natively is astonishingly slow. What Java calculates in 5 seconds, .NET takes 40 seconds, that is 8 times slower! 3x slower than Dart or JavaScript. Not only, what is really embarrassing is that C# compiled to JavaScript and running in Chrome is 2.5x faster than native C#! How is it that possible? I tried all possible compile switches but nothing seem to be able to cut the execution times. Do you have some possible explanation? Is .NET really that slow? If you like, please have a look at the Github repo and run the tests independently. There must be a flaw somewhere that I can't see. Please help me find it out.

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

          Antonino Porcino wrote:

          Is .NET really that slow?

          No, it's not. You might want to take the environment into consideration; V8 has optimized graphics running on the video-card. If you want to do the same from .NET, you'd have to use CUDA, DirectX or XNA. You're also comparing something that's compiled to IL to a scripted language (JS) :)

          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

          A 1 Reply Last reply
          0
          • L Lost User

            Antonino Porcino wrote:

            Is .NET really that slow?

            No, it's not. You might want to take the environment into consideration; V8 has optimized graphics running on the video-card. If you want to do the same from .NET, you'd have to use CUDA, DirectX or XNA. You're also comparing something that's compiled to IL to a scripted language (JS) :)

            Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

            A Offline
            A Offline
            Antonino Porcino
            wrote on last edited by
            #6

            Eddy Vluggen wrote:

            You might want to take the environment into consideration; V8 has optimized graphics running on the video-card.

            the use of graphic is minimal and doesn't justify the difference in performance. It's a 640x480 pixel, its drawing time is trascurable.

            Eddy Vluggen wrote:

            You're also comparing something that's compiled to IL to a scripted language (JS)

            exactly, what did you expect to run faster?

            L 1 Reply Last reply
            0
            • A Antonino Porcino

              I'm struggling to understand the cause of the poor performances of the .NET virtual machine when compared to Java (JVM), DartVM or even JavaScript (V8) in this Raytracer demo. In brief: I've written a ray tracer program in various languages: C#, Dart, TypeScript and Java, that I compile both for native virtual machines (CLR, JVM, DartVM) and browsers (thanks to JavaScript compilation). Now when comparing them all, C# running natively is astonishingly slow. What Java calculates in 5 seconds, .NET takes 40 seconds, that is 8 times slower! 3x slower than Dart or JavaScript. Not only, what is really embarrassing is that C# compiled to JavaScript and running in Chrome is 2.5x faster than native C#! How is it that possible? I tried all possible compile switches but nothing seem to be able to cut the execution times. Do you have some possible explanation? Is .NET really that slow? If you like, please have a look at the Github repo and run the tests independently. There must be a flaw somewhere that I can't see. Please help me find it out.

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

              A quick look at your SimpleRaytracer.cs file shows an unnecessarily large number of class instantiations. You can start off by reducing the number of allocations you're performing. Then howabout firing some of those calculations off onto parallel threads? In other words, play to the strengths.

              1 Reply Last reply
              0
              • A Antonino Porcino

                Eddy Vluggen wrote:

                You might want to take the environment into consideration; V8 has optimized graphics running on the video-card.

                the use of graphic is minimal and doesn't justify the difference in performance. It's a 640x480 pixel, its drawing time is trascurable.

                Eddy Vluggen wrote:

                You're also comparing something that's compiled to IL to a scripted language (JS)

                exactly, what did you expect to run faster?

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

                Antonino Porcino wrote:

                the use of graphic is minimal

                Enough to make quite a difference.

                Antonino Porcino wrote:

                exactly, what did you expect to run faster?

                The JavaScript of course, but only in the V8 engine. It is running in a limited and optimized environment, and should be faster due to the optimizations[^]. You also won't be loading much native assemblies there, as they are already in the environment. Memory will already be reserved by the host. As for the interpreter - it's a wrong assumption. The IL is run by the Virtual Machine (an interpreter). JavaScript in V8 is translated to machine code.

                Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                A 1 Reply Last reply
                0
                • L Lost User

                  Antonino Porcino wrote:

                  the use of graphic is minimal

                  Enough to make quite a difference.

                  Antonino Porcino wrote:

                  exactly, what did you expect to run faster?

                  The JavaScript of course, but only in the V8 engine. It is running in a limited and optimized environment, and should be faster due to the optimizations[^]. You also won't be loading much native assemblies there, as they are already in the environment. Memory will already be reserved by the host. As for the interpreter - it's a wrong assumption. The IL is run by the Virtual Machine (an interpreter). JavaScript in V8 is translated to machine code.

                  Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                  A Offline
                  A Offline
                  Antonino Porcino
                  wrote on last edited by
                  #9

                  Eddy Vluggen wrote:

                  Enough to make quite a difference.

                  By commenting out the RenderPixel() call like this

                  //Color c = RenderPixel(x, y);
                  Color c = new Color(255,23,75,193);

                  execution time is only 2 seconds, meaning that most of the time (38 secs) is spent in calculating, not in graphics. So graphic is only 5%.

                  L 1 Reply Last reply
                  0
                  • A Antonino Porcino

                    Eddy Vluggen wrote:

                    Enough to make quite a difference.

                    By commenting out the RenderPixel() call like this

                    //Color c = RenderPixel(x, y);
                    Color c = new Color(255,23,75,193);

                    execution time is only 2 seconds, meaning that most of the time (38 secs) is spent in calculating, not in graphics. So graphic is only 5%.

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

                    You're not measuring a calculation there, but the time it requires to reserve memory and create a color :) 1+1, that's a calculation.

                    Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                    A 1 Reply Last reply
                    0
                    • L Lost User

                      You're not measuring a calculation there, but the time it requires to reserve memory and create a color :) 1+1, that's a calculation.

                      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                      A Offline
                      A Offline
                      Antonino Porcino
                      wrote on last edited by
                      #11

                      Sorry if I was not clear, what I meant to show was how much it takes to do "graphics only", since you said that drawing a 640x480 is "Enough to make quite a difference". So I commented out the raytracing calculation, keeping only the code to display pixels on screen. The result is 2 seconds meaning that graphics impact only on the 5% of the total time (40secs) in that program.

                      L 1 Reply Last reply
                      0
                      • A Antonino Porcino

                        Sorry if I was not clear, what I meant to show was how much it takes to do "graphics only", since you said that drawing a 640x480 is "Enough to make quite a difference". So I commented out the raytracing calculation, keeping only the code to display pixels on screen. The result is 2 seconds meaning that graphics impact only on the 5% of the total time (40secs) in that program.

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

                        Antonino Porcino wrote:

                        Sorry if I was not clear, what I meant to show was how much it takes to do "graphics only", since you said that drawing a 640x480 is "Enough to make quite a difference".

                        It does make a difference; feeding positions to a OpenGL/DirectX bound surface is rather quick. Standard bitmap-functions aren't meant for real-time graphics. Using the graphics-card makes a huge difference.

                        Antonino Porcino wrote:

                        So I commented out the raytracing calculation, keeping only the code to display pixels on screen.

                        Yes, but you are still comparing two different things, in a rather crude way :)

                        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                        1 Reply Last reply
                        0
                        • A Antonino Porcino

                          I'm struggling to understand the cause of the poor performances of the .NET virtual machine when compared to Java (JVM), DartVM or even JavaScript (V8) in this Raytracer demo. In brief: I've written a ray tracer program in various languages: C#, Dart, TypeScript and Java, that I compile both for native virtual machines (CLR, JVM, DartVM) and browsers (thanks to JavaScript compilation). Now when comparing them all, C# running natively is astonishingly slow. What Java calculates in 5 seconds, .NET takes 40 seconds, that is 8 times slower! 3x slower than Dart or JavaScript. Not only, what is really embarrassing is that C# compiled to JavaScript and running in Chrome is 2.5x faster than native C#! How is it that possible? I tried all possible compile switches but nothing seem to be able to cut the execution times. Do you have some possible explanation? Is .NET really that slow? If you like, please have a look at the Github repo and run the tests independently. There must be a flaw somewhere that I can't see. Please help me find it out.

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

                          One question. Have you tested the V8 code on a none hardware accelerated browser? The Canvas element uses the GPU to draw things when the browser is hardware accelerated. As your C# code is targetting bitmaps directly (something that it's not really designed to do by default), the code is not taking advantage of the GPU.

                          A 1 Reply Last reply
                          0
                          • P Pete OHanlon

                            One question. Have you tested the V8 code on a none hardware accelerated browser? The Canvas element uses the GPU to draw things when the browser is hardware accelerated. As your C# code is targetting bitmaps directly (something that it's not really designed to do by default), the code is not taking advantage of the GPU.

                            A Offline
                            A Offline
                            Antonino Porcino
                            wrote on last edited by
                            #14

                            yes, but the total time spent in working with the bitmap is only 5% of the total time, so it's not graphics that is causing the bottleneck (see this other reply). I've analyzed the code, and 65% of the time is spent in clr.dll when Vector3f.operator-() operator overload is called. Don't know what that means or how to avoid it though.

                            L 1 Reply Last reply
                            0
                            • A Antonino Porcino

                              yes, but the total time spent in working with the bitmap is only 5% of the total time, so it's not graphics that is causing the bottleneck (see this other reply). I've analyzed the code, and 65% of the time is spent in clr.dll when Vector3f.operator-() operator overload is called. Don't know what that means or how to avoid it though.

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

                              Is that a XNA vector class? http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.vector3.aspx[^]

                              Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                              A 1 Reply Last reply
                              0
                              • L Lost User

                                Is that a XNA vector class? http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.vector3.aspx[^]

                                Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                                A Offline
                                A Offline
                                Antonino Porcino
                                wrote on last edited by
                                #16

                                no, it's just a user-defined class:

                                public class /\*struct\*/ Vector3f {
                                    public number x, y, z;
                                
                                    public Vector3f(number x = 0, number y = 0, number z = 0) {
                                        this.x = x;
                                        this.y = y;
                                        this.z = z;
                                    }
                                
                                    public static Vector3f operator -(Vector3f a, Vector3f b) {
                                        return new Vector3f(a.x - b.x, a.y - b.y, a.z - b.z);
                                    }
                                
                                L 1 Reply Last reply
                                0
                                • A Antonino Porcino

                                  I'm struggling to understand the cause of the poor performances of the .NET virtual machine when compared to Java (JVM), DartVM or even JavaScript (V8) in this Raytracer demo. In brief: I've written a ray tracer program in various languages: C#, Dart, TypeScript and Java, that I compile both for native virtual machines (CLR, JVM, DartVM) and browsers (thanks to JavaScript compilation). Now when comparing them all, C# running natively is astonishingly slow. What Java calculates in 5 seconds, .NET takes 40 seconds, that is 8 times slower! 3x slower than Dart or JavaScript. Not only, what is really embarrassing is that C# compiled to JavaScript and running in Chrome is 2.5x faster than native C#! How is it that possible? I tried all possible compile switches but nothing seem to be able to cut the execution times. Do you have some possible explanation? Is .NET really that slow? If you like, please have a look at the Github repo and run the tests independently. There must be a flaw somewhere that I can't see. Please help me find it out.

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

                                  Interesting. Did you get to the bottom of this? .NET is fast, so something is amiss. I don't know about web stuff like V8, but this looks like code that runs on the CPU rather than the GPU. Pete might be onto something with the number of object instantiations, but these are stupidly fast. It's the GC that comes later that slows things down. I notice you swapped the class for a struct - did that make any difference?

                                  Regards, Rob Philpott.

                                  A 1 Reply Last reply
                                  0
                                  • A Antonino Porcino

                                    I'm struggling to understand the cause of the poor performances of the .NET virtual machine when compared to Java (JVM), DartVM or even JavaScript (V8) in this Raytracer demo. In brief: I've written a ray tracer program in various languages: C#, Dart, TypeScript and Java, that I compile both for native virtual machines (CLR, JVM, DartVM) and browsers (thanks to JavaScript compilation). Now when comparing them all, C# running natively is astonishingly slow. What Java calculates in 5 seconds, .NET takes 40 seconds, that is 8 times slower! 3x slower than Dart or JavaScript. Not only, what is really embarrassing is that C# compiled to JavaScript and running in Chrome is 2.5x faster than native C#! How is it that possible? I tried all possible compile switches but nothing seem to be able to cut the execution times. Do you have some possible explanation? Is .NET really that slow? If you like, please have a look at the Github repo and run the tests independently. There must be a flaw somewhere that I can't see. Please help me find it out.

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

                                    I just downloaded your code and ran the C# version. Running the Debug version under Visual Studio took 54 seconds. Running the Release version under Visual Studio took 43 seconds. Running the Release version from the command line took 14 seconds. Part of the problem here is that you are running everything on the UI thread, so there is nothing at all being offloaded to the GPU.

                                    A 1 Reply Last reply
                                    0
                                    • A Antonino Porcino

                                      no, it's just a user-defined class:

                                      public class /\*struct\*/ Vector3f {
                                          public number x, y, z;
                                      
                                          public Vector3f(number x = 0, number y = 0, number z = 0) {
                                              this.x = x;
                                              this.y = y;
                                              this.z = z;
                                          }
                                      
                                          public static Vector3f operator -(Vector3f a, Vector3f b) {
                                              return new Vector3f(a.x - b.x, a.y - b.y, a.z - b.z);
                                          }
                                      
                                      L Offline
                                      L Offline
                                      Lost User
                                      wrote on last edited by
                                      #19

                                      return new Vector3f(a.x - b.x, a.y - b.y, a.z - b.z);

                                      Fetches the field value for x, after fetching object a. Fetches object b, fetches the value of the x field of that instance, and subtracts it from the other value. Does that three times. Create a new vector, and returns that. Calculations would benefit if they were local variables. Furthermore I'm fairly certain that the "number" datatype does not exist in C#; we call that a "double".

                                      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                                      A 1 Reply Last reply
                                      0
                                      • R Rob Philpott

                                        Interesting. Did you get to the bottom of this? .NET is fast, so something is amiss. I don't know about web stuff like V8, but this looks like code that runs on the CPU rather than the GPU. Pete might be onto something with the number of object instantiations, but these are stupidly fast. It's the GC that comes later that slows things down. I notice you swapped the class for a struct - did that make any difference?

                                        Regards, Rob Philpott.

                                        A Offline
                                        A Offline
                                        Antonino Porcino
                                        wrote on last edited by
                                        #20

                                        yes there was huge increase (~50%) but still far from Java performances. And also it's not very fair to use it for the benchmark, as the other VM does not have values types (ok, I see the argument of using the best of each language).

                                        P 1 Reply Last reply
                                        0
                                        • L Lost User

                                          return new Vector3f(a.x - b.x, a.y - b.y, a.z - b.z);

                                          Fetches the field value for x, after fetching object a. Fetches object b, fetches the value of the x field of that instance, and subtracts it from the other value. Does that three times. Create a new vector, and returns that. Calculations would benefit if they were local variables. Furthermore I'm fairly certain that the "number" datatype does not exist in C#; we call that a "double".

                                          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                                          A Offline
                                          A Offline
                                          Antonino Porcino
                                          wrote on last edited by
                                          #21

                                          Eddy Vluggen wrote:

                                          Furthermore I'm fairly certain that the "number" datatype does not exist in C#; we call that a "double".

                                          there's an using number = System.double so to make easy to switch from float to double.

                                          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