title: Why does the same algorithm work in Scala much slower than in C#? And how to make it faster?
body: <p>The algorithm creates all possible variants of the sequence from variants for each member of the sequence.</p>
  
  <p>C# code :</p>
  
  <pre><code>static void Main(string[] args)
  {
    var arg = new List&lt;List&lt;int&gt;&gt;();
    int i = 0;
    for (int j = 0; j &lt; 5; j++)
    {
      arg.Add(new List&lt;int&gt;());
      for (int j1 = i; j1 &lt; i + 3; j1++)
      {
        //if (j1 != 5)
        arg[j].Add(j1);
      }
      i += 3;
    }
    List&lt;Utils&lt;int&gt;.Variant&lt;int&gt;&gt; b2 = new List&lt;Utils&lt;int&gt;.Variant&lt;int&gt;&gt;();
    //int[][] bN;
  
    var s = System.Diagnostics.Stopwatch.StartNew();
    //for(int j = 0; j &lt; 10;j++)
      b2 = Utils&lt;int&gt;.Produce2(arg);
    s.Stop();
    Console.WriteLine(s.ElapsedMilliseconds);
  
  }
  
  
  public class Variant&lt;T&gt;
  {
    public T element;
    public Variant&lt;T&gt; previous;
  }
  
  
  public static List&lt;Variant&lt;T&gt;&gt; Produce2(List&lt;List&lt;T&gt;&gt; input)
  {
    var ret = new List&lt;Variant&lt;T&gt;&gt;();
    foreach (var form in input)
    {
      var newRet = new List&lt;Variant&lt;T&gt;&gt;(ret.Count * form.Count);
      foreach (var el in form)
      {
        if (ret.Count == 0)
        {
          newRet.Add(new Variant&lt;T&gt;{ element = el, previous = null });
        }
        else
        {
          foreach (var variant in ret)
          {
            var buf = new Variant&lt;T&gt; { previous = variant, element = el };
            newRet.Add(buf);
          }
        }
      }
      ret = newRet;
    }
    return ret;
  }
  </code></pre>
  
  <p>Scala code : </p>
  
  <pre><code>object test {
  def main() {
   var arg = new Array[Array[Int]](5)
   var i = 0
   var init = 0
   while (i&lt;5)
   {
    var buf = new Array[Int](3)
    var j = 0
    while (j&lt;3)
    {
     buf(j) = init
     init = init+1
     j = j + 1
    }
    arg(i)=buf
    i = i + 1
   }
   println("Hello, world!")
   val start = System.currentTimeMillis
   var res = Produce(arg)
   val stop = System.currentTimeMillis
   println(stop-start)
   /*for(list &lt;- res)
    {
     for(el &lt;- list)
      print(el+" ")
     println
    }*/
   println(res.length)
  }
  
   def Produce[T](input:Array[Array[T]]):Array[Variant[T]]=
    {
     var ret = new Array[Variant[T]](1)
     for(val forms &lt;- input)
     {
      if(forms!=null)
      {
       var newRet = new Array[Variant[T]](forms.length*ret.length)
       if(ret.length&gt;0)
       {
        for(val prev &lt;-ret)
         if(prev!=null)
         for(val el &lt;-forms)
         {
          newRet = newRet:+new Variant[T](el,prev)
         }
       }
       else
       {
        for(val el &lt;- forms)
          {
           newRet = newRet:+new Variant[T](el,null)
          }
       }
       ret = newRet
      }
     }
     return ret
    }
  
  
  }
  
  class Variant[T](var element:T, previous:Variant[T])
  {
  }
  </code></pre>