C
C#9mo ago
Joseph

❔ Different Runtimes

Hello, my code is
using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using System.Text;
using System.Threading.Tasks;

namespace SoloLearn
{
class Program
{
static void Main(string[] args)
{
Stopwatch watch = new Stopwatch();
watch.Start();
for (int i = 0; i < 1000; i++)
{
Console.WriteLine(i);
}
watch.Stop();
Console.WriteLine($"Execution Time: {watch.ElapsedMilliseconds} ms");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using System.Text;
using System.Threading.Tasks;

namespace SoloLearn
{
class Program
{
static void Main(string[] args)
{
Stopwatch watch = new Stopwatch();
watch.Start();
for (int i = 0; i < 1000; i++)
{
Console.WriteLine(i);
}
watch.Stop();
Console.WriteLine($"Execution Time: {watch.ElapsedMilliseconds} ms");
}
}
}
when i run this code with writing dotnet run i get 130 ms when i run run it f5 which is "start debugging" i get 20 ms and when i run it ctrl + f5 which is "run without debugging" i get 13 ms can i ask why different speeds?
3 Replies
Thinker
Thinker9mo ago
Don't do this, use Benchmark.NET if you need accurate benchmarking/run times
Developful
Developful9mo ago
theres a difference between release and debug due to debugging symbols and stuff like that. Basically, C# adds extra stuff to the compiled code so when you're debugging, its a nice and much more pleasant experience, while release is clean of those. as for dotnet run.. i'm not sure why but from what i've gathered, it has to do with building the project again, i can't find anything else on this though you can notice the difference between release and debugging while trying to evaluate certain expressions some variables might be missing even though they are there, some functions, etc
Accord
Accord9mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.