Old*_*vec 3 null benchmarking f# option
我需要在繁重的数值模拟中使用Some/None选项.以下微基准测试给了我Fast = 485和Slow = 5890.
我不喜欢null,即使我喜欢它们,我也不能使用null因为The type 'float' does not have 'null' as a proper value.
理想情况下,会有一个编译器选项将Some/None编译为value/null,因此不会有运行时惩罚.那可能吗?或者我如何使Some/None有效?
let s = System.Diagnostics.Stopwatch()
s.Start()
for h in 0 .. 1000 do
Array.init 100000 (fun i -> (float i + 1.)) |> ignore
printfn "Fast = %d" s.ElapsedMilliseconds
s.Restart()
for h in 0 .. 1000 do
Array.init 100000 (fun i -> Some (float i + 1.)) |> ignore
printfn "Slow = %d" s.ElapsedMilliseconds
Run Code Online (Sandbox Code Playgroud)
None实际上已经表示为null.但由于它option<_>是一种引用类型(null在.NET类型系统中成为有效值所必需的),因此创建Some实例必然需要堆分配.一种替代方法是使用.NET System.Nullable<_>类型,类似于option<_>,除了:
option<string>,但不能创建一个Nullable<string>.对于您的用例,这似乎是一个不重要的因素.请记住,您的基准测试工作很少,因此结果可能不是您在实际工作负载中看到的典型结果.如果可能的话,尝试根据您的实际情况使用更有意义的基准.
作为旁注,如果您#time在F#中使用该指令而不是烦恼,那么您将获得更有意义的诊断(包括垃圾收集统计)Stopwatch.