The Computer Language
Benchmarks Game

fannkuch-redux C# Mono LLVM program

source code

/* The Computer Language Benchmarks Game
   http://benchmarksgame.alioth.debian.org/

   contributed by Isaac Gouy, transliterated from Rex Kerr's Scala program
*/

using System;

class FannkuchRedux
{
   public static int fannkuch(int n) {
      int[] perm = new int[n], perm1 = new int[n], count = new int[n];
      for(int j=0; j<n; j++) perm1[j] = j;
      int f = 0, i = 0, k = 0, r = 0, flips = 0, nperm = 0, checksum = 0;

      r = n;
      while (r>0) {
         i = 0;
         while (r != 1) { count[r-1] = r; r -= 1; }
         while (i < n) { perm[i] = perm1[i]; i += 1; }
      
         // Count flips and update max  and checksum
         f = 0;
         k = perm[0];
         while (k != 0) {
           i = 0;
           while (2*i < k) {
             var t = perm[i]; perm[i] = perm[k-i]; perm[k-i] = t;
             i += 1;
           }
           k = perm[0];
           f += 1;
         }
         if (f>flips) flips = f;
         if ((nperm&0x1)==0) checksum += f;
         else checksum -= f;
                      
         // Use incremental change to generate another permutation
         var go = true;
         while (go) {
            if (r == n) {
               Console.WriteLine(checksum);
               return flips;
            }
            var p0 = perm1[0];
            i = 0;
            while (i < r) {
               var j = i+1;
               perm1[i] = perm1[j];
               i = j;
            }
            perm1[r] = p0;

            count[r] -= 1;
            if (count[r] > 0) go = false;
            else r += 1;
         }
         nperm += 1;
      }
      return flips;
   }

   static void Main(string[] args){
      int n = 7;
      if (args.Length > 0) n = Int32.Parse(args[0]);
      Console.WriteLine("Pfannkuchen({0}) = {1}", n, fannkuch(n));
   }
}
    

notes, command-line, and program output

NOTES:
32-bit Ubuntu one core
Mono JIT compiler version 4.5.1 (master/3e844dd Fri May  6 19:24:07 PDT 2016)
	LLVM:          yes(3.6.0svn-mono-master/9f79399)
	GC:            sgen



Tue, 28 Jun 2016 18:29:12 GMT

MAKE:
mv fannkuchredux.csharpllvm fannkuchredux.cs
/usr/local/bin/mcs -unsafe+ -optimize+ -platform:x86 -out:fannkuchredux.csharpllvm_run fannkuchredux.cs
rm fannkuchredux.cs
0.25s to complete and log all make actions

COMMAND LINE:
/usr/local/bin/mono --llvm --gc=sgen fannkuchredux.csharpllvm_run 12

PROGRAM OUTPUT:
3968050
Pfannkuchen(12) = 65