The Computer Language
Benchmarks Game

mandelbrot C# .NET Core #6 program

source code

/* The Computer Language Benchmarks Game
   http://benchmarksgame.alioth.debian.org/
    
   started with Java #2 program (Krause/Whipkey/Bennet/AhnTran/Enotus/Stalcup)
   adapted for C# by Jan de Vaan
   simplified and optimised to use TPL by Anthony Lloyd
   simplified to compute Cib alongside Crb by Tanner Gooding
*/

using System;
using System.Threading.Tasks;
using System.Runtime.CompilerServices;
using System.Globalization;

public class MandelBrot
{
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    static byte getByte(double[] Crb, double Ciby, int x, int y)
    {
        int res = 0;

        for (int i = 0; i < 8; i += 2)
        {
            double Crbx0 = Crb[x + i], Crbx1 = Crb[x + i + 1];

            double Zr1 = Crbx0, Zr2 = Crbx1;
            double Zi1 = Ciby, Zi2 = Ciby;

            int b = 0, j = 49;

            do
            {
                double nZr1 = Zr1 * Zr1 - Zi1 * Zi1 + Crbx0;
                Zi1 = Zr1 * Zi1 + Zr1 * Zi1 + Ciby;
                Zr1 = nZr1;

                double nZr2 = Zr2 * Zr2 - Zi2 * Zi2 + Crbx1;
                Zi2 = Zr2 * Zi2 + Zr2 * Zi2 + Ciby;
                Zr2 = nZr2;

                if (Zr1 * Zr1 + Zi1 * Zi1 > 4) { b |= 2; if (b == 3) break; }
                if (Zr2 * Zr2 + Zi2 * Zi2 > 4) { b |= 1; if (b == 3) break; }
            } while (--j > 0);

            res = (res << 2) + b;
        }

        return (byte)(res ^ -1);
    }

    public static void Main(String[] args)
    {
        var n = args.Length > 0 ? int.Parse(args[0], CultureInfo.CurrentCulture) : 16000;

        var Crb = new double[n + 7];
        var Cib = new double[n + 7];

        var invN = 2.0 / n;
        
        for (int i = 0; i < n; i++)
        {
            var tmp = i * invN;

            Crb[i] = tmp - 1.5;
            Cib[i] = tmp - 1.0;
        }

        int lineLen = (n - 1) / 8 + 1;
        var data = new byte[n * lineLen];

        Parallel.For(0, n, y =>
        {
            var offset = y * lineLen;
            for (int x = 0; x < lineLen; x++)
            {
                data[offset + x] = getByte(Crb, Cib[y], x * 8, y);
            }
        });

        Console.Out.WriteLine("P4\n{0} {0}", n);
        Console.OpenStandardOutput().Write(data, 0, data.Length);
    }
}
    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
2.0.2 a04b4bf512
"System.GC.Server": true


Fri, 27 Oct 2017 00:36:14 GMT

MAKE:
cp mandelbrot.csharpcore-6.csharpcore Program.cs
cp Include/csharpcore/tmp.csproj .
cp Include/csharpcore/runtimeconfig.template.json .
mkdir obj
cp Include/csharpcore/tmp.csproj.nuget.g.props ./obj
cp Include/csharpcore/tmp.csproj.nuget.g.targets ./obj
/usr/bin/dotnet build -c Release
Microsoft (R) Build Engine version 15.4.8.50001 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

  tmp -> /home/dunham/benchmarksgame_quadcore/mandelbrot/tmp/bin/Release/netcoreapp2.0/tmp.dll

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:03.53

6.10s to complete and log all make actions

COMMAND LINE:
/usr/bin/dotnet ./bin/Release/netcoreapp2.0/tmp.dll 16000

(BINARY) PROGRAM OUTPUT NOT SHOWN