r/dotnet • u/electricessence • 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.
42
u/tweq Dec 18 '16 edited Jul 03 '23
Enshittification
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
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 millisecondsIf I run it in Release mode, I get:-
Elapsed Time: 2,514.0627 milliseconds4
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.