In: Computer Science
In C#. Explain the technique of fusing loop.Give example.
Loop fusion is done inorder to reduce loop overhead and improve run time complexity. It is a programming technique where two or more loops are combined to one loop.
Example in C# Language :
Not Fusing Loop :
using System;
class FUSE{
  static void Main() 
  {
      int a=0,b=0;
      for(int i=0;i<10;i++)
      {
          a=a+i;
      }
      for(int j=0;j<10;j++)
      {
          b=b-j;
      }
      Console.WriteLine("value of a : "+a);
      Console.WriteLine("value of b : "+b);
  }
}
Here we can see, we have two for loops
Fusing Loop :
using System;
class FUSE{
  static void Main() 
  {
      int a=0,b=0;
      for(int i=0;i<10;i++)
      {
          a=a+i;
          b=b-i;
      }
      Console.WriteLine("value of a : "+a);
      Console.WriteLine("value of b : "+b);
  }
}
Here we can see, we have one loop. The two loops in the above example are merged.
Output for both the codes:
