Skip to content

Commit

Permalink
tested some fun stuff, sadly ToSpan, while fast is not faster than wo…
Browse files Browse the repository at this point in the history
…rking with pointers.
  • Loading branch information
LoneWandererProductions committed Jan 5, 2024
1 parent ea00b05 commit f14f6a0
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 1 deletion.
8 changes: 8 additions & 0 deletions CommonLibraryTests/CommonLibraryTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
<Authors>Wayfarer</Authors>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
<None Remove="Images\Base.png" />
<None Remove="Images\Color.png" />
Expand Down
21 changes: 21 additions & 0 deletions CommonLibraryTests/ExtendedSystemObjects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using System.Diagnostics;
using System.Linq;
using ExtendedSystemObjects;
using Mathematics;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace CommonLibraryTests
Expand Down Expand Up @@ -501,6 +502,26 @@ public void MultiArray()
normal[0, 0] = 0;
Assert.AreEqual(copy[0, 0], 1, "00");
Assert.AreEqual(copy[0, 1], 2, "01");

//see Matrix Multiplication, the speed is sadly slower, but it converts arrays to span in a fast way and the results are the same

var x = new double[,] { { 2, 1, 1 }, { 1, 2, 1 }, { 1, 1, 2 } };
var m1 = new BaseMatrix { Matrix = x };

var y = new double[,] { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 } };
var m2 = new BaseMatrix { Matrix = y };

var compare1 = MathSpeedTests.TestOne(m1, m2);

Assert.AreEqual(compare1[0, 0], 2, "00");
Assert.AreEqual(compare1[1, 0], 1, "10");
Assert.AreEqual(compare1[2, 0], 1, "20");
Assert.AreEqual(compare1[0, 1], 1, "01");
Assert.AreEqual(compare1[1, 1], 2, "11");
Assert.AreEqual(compare1[2, 1], 1, "21");
Assert.AreEqual(compare1[0, 2], 1, "02");
Assert.AreEqual(compare1[1, 2], 1, "12");
Assert.AreEqual(compare1[2, 2], 2, "22");
}

/// <summary>
Expand Down
46 changes: 46 additions & 0 deletions CommonLibraryTests/MathSpeedTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using ExtendedSystemObjects;
using Mathematics;

namespace CommonLibraryTests
{
internal static class MathSpeedTests
{
/// <summary>
/// Tests one. Matrix Multiplication
/// </summary>
/// <param name="mOne">The m one.</param>
/// <param name="mTwo">The m two.</param>
/// <returns></returns>
internal static BaseMatrix TestOne(BaseMatrix mOne, BaseMatrix mTwo)
{
var h = mOne.Height;
var w = mTwo.Width;
var l = mOne.Width;
var result = new BaseMatrix(h, w);
var spanOne = mOne.Matrix.ToSpan();
var spanTwo = mTwo.Matrix.ToSpan();
var spanResult = result.Matrix.ToSpan();

for (var i = 0; i < h; i++)
{
var iOne = i * l;

for (var j = 0; j < w; j++)
{
var iTwo = j;

double res = 0;

for (var k = 0; k < l; k++, iTwo += w)
{
res += spanOne[iOne + k] * spanTwo[iTwo];
}

spanResult[(i * w) + j] = res;
}
}

return result;
}
}
}
36 changes: 36 additions & 0 deletions CommonLibraryTests/Mathematics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,22 @@ public void MatrixMultiplications()

result = MatrixUtility.UnsafeMultiplication(m1, m2);

var timer = new Stopwatch();
timer.Start();

var compare1 = MathSpeedTests.TestOne(m1, m2);

timer.Stop();
Trace.WriteLine(string.Concat("Test one (span Version): ", timer.Elapsed));

timer = new Stopwatch();
timer.Start();

var compare2 = MatrixUtility.UnsafeMultiplication(m1, m2);

timer.Stop();
Trace.WriteLine(string.Concat("Test two (unsafe multiplication): ", timer.Elapsed));

Assert.AreEqual(result[0, 0], 2, "00");
Assert.AreEqual(result[1, 0], 1, "10");
Assert.AreEqual(result[2, 0], 1, "20");
Expand All @@ -173,6 +189,26 @@ public void MatrixMultiplications()
Assert.AreEqual(result[1, 2], 1, "12");
Assert.AreEqual(result[2, 2], 2, "22");

Assert.AreEqual(compare1[0, 0], 2, "00");
Assert.AreEqual(compare1[1, 0], 1, "10");
Assert.AreEqual(compare1[2, 0], 1, "20");
Assert.AreEqual(compare1[0, 1], 1, "01");
Assert.AreEqual(compare1[1, 1], 2, "11");
Assert.AreEqual(compare1[2, 1], 1, "21");
Assert.AreEqual(compare1[0, 2], 1, "02");
Assert.AreEqual(compare1[1, 2], 1, "12");
Assert.AreEqual(compare1[2, 2], 2, "22");

Assert.AreEqual(compare2[0, 0], 2, "00");
Assert.AreEqual(compare2[1, 0], 1, "10");
Assert.AreEqual(compare2[2, 0], 1, "20");
Assert.AreEqual(compare2[0, 1], 1, "01");
Assert.AreEqual(compare2[1, 1], 2, "11");
Assert.AreEqual(compare2[2, 1], 1, "21");
Assert.AreEqual(compare2[0, 2], 1, "02");
Assert.AreEqual(compare2[1, 2], 1, "12");
Assert.AreEqual(compare2[2, 2], 2, "22");

x = new double[,] { { 0, -1, 0 }, { 1, 0, 0 }, { 0, 0, 1 } };
m1 = new BaseMatrix { Matrix = x };
y = new double[,] { { 1 }, { 0 }, { 0 } };
Expand Down
2 changes: 1 addition & 1 deletion Imaging/DirectBitmap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public sealed class DirectBitmap : IDisposable
/// <value>
/// The bits.
/// </value>
private readonly int[] _bits;
private readonly int[]_bits;

/// <summary>
/// Initializes a new instance of the <see cref="DirectBitmap" /> class.
Expand Down

0 comments on commit f14f6a0

Please sign in to comment.