The Computer Language
24.11 Benchmarks Game

fannkuch-redux C# aot #8 program

source code

/* The Computer Language Benchmarks Game
   https://salsa.debian.org/benchmarksgame-team/benchmarksgame/

   Naive transliteration from Rex Kerr's Scala program
   contributed by Isaac Gouy 
*/

class FannkuchRedux {
   public static int fannkuch(int n) {
      int i, k, r;    
      var perm1 = new int[n];   
      for (i = 0; i < n; i++) perm1[i] = i;      
      var perm = new int[n];
      var count = new int[n];       
      int f = 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) {          
               int 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 more = true;
         while (more) {   
            if (r == n) {
               Console.WriteLine(checksum);               
               return flips; 
            }
            int p0 = perm1[0];
            i = 0;
            while (i < r) {
               int j = i + 1;
               perm1[i] = perm1[j];
               i = j;            
            }
            perm1[r] = p0; 
         
            count[r] -= 1;         
            if (count[r] > 0) more = false; else r += 1;  
         }
         nperm += 1;
      }
      return flips;      
   }
   
   static void Main(String[] args){      
      int n = (args.Length > 0) 
         ? Int32.Parse(args[0]) 
         : 7;
      
      Console.Write("Pfannkuchen({0}) = {1}\n", n, fannkuch(n));      
   }
}
    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
.NET SDK 9.0.100
Host Version: 9.0.0
Commit: 9d5a6a9aa4

<OutputType>Exe
<TargetFramework>net9.0
<ImplicitUsings>enable
<Nullable>enable
<AllowUnsafeBlocks>true
<ServerGarbageCollection>true
<ConcurrentGarbageCollection>true
<PublishAot>true
<OptimizationPreference>Speed
<IlcInstructionSet>native


 Thu, 14 Nov 2024 00:11:44 GMT

MAKE:
cp fannkuchredux.csharpaot-8.csharpaot Program.cs
cp Include/csharpaot/program.csproj .
mkdir obj
cp Include/csharpaot/project.assets.json ./obj
/opt/src/dotnet-sdk-9.0.100/dotnet publish
  Determining projects to restore...
  Restored /home/dunham/all-benchmarksgame/benchmarksgame_i53330/fannkuchredux/tmp/program.csproj (in 958 ms).
  program -> /home/dunham/all-benchmarksgame/benchmarksgame_i53330/fannkuchredux/tmp/bin/Release/net9.0/linux-x64/program.dll
  Generating native code
  program -> /home/dunham/all-benchmarksgame/benchmarksgame_i53330/fannkuchredux/tmp/bin/Release/net9.0/linux-x64/publish/

24.66s to complete and log all make actions

COMMAND LINE:
 ./bin/Release/net9.0/linux-x64/native/program 12

PROGRAM OUTPUT:
3968050
Pfannkuchen(12) = 65