r/dotnet • • Dec 18 '16

NodeJS outperforming .NET Core?

So I've been working on this Genetic Algorithm Platform, and I originally wrote it in C#, then I ported to TypeScript, then I thought, after all the advancements I made in TypeScript why not port back to .NET and try out the new shiny .NET Core.

But then I was getting frustrated with interface insanity that TypeScript simply doesn't suffer from (recursive generics are nice!).

And so I thought, just so I wasn't barking up the wrong tree I'd do a quick perf test:


C#/.NET Core

using System;
using System.Diagnostics;

namespace GeneticAlgorithmPlatform
{
    public class Program
    {
        public static void Main(string[] args)
        {

            var sw = new Stopwatch();
            sw.Start();

            var n = 0;
            for (var j = 0; j < 10; j++)
            {
                for (var i = 0; i < 1000000000; i++)
                {
                    n += i;
                }
            }

            sw.Stop();

            Console.WriteLine("Elapsed Time: " + sw.ElapsedMilliseconds);
        }
    }
}

.NET Core Result: 19,035 milliseconds.


TypeScript/NodeJS

import Stopwatch from "typescript-dotnet-umd/System/Diagnostics/Stopwatch";
console.log("Elapsed Time:",Stopwatch.measure(()=>{
	let n = 0;
	for (let j = 0; j < 10; j++)
	{
		for (let i = 0; i < 1000000000; i++)
		{
			n += i;
		}
	}
}).total.milliseconds);
// Latest: 10M loops per second.

NodeJS Result: 9,918 milliseconds


What is going on here?

I was certain .NET Core would leave NodeJS in the dust.

0 Upvotes

15 comments sorted by

42

u/tweq Dec 18 '16 edited Jul 03 '23

1

u/electricessence Dec 19 '16

Is there a flag for Optimizations? For .NET Core, how do I enable this?

2

u/tweq Dec 19 '16 edited Dec 19 '16

There is an optimize flag you can set in the project.json, but it is automatically included in the default Release configuration.

20

u/robchartier Dec 18 '16

Basically the same setup, just made sure to run with "-c Release" flag...

dotnetcore

using System;

namespace ConsoleApplication { public class Program { public static void Main(string[] args) { var sw = new System.Diagnostics.Stopwatch(); sw.Start(); var n = 0; for(var j = 0;j<10;j++) { for(var i = 0;i<1000000000;i++) { n+=i; } } sw.Stop(); System.Console.WriteLine("Elapsed:" + sw.ElapsedMilliseconds); } } }

nodejs

var sw = (new Date()).getTime() var n = 0; for(var j = 0;j<10;j++) { for(var i = 0;i<1000000000;i++) { n+=i; } } var delta = (new Date()).getTime() - sw; console.log("Elapsed:" + delta);

results...

dotnet run -c Release Project Desktop (.NETCoreApp,Version=v1.0) was previously compiled. Skipping compilation. Elapsed:3713

node Loop.js Elapsed:13763

1

u/electricessence Dec 19 '16

dotnet run -c Release

OK!!! Now this makes sense. My results were similar (proportional) with this.
Thank you.

105

u/SikhGamer Dec 18 '16

You are running the NET Core in Debug mode. If I take your example and run it in Debug mode I get:-

Elapsed Time: 19,185.8573 milliseconds

If I run it in Release mode, I get:-

Elapsed Time: 2,514.0627 milliseconds

14

u/SuperImaginativeName Dec 18 '16 edited Dec 18 '16

I wonder if OP already knows that, and was hoping no one would realise. It's possible he's a shill just trying to spread FUD.

Edit: Downvote me all you fucking like, it's a fact that for several years Java fanboys were spreading FUD about .NET when it was first realised, and now ironically all the things they claimed Microsoft would do if you used .NET, Oracle are now doing to people who use Java.

6

u/Evairfairy Dec 18 '16

It's far more likely that he either forgot to switch to Release or just didn't realise there was a performance hit from running in Debug mode.

0

u/[deleted] Dec 18 '16

[deleted]

2

u/joshman211 Dec 18 '16

It tells you when you are debugging it. IIRC its one of the first things it says.

1

u/bitcrazed Dec 19 '16

Have you filled a bug to discuss this idea?

1

u/SuperImaginativeName Dec 18 '16

I guess so, but then again "dotnet" and .NET Core are wildly messed up in terms of tooling. I don't think anyone knew it would be so fiddly just to get it working when they announced .NET Core.

1

u/[deleted] Dec 20 '16

NodeJS is JavaScript, not Java...

4

u/UtahJazz777 Dec 18 '16

If you want to test it properly, use benchmarks like https://github.com/dotnet/BenchmarkDotNet Even compiling in release and running it a few times isn't good enough to see the real difference.

5

u/[deleted] Dec 18 '16

A single iteration with overall measurement doesn't really tell you anything.

Measure each of the inner loops, present the min/max/median/mean/sum.

And as others have said, make sure you are running the code in Release mode, and without a debugger attached.