Last active
January 30, 2024 13:54
-
-
Save bikallem/7f61a1bf955691369e3e196aca34df91 to your computer and use it in GitHub Desktop.
int_of_string_opt perf
This file contains hidden or 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
| open Bechamel | |
| open Toolkit | |
| let opt_int0 = | |
| Test.make ~name:"(opaque/prim/Some)" (Staged.stage @@ fun () -> | |
| Sys.opaque_identity @@ int_of_string_opt "123") | |
| let opt_int1 = | |
| Test.make ~name:"(opaque/prim/None)" (Staged.stage @@ fun () -> | |
| Sys.opaque_identity @@ int_of_string_opt "123aa") | |
| let opt_int_exn0 = | |
| Test.make ~name:"(opaque/exn/Some)" (Staged.stage @@ fun () -> | |
| Sys.opaque_identity (try Some(int_of_string "123") with _ -> None)) | |
| let opt_int_exn1 = | |
| Test.make ~name:"(opaque/exn/None)" (Staged.stage @@ fun () -> | |
| Sys.opaque_identity (try Some(int_of_string "123aa") with _ -> None)) | |
| (************ Array based benchmark. *************) | |
| let opt_int_arr0 = | |
| Test.make ~name:"(arr/prim/Some)" (Staged.stage @@ fun () -> | |
| let ar = Array.make 1000 (Some 0) in | |
| for i = 0 to 1000 - 1 do | |
| ar.(i) <- int_of_string_opt "123" | |
| done; | |
| ) | |
| let opt_int_arr1 = | |
| Test.make ~name:"(arr/prim/None)" (Staged.stage @@ fun () -> | |
| let ar = Array.make 1000 (None) in | |
| for i = 0 to 1000 - 1 do | |
| ar.(i) <- int_of_string_opt "123aa" | |
| done; | |
| ) | |
| let opt_int_exn_arr0 = | |
| Test.make ~name:"(arr/exn/Some)" (Staged.stage @@ fun () -> | |
| let ar = Array.make 1000 (Some 0) in | |
| for i = 0 to 1000 - 1 do | |
| ar.(i) <- try Some(int_of_string "123") with _ -> None | |
| done; | |
| ) | |
| let opt_int_exn_arr1 = | |
| Test.make ~name:"(arr/exn/None)" (Staged.stage @@ fun () -> | |
| let ar = Array.make 1000 None in | |
| for i = 0 to 1000 - 1 do | |
| ar.(i) <- try Some(int_of_string "123aa") with _ -> None | |
| done; | |
| ) | |
| let tests = Test.make_grouped ~name:"int_of_string_opt" ~fmt:"%s %s" | |
| [ opt_int0 | |
| ; opt_int_exn0 | |
| ; opt_int1 | |
| ; opt_int_exn1 | |
| ; opt_int_arr0 | |
| ; opt_int_exn_arr0 | |
| ; opt_int_arr1 | |
| ; opt_int_exn_arr1 | |
| ] | |
| let benchmark () = | |
| let ols = | |
| Analyze.ols ~bootstrap:0 ~r_square:true ~predictors:Measure.[| run |] | |
| in | |
| let instances = | |
| Instance.[ minor_allocated; major_allocated; monotonic_clock ] | |
| in | |
| let cfg = | |
| Benchmark.cfg ~limit:2000 ~quota:(Time.second 0.5) ~kde:(Some 1000) () | |
| in | |
| let raw_results = Benchmark.all cfg instances tests in | |
| let results = | |
| List.map (fun instance -> Analyze.all ols instance raw_results) instances | |
| in | |
| let results = Analyze.merge ols instances results in | |
| (results, raw_results) | |
| let () = | |
| List.iter | |
| (fun v -> Bechamel_notty.Unit.add v (Measure.unit v)) | |
| Instance.[ minor_allocated; major_allocated; monotonic_clock ] | |
| let img (window, results) = | |
| Bechamel_notty.Multiple.image_of_ols_results ~rect:window | |
| ~predictor:Measure.run results | |
| open Notty_unix | |
| let () = | |
| let window = | |
| match winsize Unix.stdout with | |
| | Some (w, h) -> { Bechamel_notty.w; h } | |
| | None -> { Bechamel_notty.w = 80; h = 1 } | |
| in | |
| let results, _ = benchmark () in | |
| img (window, results) |> eol |> output_image |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment