-
-
Save mattleibow/7f7f554d8717caa065970081b8ffd115 to your computer and use it in GitHub Desktop.
SKPath Iterator Benchmarks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using BenchmarkDotNet.Attributes; | |
using BenchmarkDotNet.Jobs; | |
using BenchmarkDotNet.Running; | |
namespace SkiaSharp.Benchmarks | |
{ | |
//[SimpleJob(RuntimeMoniker.CoreRt31)] | |
[SimpleJob(RuntimeMoniker.Mono)] | |
[SimpleJob(RuntimeMoniker.Net472)] | |
[SimpleJob(RuntimeMoniker.NetCoreApp31)] | |
[SimpleJob(RuntimeMoniker.NetCoreApp50)] | |
[MemoryDiagnoser] | |
public unsafe class Benchmark | |
{ | |
private List<SKPath> _paths = new List<SKPath>(); | |
public Benchmark() | |
{ | |
var random = new Random(); | |
// setup | |
for (var i = 0; i < 100; i++) | |
{ | |
var path = new SKPath(); | |
for (var l = 0; l < 100; l++) | |
{ | |
path.LineTo((float)random.NextDouble(), (float)random.NextDouble()); | |
} | |
_paths.Add(path); | |
} | |
} | |
[Benchmark(Description = "iterator.Next(new SKPoint[])", Baseline = true)] | |
public void ExistingRawIterator() | |
{ | |
// benchmark | |
foreach (var path in _paths) | |
{ | |
using var iterator = path.CreateRawIterator(); | |
var span = new Span<SKPoint>(new SKPoint[4]); | |
while (true) | |
{ | |
var verb = iterator.Next(span); | |
if (verb == SKPathVerb.Done) | |
{ | |
break; | |
} | |
} | |
} | |
} | |
[Benchmark(Description = "SkiaApi.sk_path_rawiter_next(new SKPoint[])")] | |
public void SkiaApiRawIterator() | |
{ | |
// benchmark | |
foreach (var path in _paths) | |
{ | |
using var iterator = path.CreateRawIterator(); | |
var span = new Span<SKPoint>(new SKPoint[4]); | |
fixed (SKPoint* p = span) | |
{ | |
while (true) | |
{ | |
var verb = SkiaApi.sk_path_rawiter_next(iterator.Handle, p); | |
if (verb == SKPathVerb.Done) | |
{ | |
break; | |
} | |
} | |
} | |
} | |
} | |
[Benchmark(Description = "iterator.Next(new SKPoint*)")] | |
public void NewRawIterator() | |
{ | |
// benchmark | |
foreach (var path in _paths) | |
{ | |
using var iterator = path.CreateRawIterator(); | |
fixed (SKPoint* p = new SKPoint[4]) | |
{ | |
while (true) | |
{ | |
var verb = SkiaApi.sk_path_rawiter_next(iterator.Handle, p); | |
if (verb == SKPathVerb.Done) | |
{ | |
break; | |
} | |
} | |
} | |
} | |
} | |
[Benchmark(Description = "iterator.Next(stackalloc SKPoint[])")] | |
public void StackAllocExistingRawIterator() | |
{ | |
// benchmark | |
foreach (var path in _paths) | |
{ | |
using var iterator = path.CreateRawIterator(); | |
Span<SKPoint> span = stackalloc SKPoint[4]; | |
while (true) | |
{ | |
var verb = iterator.Next(span); | |
if (verb == SKPathVerb.Done) | |
{ | |
break; | |
} | |
} | |
} | |
} | |
[Benchmark(Description = "SkiaApi.sk_path_rawiter_next(stackalloc SKPoint[])")] | |
public void StackAllocSkiaApiRawIterator() | |
{ | |
// benchmark | |
foreach (var path in _paths) | |
{ | |
using var iterator = path.CreateRawIterator(); | |
var span = stackalloc SKPoint[4]; | |
while (true) | |
{ | |
var verb = SkiaApi.sk_path_rawiter_next(iterator.Handle, span); | |
if (verb == SKPathVerb.Done) | |
{ | |
break; | |
} | |
} | |
} | |
} | |
[Benchmark(Description = "iterator.Next(stackalloc SKPoint*)")] | |
public void StackAllocNewRawIterator() | |
{ | |
// benchmark | |
foreach (var path in _paths) | |
{ | |
using var iterator = path.CreateRawIterator(); | |
var span = stackalloc SKPoint[4]; | |
while (true) | |
{ | |
var verb = iterator.Next(span); | |
if (verb == SKPathVerb.Done) | |
{ | |
break; | |
} | |
} | |
} | |
} | |
} | |
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
var summary = BenchmarkRunner.Run<Benchmark>(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment