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?
dotnet build -c Release) - although in reality, anything that lasts only a few hundred milliseconds: probably hard to be conclusive about-O3build is likely to optimise the loop away completely. Comparing the performance of trivial code is hard.