performance measurements

Each table row shows performance measurements for this C# Mono program with a particular command-line input value N.

 N  CPU secs Elapsed secs Memory KB Code B ≈ CPU Load
100.740.7520,7401096  0% 1% 1% 100%
116.816.8122,8001096  1% 1% 1% 100%
1287.7087.7420,9761096  1% 1% 0% 100%

Read the ↓ make, command line, and program output logs to see how this program was run.

Read fannkuch-redux benchmark to see what this program should do.

 notes

Mono JIT compiler version 3.8.1 (master/db3eb16 Wed Aug 13 12:36:50 PDT 2014)
LLVM: yes(3.4svn-mono-mono/e656cac)
GC: sgen

 fannkuch-redux C# Mono #3 program source code

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

   contributed by Isaac Gouy, transliterated from Oleg Mazurov's Java program
*/

using System;
using System.Threading;


class FannkuchRedux
{
   private static int NCHUNKS = 150;
   private static int CHUNKSZ;
   private static int NTASKS;
   private static int n;
   private static int[] Fact;
   private static int[] maxFlips;
   private static int[] chkSums;
   private static int taskId;

   int[] p, pp, count;


   void FirstPermutation( int idx )
   {
       for ( int i=0; i<p.Length; ++i ) {
          p[i] = i;
       }

       for ( int i=count.Length-1; i>0; --i ) {
           int d = idx / Fact[i];
           count[i] = d;
           idx = idx % Fact[i];

           Array.Copy( p, 0, pp, 0, i+1 );
           for ( int j=0; j<=i; ++j ) {
               p[j] = j+d <= i ? pp[j+d] : pp[j+d-i-1];
           }
       }
   }


   bool NextPermutation()
   {
      int first = p[1];
      p[1] = p[0];
      p[0] = first;

      int i=1;
      while ( ++count[i] > i ) {
         count[i++] = 0;
         int next = p[0] = p[1];
         for ( int j=1; j<i; ++j ) {
            p[j] = p[j+1];
         }
         p[i] = first;
         first = next;
      }
      return true;
   }


   int CountFlips()
   {
      int flips = 1;
      int first = p[0];
      if ( p[first] != 0 ) {
         Array.Copy( p, 0, pp, 0, pp.Length );
         do {
            ++flips;
            for ( int lo = 1, hi = first - 1; lo < hi; ++lo, --hi ) {
               int t = pp[lo];
               pp[lo] = pp[hi];
               pp[hi] = t;
            }
            int tp = pp[first];
            pp[first] = first;
            first = tp;
         } while ( pp[first] != 0 );
      }
      return flips;
   }


   void RunTask( int task )
   {
      int idxMin = task*CHUNKSZ;
      int idxMax = Math.Min( Fact[n], idxMin+CHUNKSZ );

      FirstPermutation( idxMin );

      int maxflips = 1;
      int chksum = 0;
      for ( int i=idxMin;; ) {

         if ( p[0] != 0 ) {
            int flips = CountFlips();
            maxflips = Math.Max( maxflips, flips );
            chksum += i%2 ==0 ? flips : -flips;
         }

         if ( ++i == idxMax ) {
	    break;
	 }

         NextPermutation();
      }
      maxFlips[task] = maxflips;
      chkSums[task]  = chksum;
   }


   public void Run()
   {
      p     = new int[n];
      pp    = new int[n];
      count = new int[n];

      int task;
      while ( (task = taskId++) < NTASKS ) { // NOT SAFE - need PFX
	 RunTask( task );       
      } 
   }


   static void Main(string[] args)
   {
      n = 7;
      if (args.Length > 0) n = Int32.Parse(args[0]);

      Fact = new int[n+1];
      Fact[0] = 1;
      for ( int i=1; i<Fact.Length; ++i ) {
         Fact[i] = Fact[i-1] * i;
      }

      CHUNKSZ = (Fact[n] + NCHUNKS - 1) / NCHUNKS;
      NTASKS = (Fact[n] + CHUNKSZ - 1) / CHUNKSZ;
      maxFlips = new int[NTASKS];
      chkSums  = new int[NTASKS];
      taskId = 0;

      int nthreads = Environment.ProcessorCount;
      Thread[] threads = new Thread[nthreads];
      for ( int i=0; i<nthreads; ++i ) {
         threads[i] = new Thread( new ThreadStart(new FannkuchRedux().Run) );
         threads[i].Start();
      }
      foreach ( Thread t in threads ) {
         t.Join();
      }

      int res = 0;
      foreach ( int v in maxFlips ) {
         res = Math.Max( res, v );
      }
      int chk = 0;
      foreach ( int v in chkSums ) {
         chk += v;
      }

      Console.WriteLine("{0}\nPfannkuchen({1}) = {2}", chk, n, res);
   }
}

 make, command-line, and program output logs

Thu, 14 Aug 2014 01:10:26 GMT

MAKE:
mv fannkuchredux.csharp-3.csharp fannkuchredux.csharp-3.cs
/usr/local/bin/mcs -unsafe+ -optimize+ -platform:x64 -out:fannkuchredux.csharp-3.csharp_run fannkuchredux.csharp-3.cs
rm fannkuchredux.csharp-3.cs
0.24s to complete and log all make actions

COMMAND LINE:
/usr/local/bin/mono --llvm fannkuchredux.csharp-3.csharp_run 12

PROGRAM OUTPUT:
3968050
Pfannkuchen(12) = 65

Revised BSD license

  Home   Conclusions   License   Play