Skip to content

Commit

Permalink
feat(C#): add Mandelbrot benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
leon0399 committed Jun 30, 2024
1 parent 0ff6584 commit 5a4ddf4
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
1 change: 1 addition & 0 deletions c-sharp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/*.exe
17 changes: 17 additions & 0 deletions c-sharp/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
SCRIPTS = $(wildcard **/*.cs)

BINARIES = $(SCRIPTS:%.cs=%-csc.exe) \
$(SCRIPTS:%.cs=%-mcs.exe)

%-csc.exe: %.cs
mono-csc $< -out:$@

%-mcs.exe: %.cs
mcs $< -out:$@

all: $(BINARIES)

.PHONY: clean

clean:
rm -f $(BINARIES)
13 changes: 13 additions & 0 deletions c-sharp/benchmark.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
title: 'C#'

strategy:
matrix:
command:
- title: 'csc'
command: 'mono %s-csc.exe'

- title: 'mcs'
command: 'mono %s-mcs.exe'

files:
- mandelbrot/Simple
71 changes: 71 additions & 0 deletions c-sharp/mandelbrot/Simple.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System;

class Program
{
static void Main()
{
var startTime = DateTime.Now;

Index();

var endTime = DateTime.Now;
var duration = endTime - startTime;
Console.WriteLine("Execution time: " + duration.TotalMilliseconds + "ms");
}

static void Index()
{
for (int y = -39; y < 39; y++)
{
Console.WriteLine();

for (int x = -39; x < 39; x++)
{
int i = Mandelbrot(x / 40.0, y / 40.0);

if (i == 0)
{
Console.Write("*");
}
else
{
Console.Write(" ");
}
}
}

Console.WriteLine();
}

static int Mandelbrot(double x, double y)
{
double cr = y - 0.5;
double ci = x;
double zi = 0.0;
double zr = 0.0;
int i = 0;

while (true)
{
i++;

double temp = zr * zi;

double zr2 = zr * zr;
double zi2 = zi * zi;

zr = zr2 - zi2 + cr;
zi = temp + temp + ci;

if (zi2 + zr2 > 16)
{
return i;
}

if (i > 5000)
{
return 0;
}
}
}
}

0 comments on commit 5a4ddf4

Please sign in to comment.