1

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?

11
  • 3
    Did you build it in release mode? (dotnet build -c Release) - although in reality, anything that lasts only a few hundred milliseconds: probably hard to be conclusive about Commented Nov 4, 2021 at 18:01
  • 4
    "Performance" isn't really something you can run once and fully trust the results. This also very much depends on how you compiled each app, how you run them, what versions of tools you're doing, and much more. Plus, you'd really want to analyse your claim on a lot of computers to get an average. Commented Nov 4, 2021 at 18:04
  • Fair bet the C# JIT compiler spends quite a bit of resources optimizing things, too. Commented Nov 5, 2021 at 0:31
  • @gunr2171 thanks for correcting me. I've made changed to the nodejs execution command. Commented Nov 5, 2021 at 2:44
  • 1
    If you wrote that loop in C, an -O3 build is likely to optimise the loop away completely. Comparing the performance of trivial code is hard. Commented Nov 5, 2021 at 3:25

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

I tried this code in C#:

 DateTime dt = DateTime.Now; for (int i = 0; i <= 1000000; i++) { } Console.WriteLine("Start: " + dt.ToLongTimeString()); DateTime dt2 = DateTime.Now; Console.WriteLine("End: " + dt2.ToLongTimeString()); Console.WriteLine(dt2 - dt); Console.ReadLine(); 

and this in Javascript:

 var dt = Date.now(); for(i=0;i<=1000000;i++){ } var dt2 = Date.now(); console.log(`Time: ${(dt2-dt)/1000}`); 

the results were 0.011 for C# and 0.003 for Javascript but when I changed 1000000 to 1000000000 the results were 0.9 for C# and 2.38 for JavaScript >>> It's strange.

1 Comment

Interesting !!! Seems like running the benchmark within the application gives you a better measurement
0

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.

1 Comment

I actually looped through a larger number and nodejs seemed to be faster most times.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.