Created
November 1, 2018 01:40
-
-
Save CaringDev/ee3d7df4306a64369b2c44a5c4d513f7 to your computer and use it in GitHub Desktop.
Benchmark for https://stackoverflow.com/questions/53088411
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
namespace Benchmark | |
open BenchmarkDotNet.Attributes | |
open BenchmarkDotNet.Running | |
[<HtmlExporter>] | |
type CommaAndDot() = | |
let mutable words : string list = [] | |
[<Params (0, 1, 10, 100)>] | |
member val public Verbosity = 0 with get, set | |
[<GlobalSetup>] | |
member self.GlobalSetup() = | |
words <- List.init self.Verbosity (fun i -> System.IO.Path.GetTempFileName()) | |
[<Benchmark>] | |
member __.Concat() = | |
words | |
|> List.mapi (fun i n -> if i = words.Length - 1 && i > 0 then "and " + n else n) | |
|> String.concat ", " | |
[<Benchmark>] | |
member __.Yield() = | |
let rec seriesComma = function | |
| [] -> [] | |
| [x] -> [x; "."] | |
| x::[y] -> [x; ", and "] @ seriesComma [y] | |
| x::xs -> [yield! [x; ", "]; yield! seriesComma xs] | |
words |> seriesComma |> String.concat "" | |
[<Benchmark>] | |
member __.Accumulator() = | |
let rec helper acc = function | |
| [] -> acc | |
| [ a ] -> sprintf "%s%s." acc a | |
| a :: [ b ] -> sprintf "%s%s and %s." acc a b | |
| a :: rest -> helper (acc + sprintf "%s, " a) rest | |
helper "" words | |
[<Benchmark>] | |
member __.Builder() = | |
let sb = System.Text.StringBuilder() | |
let rec helper = function | |
| [] -> sb | |
| [ a ] -> sprintf "%s." a |> sb.Append | |
| a :: [ b ] -> sprintf "%s and %s." a b |> sb.Append | |
| a :: rest -> | |
sprintf "%s, " a |> sb.Append |> ignore | |
helper rest | |
helper words |> string | |
[<Benchmark>] | |
member __.BuilderOpt() = | |
let sb = System.Text.StringBuilder() | |
let rec helper = function | |
| [ a ] -> sprintf "%s." a |> sb.Append | |
| a :: [ b ] -> sprintf "%s and %s." a b |> sb.Append | |
| a :: rest -> | |
sprintf "%s, " a |> sb.Append |> ignore | |
helper rest | |
| [] -> sb | |
helper words |> string | |
[<Benchmark(Baseline = true)>] | |
member __.BuilderChained() = | |
let sb = System.Text.StringBuilder() | |
let rec helper = function | |
| [ a ] -> | |
sb.Append(a : string).Append('.') | |
| a :: [ b ] -> | |
sb.Append(a).Append(" and ").Append(b).Append('.') | |
| a :: rest -> | |
sb.Append(a).Append(", ") |> ignore | |
helper rest | |
| [] -> sb | |
helper words |> string | |
[<Benchmark>] | |
member __.BuilderFormat() = | |
let sb = System.Text.StringBuilder() | |
let rec helper = function | |
| [ a : string ] -> | |
sb.AppendFormat("{0}.", a) | |
| a :: [ b ] -> | |
sb.AppendFormat("{0} and {1}.", a, b) | |
| a :: rest -> | |
sb.AppendFormat("{0}, ", a) |> ignore | |
helper rest | |
| [] -> sb | |
helper words |> string | |
module Program = | |
[<EntryPoint>] | |
let main _ = | |
BenchmarkRunner.Run<CommaAndDot>() |> ignore | |
0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment