Skip to content

Commit 9a8fa07

Browse files
committed
Add benchmarks
1 parent 2056d8e commit 9a8fa07

File tree

6 files changed

+166
-92
lines changed

6 files changed

+166
-92
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<ItemGroup>
4+
<ProjectReference Include="..\FixedMath.NET\FixedMath.NET.csproj" />
5+
</ItemGroup>
6+
7+
<ItemGroup>
8+
<PackageReference Include="BenchmarkDotNet" Version="0.13.0" />
9+
</ItemGroup>
10+
11+
<PropertyGroup>
12+
<OutputType>Exe</OutputType>
13+
<TargetFramework>net5.0</TargetFramework>
14+
</PropertyGroup>
15+
16+
</Project>

src/FixedMath.NET.Bench/Program.cs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System;
2+
using BenchmarkDotNet.Attributes;
3+
using BenchmarkDotNet.Running;
4+
5+
namespace FixedMath.NET.Bench
6+
{
7+
public class Trigonometry
8+
{
9+
private const int N = 10000;
10+
private readonly Fix64[] sinAngles;
11+
private readonly Fix64[] atanAngles;
12+
13+
public Trigonometry()
14+
{
15+
sinAngles = new Fix64[N];
16+
for (int i = 0; i < N; i++)
17+
{
18+
var angle = ((2 * Math.PI) / N) * i;
19+
sinAngles[i] = (Fix64)angle;
20+
}
21+
22+
atanAngles = new Fix64[N];
23+
for (int i = 0; i < N; i++)
24+
{
25+
var angle = -1.0 + (2.0 / N) * i;
26+
atanAngles[i] = (Fix64)angle;
27+
}
28+
}
29+
30+
[Benchmark]
31+
public void Sin()
32+
{
33+
foreach (var angle in sinAngles)
34+
{
35+
var actualF = Fix64.Sin(angle);
36+
}
37+
}
38+
39+
[Benchmark]
40+
public void Atan()
41+
{
42+
foreach (var angle in atanAngles)
43+
{
44+
var actualF = Fix64.Atan(angle);
45+
}
46+
}
47+
}
48+
49+
class Program
50+
{
51+
static void Main(string[] args)
52+
{
53+
var summary = BenchmarkRunner.Run<Trigonometry>();
54+
}
55+
}
56+
}

src/FixedMath.NET.Common/Fix64Constants.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
namespace FixedMath.NET
1+
using System;
2+
3+
namespace FixedMath
24
{
3-
public class Constants
5+
public struct Fix64Constants
46
{
57
public const long MAX_VALUE = long.MaxValue;
68
public const long MIN_VALUE = long.MinValue;

src/FixedMath.NET.Generators/Fix64LutGenerator.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,24 @@ public class LutGenerator : ISourceGenerator
1313
{
1414
private void BuildSinLut(GeneratorExecutionContext context)
1515
{
16-
var sourceBuilder = new StringBuilder(@"namespace FixedMath.NET
16+
var sourceBuilder = new StringBuilder(@"namespace FixedMath
1717
{
1818
partial struct Fix64
1919
{
2020
public static readonly long[] SinLut = new[]
2121
{");
2222

2323
int lineCounter = 0;
24-
for (int i = 0; i < Constants.LUT_SIZE; ++i)
24+
for (int i = 0; i < Fix64Constants.LUT_SIZE; ++i)
2525
{
26-
var angle = i * Math.PI * 0.5 / (Constants.LUT_SIZE - 1);
26+
var angle = i * Math.PI * 0.5 / (Fix64Constants.LUT_SIZE - 1);
2727
if (lineCounter++ % 8 == 0)
2828
{
2929
sourceBuilder.AppendLine();
3030
sourceBuilder.Append(" ");
3131
}
3232
var sin = Math.Sin(angle);
33-
var rawValue = (long)(sin * Constants.ONE);
33+
var rawValue = (long)(sin * Fix64Constants.ONE);
3434
sourceBuilder.Append(string.Format("0x{0:X}L, ", rawValue));
3535
}
3636
sourceBuilder.Append(
@@ -45,28 +45,28 @@ partial struct Fix64
4545

4646
private void BuildTanLut(GeneratorExecutionContext context)
4747
{
48-
var sourceBuilder = new StringBuilder(@"namespace FixedMath.NET
48+
var sourceBuilder = new StringBuilder(@"namespace FixedMath
4949
{
5050
partial struct Fix64
5151
{
5252
public static readonly long[] TanLut = new[]
5353
{");
5454

5555
int lineCounter = 0;
56-
for (int i = 0; i < Constants.LUT_SIZE; ++i)
56+
for (int i = 0; i < Fix64Constants.LUT_SIZE; ++i)
5757
{
58-
var angle = i * Math.PI * 0.5 / (Constants.LUT_SIZE - 1);
58+
var angle = i * Math.PI * 0.5 / (Fix64Constants.LUT_SIZE - 1);
5959
if (lineCounter++ % 8 == 0)
6060
{
6161
sourceBuilder.AppendLine();
6262
sourceBuilder.Append(" ");
6363
}
6464
var tan = Math.Tan(angle);
65-
if (tan > (double)Constants.MAX_VALUE || tan < 0.0)
65+
if (tan > (double)Fix64Constants.MAX_VALUE || tan < 0.0)
6666
{
67-
tan = (double)Constants.MAX_VALUE;
67+
tan = (double)Fix64Constants.MAX_VALUE;
6868
}
69-
var rawValue = (((decimal)tan > (decimal)Constants.MAX_VALUE || tan < 0.0) ? Constants.MAX_VALUE : (long)(tan * Constants.ONE));
69+
var rawValue = (((decimal)tan > (decimal)Fix64Constants.MAX_VALUE || tan < 0.0) ? Fix64Constants.MAX_VALUE : (long)(tan * Fix64Constants.ONE));
7070
sourceBuilder.Append(string.Format("0x{0:X}L, ", rawValue));
7171
}
7272
sourceBuilder.Append(

0 commit comments

Comments
 (0)