我正在编写一些生成器和一个 Arbitrary,但速度太慢(另请参阅 GC 编号)。我想我的代码有错误,但我不知道在哪里。或者我的方法 ( map2 (fold)
) 是“奇怪的”?
发电机:
type Generators () =
static let notAllowed = Array.append [| '0'..'9' |] [| '\n'; '\r'; '['; ']'; '/'; |]
static let containsInvalidValues (s : string) = s.IndexOfAny(notAllowed) <> -1
static member positiveIntsGen() = Arb.generate<PositiveInt> |> Gen.map int
static member separatorStringGen() =
Arb.generate<NonEmptyString>
|> Gen.suchThat (fun s -> s.Get.Length < 5 && not (s.Get |> containsInvalidValues))
随意的:
let manyNumbersNewLineCustomDelimiterStrInput =
Gen.map2 (fun (ints : int[]) (nes : NonEmptyString) ->
Array.fold (fun acc num ->
if num % 2 = 0 then acc + "," + num.ToString()
else if num % 3 = 0 then acc + "\n" + num.ToString()
else acc + "\n" + num.ToString()) ("//[" + nes.Get + "]\n") ints )
(Generators.array12OfIntsGen())
(Generators.separatorStringGen())
|> Arb.fromGen
配置MaxTest = 500
完成,大约需要 5 分钟。
输出(使用#timer):
StrCalcTest.get_When pass an string that starts with "//[" and contains "]\n" use the multicharacter value between them as separator-Ok, passed 500 tests.
Real: 00:07:03.467, CPU: 00:07:03.296, GC gen0: 75844, gen1: 71968, gen2: 4