Threadring的计算机语言基准游戏的F# 条目包含一个看似无用的行:if false then ()
. 当我注释掉这一行时,程序运行得更快(对于 50000000 的输入,~2s vs ~55s)并产生相同的结果。这是如何运作的?为什么会有这条线?编译器到底在做什么似乎是无操作的?
编码:
let ringLength = 503
let cells = Array.zeroCreate ringLength
let threads = Array.zeroCreate ringLength
let answer = ref -1
let createWorker i =
let next = (i+1)%ringLength
async { let value = cells.[i]
if false then ()
match value with
| 0 -> answer := i+1
| _ ->
cells.[next] <- value - 1
return! threads.[next] }
[<EntryPoint>]
let main args =
cells.[0] <- if args.Length>0 then int args.[0] else 50000000
for i in 0..ringLength-1 do
threads.[i]<-createWorker i
let result = Async.StartImmediate(threads.[0])
printfn "%d" !answer
0