- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
以下代码显示使用包含 for
的序列表达式生成序列比使用 Seq.init
生成相同序列快大约五倍.
open System
let rand count =
let rnd = Random() // if this value is not created all numbers are equal
seq {for i in 0..(count - 1) -> rnd.NextDouble()}
/// Perhaps more "functional" than rand but slower
let rand2 count =
let rnd = Random()
let rnd2 (i: int) = rnd.NextDouble()
Seq.init count rnd2
> rand 1000000 |> List.ofSeq |> List.head;;
Real: 00:00:00.092, CPU: 00:00:00.093, GC gen0: 3, gen1: 2, gen2: 0
val it : float = 0.1358240168
> rand2 1000000 |> List.ofSeq |> List.head;;
Real: 00:00:00.473, CPU: 00:00:00.484, GC gen0: 21, gen1: 20, gen2: 1
val it : float = 0.4128856414
Seq.init
替代在某种意义上“更具功能性”然后序列表达替代?
最佳答案
Seq.init
很慢,因为它使用 Seq.upto
这是缓慢的。 Seq.upto
很慢,主要是因为它创建了一个 Lazy
管道中每个对象的实例。这也解释了 GC 压力。Seq
不是正确的选择。seq {for i in 0..(count - 1) -> rnd.NextDouble()}
Seq
. Seq.init
替代在某种意义上“更具功能性”然后序列表达替代?Seq
和
LINQ
慢的是他们依赖拉管道。推送管道更快。 Nessos 和 manofstick 管道都支持 AFAICT 并在可能的情况下选择推送。
// A simplistic push-pipeline
type Receiver<'T> = 'T -> bool
type Stream<'T> = Receiver<'T> -> unit
module Stream =
let inline init count gen : Stream<_> =
fun r ->
let rec loop i =
if i < count && r (gen i) then
loop (i + 1)
loop 0
let inline sum (s : Stream<_>) : _ =
let mutable a = LanguagePrimitives.GenericZero<_>
s (fun v -> a <- a + v; true)
a
let rnd = System.Random ()
let gen = fun _ -> rnd.NextDouble ()
let clock =
let sw = System.Diagnostics.Stopwatch ()
sw.Start ()
fun () -> sw.ElapsedMilliseconds
open System
let timeIt n a =
let r = a () // Warm-up
GC.Collect (2, GCCollectionMode.Forced, true)
let inline cc g = GC.CollectionCount g
let bcc0, bcc1, bcc2 = cc 0, cc 1, cc 2
let before = clock ()
for i = 1 to n do
a () |> ignore
let after = clock ()
let acc0, acc1, acc2 = cc 0, cc 1, cc 2
after - before, acc0 - bcc0, acc1 - bcc1, acc2 - bcc2, r
open System.Linq
[<EntryPoint>]
let main argv =
let count = 10000000
let outers =
[|
1000000
10000
100
1
|]
for outer in outers do
let inner = count / outer
let testCases =
[|
"Push stream" , fun () -> Stream.init inner gen |> Stream.sum
"LINQ" , fun () -> (Enumerable.Range (0, inner)).Select(gen).Sum()
"Seq.init" , fun () -> Seq.init inner gen |> Seq.sum
"Seq comprehension" , fun () -> seq { for i in 0..inner - 1 -> gen i } |> Seq.sum
"Tail-recursion" , fun () ->
let rec loop a i =
if i < inner then
loop (a + gen i) (i + 1)
else
a
loop 0. 0
|]
printfn "Using outer = %A, inner = %A, total is: %A" outer inner count
for nm, a in testCases do
printfn " Running test case: %A" nm
let tm, cc0, cc1, cc2, r = timeIt outer a
printfn " it took %A ms (%A, %A, %A), result is: %A" tm cc0 cc1 cc2 r
0
Using outer = 1000000, inner = 10, total is: 10000000
Running test case: "Push stream"
it took 145L ms (22, 0, 0), result is: 5.348407591
Running test case: "LINQ"
it took 296L ms (63, 0, 0), result is: 5.056716735
Running test case: "Seq.init"
it took 1490L ms (724, 0, 0), result is: 3.977087705
Running test case: "Seq comprehension"
it took 333L ms (66, 0, 0), result is: 5.208401204
Running test case: "Tail-recursion"
it took 109L ms (0, 0, 0), result is: 5.898073628
Using outer = 10000, inner = 1000, total is: 10000000
Running test case: "Push stream"
it took 118L ms (0, 0, 0), result is: 510.943297
Running test case: "LINQ"
it took 210L ms (0, 0, 0), result is: 488.3970571
Running test case: "Seq.init"
it took 1411L ms (661, 0, 0), result is: 505.2632877
Running test case: "Seq comprehension"
it took 264L ms (0, 0, 0), result is: 502.1710107
Running test case: "Tail-recursion"
it took 101L ms (0, 0, 0), result is: 487.9451813
Using outer = 100, inner = 100000, total is: 10000000
Running test case: "Push stream"
it took 118L ms (0, 0, 0), result is: 49850.99306
Running test case: "LINQ"
it took 202L ms (0, 0, 0), result is: 50113.06557
Running test case: "Seq.init"
it took 1398L ms (661, 0, 0), result is: 49923.14521
Running test case: "Seq comprehension"
it took 262L ms (0, 0, 0), result is: 50196.00191
Running test case: "Tail-recursion"
it took 98L ms (0, 0, 0), result is: 49878.16573
Using outer = 1, inner = 10000000, total is: 10000000
Running test case: "Push stream"
it took 117L ms (0, 0, 0), result is: 5000088.583
Running test case: "LINQ"
it took 201L ms (0, 0, 0), result is: 5000569.657
Running test case: "Seq.init"
it took 1388L ms (661, 0, 0), result is: 5000169.339
Running test case: "Seq comprehension"
it took 260L ms (0, 0, 0), result is: 5000263.083
Running test case: "Tail-recursion"
it took 97L ms (0, 0, 0), result is: 4999990.197
Press any key to continue . . .
Seq.init
在 CPU 性能和内存使用方面做得最差,“尾递归”(本质上是一个循环)做得最好。
id
相反,数字看起来像这样:
Using outer = 1000000, inner = 10, total is: 10000000
Running test case: "Push stream"
it took 47L ms (22, 0, 0), result is: 45.0
Running test case: "LINQ"
it took 211L ms (63, 0, 0), result is: 45.0
Running test case: "Seq.init"
it took 1364L ms (724, 0, 0), result is: 45.0
Running test case: "Seq comprehension"
it took 241L ms (66, 0, 0), result is: 45.0
Running test case: "Tail-recursion"
it took 10L ms (0, 0, 0), result is: 45.0
Using outer = 10000, inner = 1000, total is: 10000000
Running test case: "Push stream"
it took 28L ms (0, 0, 0), result is: 499500.0
Running test case: "LINQ"
it took 125L ms (0, 0, 0), result is: 499500.0
Running test case: "Seq.init"
it took 1285L ms (661, 0, 0), result is: 499500.0
Running test case: "Seq comprehension"
it took 170L ms (0, 0, 0), result is: 499500.0
Running test case: "Tail-recursion"
it took 8L ms (0, 0, 0), result is: 499500.0
Using outer = 100, inner = 100000, total is: 10000000
Running test case: "Push stream"
it took 27L ms (0, 0, 0), result is: 4999950000.0
Running test case: "LINQ"
it took 121L ms (0, 0, 0), result is: 4999950000.0
Running test case: "Seq.init"
it took 1289L ms (661, 0, 0), result is: 4999950000.0
Running test case: "Seq comprehension"
it took 169L ms (0, 0, 0), result is: 4999950000.0
Running test case: "Tail-recursion"
it took 9L ms (0, 0, 0), result is: 4999950000.0
Using outer = 1, inner = 10000000, total is: 10000000
Running test case: "Push stream"
it took 28L ms (0, 0, 0), result is: 4.9999995e+13
Running test case: "LINQ"
it took 121L ms (0, 0, 0), result is: 4.9999995e+13
Running test case: "Seq.init"
it took 1289L ms (661, 0, 0), result is: 4.9999995e+13
Running test case: "Seq comprehension"
it took 169L ms (0, 0, 0), result is: 4.9999995e+13
Running test case: "Tail-recursion"
it took 8L ms (0, 0, 0), result is: 4.9999995e+13
关于f# - 为什么 Seq.init 比带有 'for' 的序列表达式慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43696025/
我是 F# 的新手,目前想知道如何将序列的字节序列转换为序列的浮点序列 seq -> seq 所以我有以下字节序列 let colourList = seq[ seq[10uy;20uy;30uy];
我想在一个序列中聚合兼容的元素,即转换 Seq[T]成Seq[Seq[T]]其中每个子序列中的元素彼此兼容,同时保留原始 seq 顺序,例如从 case class X(i: Int, n: Int)
以下函数files返回seq> 。如何让它返回seq相反? type R = { .... } let files = seqOfStrs |> Seq.choose(fun s -> mat
我正在尝试转换如下所示的数据: val inputData = Seq(("STUDY1", "Follow-up", 1), ("STUDY1", "Off Study", 2),
稍微简化一下,我的问题来自字符串列表 input我想用函数解析 parse返回 Either[String,Int] . 然后list.map(parse)返回 Either 的列表s。程序的下一步是
如标题中所述,我不明白为什么这些函数无法编译并要求 Seq。 def f1[V a + b } error: type mismatch; found : Seq[Int] required:
我有一个类型为 Flow[T, Seq[Seq[String]], NotUsed] 的流。 我想以示例流的方式将其展平 ev1: Seq(Seq("a", "b"), Seq("n", "m") e
我对 Scala 比较陌生,但我想我理解它的类型系统和并行集合,但我无法理解这个错误: 我有一个函数 def myFun(a : Seq[MyType], b : OtherType) : Seq[M
在学习 F# 时,我正在做一个小挑战: Enter a string and the program counts the number of vowels in the text. For adde
------------------------- clojure.core/seq ([coll]) Returns a seq on the collection. If the collec
我担心不知道什么时候可以使用 "Seq", "seq"。你能告诉我有哪些不同之处吗? 这是我的代码。为什么不使用“seq”? let s = ResizeArray() s.Add(1.1) s
我试图返回一个带有直到循环的可变序列,但我有一个不可变的序列作为 (0 until nbGenomes) 的返回: def generateRandomGenome(nbGenomes:Int):
将 Seq(Seq) 分配到多个类型化数组而不先将 Seq 分配给标量的正确语法是什么? Seq 是否会以某种方式变平?这失败了: class A { has Int $.r } my A (@ra1
我正在尝试训练 序列到序列 一个简单的正弦波模型。目标是获得Nin数据点和预测 Nout下一个数据点。任务看起来很简单,模型对大频率的预测很好 freq (y = sin(freq * x))。例如,
我正在努力重构一些使用 Seq 的 Node.js 代码,以及文档和 this answer ,我知道我使用 this() 转到下一个 .seq(),但是如何将变量传递给下一个 .seq( )的功能?
我有一个像这样的字符串序列(文件中的行) [20150101] error a details 1 details 2 [20150101] error b details [20150101] er
给定两个序列 a 和 b,声明如下: var a = @[1, 2, 3] b = @[4, 5, 6] a = b 会创建一个新的 seq 将所有内容从 b 复制到 a 还是重用 a?我有特
type Suit = Spades | Clubs | Hearts | Diamonds type Rank = Ace | Two | Three | Four | Five | Six | S
慢慢地掌握列表匹配和尾递归的窍门,我需要一个函数将列表“缝合”在一起,去掉中间值(更容易显示而不是解释): 合并 [[1;2;3];[3;4;5];[5;6;7]]//-> [1;2;3;4;5;6;
为什么这段代码不起作用? type Test() = static member func (a: seq) = 5. let a = [[4.]] Test.func(a) 它给出以下错误: T
我是一名优秀的程序员,十分优秀!