Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if statement performance in c# [closed]

I'm just trying to determine the impact of each "if" statement on the performance of my C# application when it is used in cycles with large number of iterations. I have not found the topic about this so I have created this one.

For the test I made 2 cycles: one without "if" and one with a single "if" statement. The code is the following.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace IfPerformance
{
    class Program
    {
        static void Main(string[] args)
        {
            int N = 500000000;
            Stopwatch sw = new Stopwatch();

            double a = 0, b = 0;
            bool f;

            sw.Restart();
            for (int i = 0; i < N; i++)
            {
                a += 1.1;
                f = a < N;
            }
            sw.Stop();
            Console.WriteLine("Without if: " + sw.ElapsedMilliseconds + " ms");

            a = 0;
            sw.Restart();
            for (int i = 0; i < N; i++)
            {
                if (a < N)
                    a += 1.1;
                else
                    b += 1.1;
            }
            sw.Stop();
            Console.WriteLine("With if:    " + sw.ElapsedMilliseconds + " ms");
            Console.ReadKey();
        }
    }
}

I ran the test with "Optimize code" build option and "Start without debugging". The result is the following: Without if: 154 ms With if: 742 ms

This means that a single "if" statement brings almost 5 times slowdown to the performance. I think regarding this will be helpful.

Also, I have noticed that the presence of several extra "if"s in a large loop may slow down my final application by 25%, which on my opinion is significant.

To be specific, I'm running Monte-Carlo optimization on a set of data, which require many loops through the whole data set. The loop contains branching which depends on the user settings. From this point "if"s arise.

My questions to the professionals in performance aspects are:

  1. What is the impact of extra "if"s in a loop on the time of running many iterations?
  2. How to avoid the slowdown?

Please post your opinion if I'm going in the wrong direction.

like image 529
kudral Avatar asked Nov 29 '22 12:11

kudral


2 Answers

It doesn't matter ... You're testing 500 MILLION iterations ... and it takes less than a second ... IN THE WORST case ...

As comments said, you'll be in a hell of a trouble to begin with, since you won't be running in debug for testing performance, and even then, you'll have heaps of other things to take into consideration (it's a whole big world about performance testing, and it's not as simple as it seems usually).

Now, do notice that you're doing two different things in the two places. If you would like to see the performance of the if, you should have them do basically the same. I'm sure the branching changes the IL code to begin with ...

Last, but not least, as I said again ... it DOESTN'T MATTER ... unless you really need to run 500 MILLION times, and have this in so many places that your program starts to slow down because of that.

Go for readability over obsessing if you can save some micro seconds on an if statement

Feel free to read these articles by Eric Lippert (who has "only" 250K rep and i̶s̶ was a principal developer on the C# compiler team :) who'll get you on the right direction:

  • c# performance benchmarks mistakes part 1
  • c# performance benchmarks mistakes part 2
  • c# performance benchmarks mistakes part 3
  • c# performance benchmarks mistakes part 4

(Talking about this, I would guess that garbage collection (article 4) might have been something to consider ...)

Then look at: this elaborate answer about the topic

And last, but not least, have a look at Writing Faster Managed Code: Know What Things Cost. This is by Jan Gray, from the Microsoft CLR Performance Team. I'll be honest and say I didn't read this one yet :). I Will though, later on...

It goes on an on ... :)

like image 166
Noctis Avatar answered Dec 06 '22 04:12

Noctis


These code sample are two different codes one is a boolean assignment and the other one is condition statement so this is not suitable method to evaluate performance

like image 44
saeed Avatar answered Dec 06 '22 03:12

saeed