Reputation: 8552
Stopwatch t1 = new Stopwatch();
t1.Start();
int i = 0;
for (int j = 0; j < 2000000001; j++)
i += 1;
t1.Stop();
Stopwatch t2 = new Stopwatch();
t2.Start();
int k = 0;
for (int j = 0; j < 2000000001; j++)
{
k += 1;
}
t2.Stop();
Console.WriteLine(t1.Elapsed);
Console.WriteLine(t2.Elapsed);
Console.ReadLine();
O/P:
00:00:05.0266990
00:00:04.7179756
performance depends on variable name also?
Upvotes: 0
Views: 172
Reputation: 223307
No, omitting curly braces will not have any effect on the performance. The difference you are seeing is probably due to program warming up in the start. Swap those two code block and you will see the same difference or none.
If you build your program in release mode and open the executable in ILSpy (ILSpy version 2.1.0.1603) then you will see that curly braces has been added. :
private static void Main(string[] args)
{
Stopwatch t = new Stopwatch();
t.Start();
int i = 0;
for (int j = 0; j < 2000000001; j++)
{
i++;
}
t.Stop();
Stopwatch t2 = new Stopwatch();
t2.Start();
int k = 0;
for (int l = 0; l < 2000000001; l++)
{
k++;
}
t2.Stop();
Console.WriteLine(t.Elapsed);
Console.WriteLine(t2.Elapsed);
Console.ReadLine();
}
Original code:
Stopwatch t1 = new Stopwatch();
t1.Start();
int i = 0;
for (int j = 0; j < 2000000001; j++)
i += 1;
t1.Stop();
Stopwatch t2 = new Stopwatch();
t2.Start();
int k = 0;
for (int j = 0; j < 2000000001; j++)
{
k += 1;
}
t2.Stop();
Upvotes: 7