Skip to content

Instantly share code, notes, and snippets.

@elden1337
Last active January 8, 2018 09:06
Show Gist options
  • Save elden1337/afe1b28449c10384f91e9faa18e1b184 to your computer and use it in GitHub Desktop.
Save elden1337/afe1b28449c10384f91e9faa18e1b184 to your computer and use it in GitHub Desktop.
C# polygon parallell-merge tester
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using SqlServerTypes;
using Microsoft.SqlServer.Types;
using System.Data.SqlTypes;
using System.Threading;
namespace test_parallell
{
class Program
{
static void Main()
{
SqlServerTypes.Utilities.LoadNativeAssemblies(AppDomain.CurrentDomain.BaseDirectory);
List<SqlGeometry> pGeo = new List<SqlGeometry>();
Stopwatch regularLoop = new Stopwatch();
Stopwatch thread2NoLoop = new Stopwatch();
Stopwatch thread2Loop = new Stopwatch();
Stopwatch thread3NoLoop = new Stopwatch();
Stopwatch thread3Loop = new Stopwatch();
Stopwatch thread4NoLoop = new Stopwatch();
Stopwatch thread4Loop = new Stopwatch();
Stopwatch thread5NoLoop = new Stopwatch();
Stopwatch thread5Loop = new Stopwatch();
Stopwatch thread6NoLoop = new Stopwatch();
Stopwatch thread6Loop = new Stopwatch();
Stopwatch thread7NoLoop = new Stopwatch();
Stopwatch thread7Loop = new Stopwatch();
Stopwatch thread8NoLoop = new Stopwatch();
Stopwatch thread8Loop = new Stopwatch();
SqlGeometry regularAdd = SqlGeometry.STPolyFromText(new SqlChars(new SqlString("POLYGON ((5 5, 10 5, 10 10, 5 5))")), 4326);
SqlGeometry thread2NoAdd = SqlGeometry.STPolyFromText(new SqlChars(new SqlString("POLYGON ((5 5, 10 5, 10 10, 5 5))")), 4326);
SqlGeometry thread2Add = SqlGeometry.STPolyFromText(new SqlChars(new SqlString("POLYGON ((5 5, 10 5, 10 10, 5 5))")), 4326);
SqlGeometry thread3NoAdd = SqlGeometry.STPolyFromText(new SqlChars(new SqlString("POLYGON ((5 5, 10 5, 10 10, 5 5))")), 4326);
SqlGeometry thread3Add = SqlGeometry.STPolyFromText(new SqlChars(new SqlString("POLYGON ((5 5, 10 5, 10 10, 5 5))")), 4326);
SqlGeometry thread4NoAdd = SqlGeometry.STPolyFromText(new SqlChars(new SqlString("POLYGON ((5 5, 10 5, 10 10, 5 5))")), 4326);
SqlGeometry thread4Add = SqlGeometry.STPolyFromText(new SqlChars(new SqlString("POLYGON ((5 5, 10 5, 10 10, 5 5))")), 4326);
SqlGeometry thread5NoAdd = SqlGeometry.STPolyFromText(new SqlChars(new SqlString("POLYGON ((5 5, 10 5, 10 10, 5 5))")), 4326);
SqlGeometry thread5Add = SqlGeometry.STPolyFromText(new SqlChars(new SqlString("POLYGON ((5 5, 10 5, 10 10, 5 5))")), 4326);
SqlGeometry thread6NoAdd = SqlGeometry.STPolyFromText(new SqlChars(new SqlString("POLYGON ((5 5, 10 5, 10 10, 5 5))")), 4326);
SqlGeometry thread6Add = SqlGeometry.STPolyFromText(new SqlChars(new SqlString("POLYGON ((5 5, 10 5, 10 10, 5 5))")), 4326);
SqlGeometry thread7NoAdd = SqlGeometry.STPolyFromText(new SqlChars(new SqlString("POLYGON ((5 5, 10 5, 10 10, 5 5))")), 4326);
SqlGeometry thread7Add = SqlGeometry.STPolyFromText(new SqlChars(new SqlString("POLYGON ((5 5, 10 5, 10 10, 5 5))")), 4326);
SqlGeometry thread8NoAdd = SqlGeometry.STPolyFromText(new SqlChars(new SqlString("POLYGON ((5 5, 10 5, 10 10, 5 5))")), 4326);
SqlGeometry thread8Add = SqlGeometry.STPolyFromText(new SqlChars(new SqlString("POLYGON ((5 5, 10 5, 10 10, 5 5))")), 4326);
Random rnd = new Random();
SqlGeometry k = null;
for (int n = 1; n < 1001; n++)
{
int checker = rnd.Next(-1000, 1000);
int checkerd = checker * 2;
string sqlstring = "POLYGON ((" + checker + " " + checker + ", " + checkerd + " " + checker + ", " + checkerd + " " + checkerd + ", " + checker + " " + checker + "))";
k = SqlGeometry.STPolyFromText(new SqlChars(new SqlString(sqlstring)), 4326).MakeValid();
pGeo.Add(k);
}
//REGULAR FOREACH
regularLoop.Start();
int totalSeen1 = 0;
foreach (var x in pGeo)
{ regularAdd = regularAdd.STUnion(x); totalSeen1++; }
regularLoop.Stop();
//REGULAR FOREACH
//2
thread2Loop.Start();
int totalSeen2 = 0;
Parallel.ForEach(pGeo, new ParallelOptions { MaxDegreeOfParallelism = 2 }, x =>
{ lock (thread2Add) { thread2Add = thread2Add.STUnion(x); totalSeen2++; } });
thread2Loop.Stop();
thread2NoLoop.Start();
int totalSeen2No = 0;
Parallel.ForEach(pGeo, new ParallelOptions { MaxDegreeOfParallelism = 2 }, x =>
{ thread2NoAdd = thread2NoAdd.STUnion(x); totalSeen2No++; });
thread2NoLoop.Stop();
//3
thread3Loop.Start();
int totalSeen3 = 0;
Parallel.ForEach(pGeo, new ParallelOptions { MaxDegreeOfParallelism = 3 }, x =>
{ lock (thread3Add) { thread3Add = thread3Add.STUnion(x); totalSeen3++; } });
thread3Loop.Stop();
thread3NoLoop.Start();
int totalSeen3No = 0;
Parallel.ForEach(pGeo, new ParallelOptions { MaxDegreeOfParallelism = 3 }, x =>
{ thread3NoAdd = thread3NoAdd.STUnion(x); totalSeen3No++; });
thread3NoLoop.Stop();
//4
thread4Loop.Start();
int totalSeen4 = 0;
Parallel.ForEach(pGeo, new ParallelOptions { MaxDegreeOfParallelism = 4 }, x =>
{ lock (thread4Add) { thread4Add = thread4Add.STUnion(x); totalSeen4++; } });
thread4Loop.Stop();
thread4NoLoop.Start();
int totalSeen4No = 0;
Parallel.ForEach(pGeo, new ParallelOptions { MaxDegreeOfParallelism = 4 }, x =>
{ thread4NoAdd = thread4NoAdd.STUnion(x); totalSeen4No++; });
thread4NoLoop.Stop();
//5
thread5Loop.Start();
int totalSeen5 = 0;
Parallel.ForEach(pGeo, new ParallelOptions { MaxDegreeOfParallelism = 5 }, x =>
{ lock (thread5Add) { thread5Add = thread5Add.STUnion(x); totalSeen5++; } });
thread5Loop.Stop();
thread5NoLoop.Start();
int totalSeen5No = 0;
Parallel.ForEach(pGeo, new ParallelOptions { MaxDegreeOfParallelism = 5 }, x =>
{ thread5NoAdd = thread5NoAdd.STUnion(x); totalSeen5No++; });
thread5NoLoop.Stop();
//6
thread6Loop.Start();
int totalSeen6 = 0;
Parallel.ForEach(pGeo, new ParallelOptions { MaxDegreeOfParallelism = 6 }, x =>
{ lock (thread6Add) { thread6Add = thread6Add.STUnion(x); totalSeen6++; } });
thread6Loop.Stop();
thread6NoLoop.Start();
int totalSeen6No = 0;
Parallel.ForEach(pGeo, new ParallelOptions { MaxDegreeOfParallelism = 6 }, x =>
{ thread6NoAdd = thread6NoAdd.STUnion(x); totalSeen6No++; });
thread6NoLoop.Stop();
//7
thread7Loop.Start();
int totalSeen7 = 0;
Parallel.ForEach(pGeo, new ParallelOptions { MaxDegreeOfParallelism = 7 }, x =>
{ lock (thread7Add) { thread7Add = thread7Add.STUnion(x); totalSeen7++; } });
thread7Loop.Stop();
thread7NoLoop.Start();
int totalSeen7No = 0;
Parallel.ForEach(pGeo, new ParallelOptions { MaxDegreeOfParallelism = 7 }, x =>
{ thread7NoAdd = thread7NoAdd.STUnion(x); totalSeen7No++; });
thread7NoLoop.Stop();
//8
thread8Loop.Start();
int totalSeen8 = 0;
Parallel.ForEach(pGeo, new ParallelOptions { MaxDegreeOfParallelism = 8 }, x =>
{ lock (thread8Add) { thread8Add = thread8Add.STUnion(x); totalSeen8++; } });
thread8Loop.Stop();
thread8NoLoop.Start();
int totalSeen8No = 0;
Parallel.ForEach(pGeo, new ParallelOptions { MaxDegreeOfParallelism = 8 }, x =>
{ thread8NoAdd = thread8NoAdd.STUnion(x); totalSeen8No++; });
thread8NoLoop.Stop();
double cover = double.Parse(regularAdd.STArea().ToString());
double cover2 = double.Parse(thread2Add.STArea().ToString());
double cover2No = double.Parse(thread2NoAdd.STArea().ToString());
double cover3 = double.Parse(thread3Add.STArea().ToString());
double cover3No = double.Parse(thread3NoAdd.STArea().ToString());
double cover4 = double.Parse(thread4Add.STArea().ToString());
double cover4No = double.Parse(thread4NoAdd.STArea().ToString());
double cover5 = double.Parse(thread5Add.STArea().ToString());
double cover5No = double.Parse(thread5NoAdd.STArea().ToString());
double cover6 = double.Parse(thread6Add.STArea().ToString());
double cover6No = double.Parse(thread6NoAdd.STArea().ToString());
double cover7 = double.Parse(thread7Add.STArea().ToString());
double cover7No = double.Parse(thread7NoAdd.STArea().ToString());
double cover8 = double.Parse(thread8Add.STArea().ToString());
double cover8No = double.Parse(thread8NoAdd.STArea().ToString());
double perc2 = Math.Round(((cover2 / cover) * 100), 2);
double perc2No = Math.Round(((cover2No / cover) * 100), 2);
double perc3 = Math.Round(((cover3 / cover) * 100), 2);
double perc3No = Math.Round(((cover3No / cover) * 100), 2);
double perc4 = Math.Round(((cover4 / cover) * 100), 2);
double perc4No = Math.Round(((cover4No / cover) * 100), 2);
double perc5 = Math.Round(((cover5 / cover) * 100), 2);
double perc5No = Math.Round(((cover5No / cover) * 100), 2);
double perc6 = Math.Round(((cover6 / cover) * 100), 2);
double perc6No = Math.Round(((cover6No / cover) * 100), 2);
double perc7 = Math.Round(((cover7 / cover) * 100), 2);
double perc7No = Math.Round(((cover7No / cover) * 100), 2);
double perc8 = Math.Round(((cover8 / cover) * 100), 2);
double perc8No = Math.Round(((cover8No / cover) * 100), 2);
Console.WriteLine("TestName;Polygon Points;Checked Points;Duration;Coverage");
Console.WriteLine("Regular Loop;" + regularAdd.STNumPoints() + ";" + totalSeen1 + ";" + regularLoop.ElapsedMilliseconds + ";100%");
Console.WriteLine("2Loop No Lock;" + thread2NoAdd.STNumPoints() + ";" + totalSeen2No + ";" + thread2NoLoop.ElapsedMilliseconds + ";" + perc2No + "%");
Console.WriteLine("2Loop With Lock;" + thread2Add.STNumPoints() + ";" + totalSeen2 + ";" + thread2Loop.ElapsedMilliseconds + ";" + perc2 + "%");
Console.WriteLine("3Loop No Lock;" + thread3NoAdd.STNumPoints() + ";" + totalSeen3No + ";" + thread3NoLoop.ElapsedMilliseconds + ";" + perc3No + "%");
Console.WriteLine("3Loop With Lock;" + thread3Add.STNumPoints() + ";" + totalSeen3 + ";" + thread3Loop.ElapsedMilliseconds + ";" + perc3 + "%");
Console.WriteLine("4Loop No Lock;" + thread4NoAdd.STNumPoints() + ";" + totalSeen4No + ";" + thread4NoLoop.ElapsedMilliseconds + ";" + perc4No + "%");
Console.WriteLine("4Loop With Lock;" + thread4Add.STNumPoints() + ";" + totalSeen4 + ";" + thread4Loop.ElapsedMilliseconds + ";" + perc4 + "%");
Console.WriteLine("5Loop No Lock;" + thread5NoAdd.STNumPoints() + ";" + totalSeen5No + ";" + thread5NoLoop.ElapsedMilliseconds + ";" + perc5No + "%");
Console.WriteLine("5Loop With Lock;" + thread5Add.STNumPoints() + ";" + totalSeen5 + ";" + thread5Loop.ElapsedMilliseconds + ";" + perc5 + "%");
Console.WriteLine("6Loop No Lock;" + thread6NoAdd.STNumPoints() + ";" + totalSeen6No + ";" + thread6NoLoop.ElapsedMilliseconds + ";" + perc6No + "%");
Console.WriteLine("6Loop With Lock;" + thread6Add.STNumPoints() + ";" + totalSeen6 + ";" + thread6Loop.ElapsedMilliseconds + ";" + perc6 + "%");
Console.WriteLine("7Loop No Lock;" + thread7NoAdd.STNumPoints() + ";" + totalSeen7No + ";" + thread7NoLoop.ElapsedMilliseconds + ";" + perc7No + "%");
Console.WriteLine("7Loop With Lock;" + thread7Add.STNumPoints() + ";" + totalSeen7 + ";" + thread7Loop.ElapsedMilliseconds + ";" + perc7 + "%");
Console.WriteLine("8Loop No Lock;" + thread8NoAdd.STNumPoints() + ";" + totalSeen8No + ";" + thread8NoLoop.ElapsedMilliseconds + ";" + perc8No + "%");
Console.WriteLine("8Loop With Lock;" + thread8Add.STNumPoints() + ";" + totalSeen8 + ";" + thread8Loop.ElapsedMilliseconds + ";" + perc8 + "%");
Console.WriteLine("\n");
Console.WriteLine("Press any key to quit...");
Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment