Skip to content

Instantly share code, notes, and snippets.

@isaacabraham
Last active January 27, 2019 16:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isaacabraham/34337f938852b47bc196b9b9272ce5ab to your computer and use it in GitHub Desktop.
Save isaacabraham/34337f938852b47bc196b9b9272ce5ab to your computer and use it in GitHub Desktop.
#I @"C:\Users\Isaac\.nuget\packages"
#r @"Microsoft.NETCore.App\2.2.1\ref\netcoreapp2.2\netstandard.dll"
#r @"system.memory\4.5.2\lib\netstandard2.0\System.Memory.dll"
#r @"System.Runtime.CompilerServices.Unsafe\4.5.2\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll"
#r "System.Runtime"
open System
let x = "Foobarbazbiz"
type Span<'T> with
member inline this.GetSlice(startS, endS) =
match (startS, endS) with
| (None, None) -> this
| (Some s, None) -> this.Slice s
| (None, Some s) -> this.Slice(0, s + 1)
| (Some a, Some b) -> this.Slice(a, (b - a + 1))
type ReadOnlySpan<'T> with
member inline this.GetSlice(startS, endS) =
match (startS, endS) with
| (None, None) -> this
| (Some s, None) -> this.Slice s
| (None, Some s) -> this.Slice(0, s + 1)
| (Some a, Some b) -> this.Slice(a, (b-a + 1))
let inline foo() =
let z = x.AsSpan()
z.[0..2]
let bar() =
let z = x.AsSpan()
z.Slice(0, 3)
let baz() =
x.Substring(0, 3)
#time
// Real: 00:00:00.011, CPU: 00:00:00.015, GC gen0: 0, gen1: 0, gen2: 0
for _ in 1 .. 1000000 do
let _ = foo()
()
// Real: 00:00:00.002, CPU: 00:00:00.015, GC gen0: 0, gen1: 0, gen2: 0
for _ in 1 .. 1000000 do
let _ = bar()
()
// Real: 00:00:00.016, CPU: 00:00:00.015, GC gen0: 10, gen1: 0, gen2: 0
for _ in 1 .. 1000000 do
let _ = baz()
()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment