i have following function in (turn-based) game code, lists legal moves player in game:
let moves game = let movestype1 game = ... //returns move list let movestype2 game = ... //returns move list let movestype3 game = ... //returns move list list.concat [ (movestype1 game); (movestype2 game); (movestype3 game) ]
now i'm wondering difference if used list comprehension , yield!
following:
let moves game = let movestype1 game = ... //returns move list let movestype2 game = ... //returns move list let movestype3 game = ... //returns move list [ yield! movestype1 game yield! movestype2 game yield! movestype3 game ]
i might use function many times in cases, i'm little concerned performance.
here simple test script measuring timing difference between 2 implementations:
let test1 () = list.concat [ [1..10000]; [1..10000]; [1..10000] ] let test2 () = [ yield! [1..10000] yield! [1..10000] yield! [1..10000] ] let runtest testimplementation = in 1..1000 testimplementation () |> ignore #time runtest test1 //real: 00:00:02.353, cpu: 00:00:02.371, gc gen0: 143, gen1: 96, gen2: 1 #time system.gc.waitforfullgccomplete() |> ignore #time runtest test2 //real: 00:00:03.739, cpu: 00:00:03.712, gc gen0: 185, gen1: 185, gen2: 0 #time
it appears list.concat
bit better, performance things, should measure see if performance benefit matters use case.
Comments
Post a Comment