Skip to content

Instantly share code, notes, and snippets.

@latkin latkin/test.fsx
Created Apr 22, 2015

Embed
What would you like to do?
Try block perf overhead
#time ;;
let testNoTry () =
let mutable x = 0
for i in 1 .. 1000000000 do
x <- x + 1
x
let testTry () =
let mutable x = 0
for i in 1 .. 1000000000 do
try
x <- x + 1
with _ -> ()
x
let data =
let rnd = System.Random()
Array.init 1000000 (fun _ -> rnd.Next())
let testNoTryRnd () =
let mutable x = 0
for i in 0 .. 1000000000 do
x <- x + data.[i % data.Length]
x
let testTryRnd () =
let mutable x = 0
for i in 0 .. 1000000000 do
try
x <- x + data.[i % data.Length]
with _ -> ()
x
let testNoTryFloat () =
let mutable x = 0.0
for i in 1. .. 100000000. do
x <- x + 1.
x
let testTryFloat () =
let mutable x = 0.
for i in 1. .. 100000000. do
try
x <- x + 1.
with _ -> ()
x
;;
testNoTry () ;; // 0.327s, this case seems to be super-optimized by the JIT
testTry () ;; // 1.996s
testNoTryRnd () ;; // 2.837s
testTryRnd () ;; // 3.482s 645ms cost over 1B iterations: ~1.5M iters to reach 1ms cost
testNoTryFloat () ;; // 6.420s
testTryFloat () ;; // 6.411s undetectable cost
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.