Created
April 22, 2015 03:38
-
-
Save latkin/857be0ec50626d4d3327 to your computer and use it in GitHub Desktop.
Try block perf overhead
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
#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