I tried executing this loop for c# version 10
long num = 0;
for (long i = 0; i < 100000000; i )
{
num = num i;
}
Console.WriteLine(num);
And after building the console app(newapp) using the dotnet CLI tool (dotnet build) I measured the time it would take to execute the command by executing the exe compiled code of my console app using the code below:
Measure-Command { dotnet ./newapp.exe}
I got on the average this output
Days : 0
Hours : 0
Minutes : 0
Seconds : 0
Milliseconds : 391
Ticks : 3914079
TotalDays : 4.53018402777778E-06
TotalHours : 0.000108724416666667
TotalMinutes : 0.006523465
TotalSeconds : 0.3914079
TotalMilliseconds : 391.4079
And when I wrote this for nodejs:
var num=0;
for(let i = 0; i<100000000; i ){
num=num i;
}
console.log(num);
And I executed this:
Measure-Command { node c.js}
I got on average the output below:
Days : 0
Hours : 0
Minutes : 0
Seconds : 0
Milliseconds : 346
Ticks : 3462230
TotalDays : 4.00721064814815E-06
TotalHours : 9.61730555555555E-05
TotalMinutes : 0.00577038333333333
TotalSeconds : 0.346223
TotalMilliseconds : 346.223
So my question is why is it that nodejs execute faster on my machine. I thought the compiled c# should execute faster than nodejs. Or am I doing something wrong?
CodePudding user response:
Most of the time is spent in the overhead of launching the process and printing, not the actual loop itself. You need to measure only the time of the loop run.
Also, JIT makes the first run a little slower, so you should probably benchmark the C# code with BenchmarkDotNet
. I'm not sure what's the best alternative for nodejs though.
CodePudding user response:
This is too simplistic of a speed test. A more complicated numerical processing algorithm would give more meaningful results.
It's literally compiling down to 25 machine instructions and that's too small of a sample to give a good comparison.