2 people like it.
Like the snippet!
sprintf vs String.Format
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
|
open FSharpx.TimeMeasurement //in nuget package FSharpx.Extras
compareTwoRuntimes
10
"sprintf"
(fun _ -> for _ in 1..100000 do sprintf "%d" 1024 |> ignore)
"String.Format"
(fun _ -> for _ in 1..100000 do System.String.Format ("{0}", 1024) |> ignore)
compareTwoRuntimes
10
"sprintf"
(fun _ ->
for _ in 1..100000 do
sprintf "%d %s %O" 1024 "a string" (obj()) |> ignore)
"String.Format"
(fun _ ->
for _ in 1..100000 do
System.String.Format ("{0} {1} {2}", 1024, "a string", obj()) |> ignore)
compareTwoRuntimes
10
"sprintf"
(fun _ ->
for _ in 1..100000 do
sprintf "%d %s %O %d %s %O" 1024 "a string" (obj()) 1024 "a string" (obj()) |> ignore)
"String.Format"
(fun _ ->
for _ in 1..100000 do
System.String.Format ("{0} {1} {2} {3} {4} {5}", 1024, "a string", obj(), 1024, "a string", obj()) |> ignore)
(*
sprintf 45.0ms
String.Format 25.6ms
Ratio: 1.7578125
sprintf 165.9ms
String.Format 40.7ms
Ratio: 4.076167076
sprintf 299.1ms
String.Format 73.7ms
Ratio: 4.05834464
*)
|
namespace FSharpx
module TimeMeasurement
from FSharpx
val compareTwoRuntimes : count:int -> desc1:string -> f1:(unit -> 'a) -> desc2:string -> f2:(unit -> 'b) -> unit
Full name: FSharpx.TimeMeasurement.compareTwoRuntimes
val sprintf : format:Printf.StringFormat<'T> -> 'T
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.sprintf
val ignore : value:'T -> unit
Full name: Microsoft.FSharp.Core.Operators.ignore
namespace System
Multiple items
type String =
new : value:char -> string + 7 overloads
member Chars : int -> char
member Clone : unit -> obj
member CompareTo : value:obj -> int + 1 overload
member Contains : value:string -> bool
member CopyTo : sourceIndex:int * destination:char[] * destinationIndex:int * count:int -> unit
member EndsWith : value:string -> bool + 2 overloads
member Equals : obj:obj -> bool + 2 overloads
member GetEnumerator : unit -> CharEnumerator
member GetHashCode : unit -> int
...
Full name: System.String
--------------------
System.String(value: nativeptr<char>) : unit
System.String(value: nativeptr<sbyte>) : unit
System.String(value: char []) : unit
System.String(c: char, count: int) : unit
System.String(value: nativeptr<char>, startIndex: int, length: int) : unit
System.String(value: nativeptr<sbyte>, startIndex: int, length: int) : unit
System.String(value: char [], startIndex: int, length: int) : unit
System.String(value: nativeptr<sbyte>, startIndex: int, length: int, enc: System.Text.Encoding) : unit
System.String.Format(format: string, [<System.ParamArray>] args: obj []) : string
System.String.Format(format: string, arg0: obj) : string
System.String.Format(provider: System.IFormatProvider, format: string, [<System.ParamArray>] args: obj []) : string
System.String.Format(format: string, arg0: obj, arg1: obj) : string
System.String.Format(format: string, arg0: obj, arg1: obj, arg2: obj) : string
type obj = System.Object
Full name: Microsoft.FSharp.Core.obj
More information