0 people like it.

Logging speed

Using Type Provider generated functions for type safe logging.

 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: 
45: 
46: 
47: 
48: 
49: 
50: 
51: 
52: 
53: 
54: 
55: 
56: 
57: 
58: 
59: 
60: 
61: 
62: 
63: 
64: 
65: 
66: 
67: 
68: 
69: 
70: 
71: 
72: 
73: 
74: 
75: 
76: 
77: 
78: 
79: 
80: 
81: 
82: 
83: 
84: 
85: 
86: 
open FSharpx.TimeMeasurement
open TypeSet.Provided

printfn "Single Int:"
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" 1024 |> ignore)
    "TypeSet"
    (fun _ -> for _ in 1..100000 do TPrint<"%d">.show 1024 |> ignore)

compareTwoRuntimes 
    10
    "TypeSet"
    (fun _ -> for _ in 1..100000 do TPrint<"%d">.show 1024 |> ignore)
    "String.Format"
    (fun _ -> for _ in 1..100000 do System.String.Format ("{0}", 1024) |> ignore)


printfn ""
printfn ""


printfn "Int and String"

compareTwoRuntimes 
    10
    "TPrint"
    (fun _ -> 
        for _ in 1..100000 do
            TPrint<"%d %s %s">.show 1024 "a string" "Another" |> ignore)
    "String.Format"
    (fun _ ->
        for _ in 1..100000 do
            System.String.Format ("{0} {1} {2}", 1024, "a string", "Another") |> ignore)


printfn ""
printfn ""

printfn "A bit longer string"

compareTwoRuntimes 
    10
    "sprintf"
    (fun _ -> 
        for _ in 1..100000 do
            TPrint<"%d %s %s %d %s %s">.show 1024 "a string" "#" 1024 "a string" "Some more" |> ignore)
    "String.Format"
    (fun _ ->
        for _ in 1..100000 do
            System.String.Format ("{0} {1} {2} {3} {4} {5}", 1024, "a string", "#", 1024, "a string", "Some more") |> ignore)


System.Console.ReadLine()|>ignore
(*
Single Int:
sprintf 54.5ms
String.Format 25.9ms
  Ratio:  2.104247104
sprintf 49.4ms
TypeSet 18.5ms
  Ratio:  2.67027027
TypeSet 20.4ms
String.Format 25.9ms
  Ratio:  0.7876447876


Int and String
TPrint 33.7ms
String.Format 33.8ms
  Ratio:  0.9970414201


A bit longer string
sprintf 71.9ms
String.Format 72.0ms
  Ratio:  0.9986111111
*)
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
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 Console =
  static member BackgroundColor : ConsoleColor with get, set
  static member Beep : unit -> unit + 1 overload
  static member BufferHeight : int with get, set
  static member BufferWidth : int with get, set
  static member CapsLock : bool
  static member Clear : unit -> unit
  static member CursorLeft : int with get, set
  static member CursorSize : int with get, set
  static member CursorTop : int with get, set
  static member CursorVisible : bool with get, set
  ...

Full name: System.Console
System.Console.ReadLine() : string
Raw view Test code New version

More information

Link:http://fssnip.net/mR
Posted:9 years ago
Author:mavnn
Tags: type providers