【发布时间】:2023-03-12 18:59:02
【问题描述】:
我在 coursera "scala specialization" 做作业时遇到了这个问题(这是简化版,不包含任何作业细节,只是数组遍历)
val chars: Array[Char] = some array
def fun1(idx:Int):Int = {
some code here (including the stop condition)
val c = chars(idx)
c match{
case '(' => fun1(idx+1)
case _ => fun1(idx+1)
}
}
这段代码比
慢 4 倍def fun2(idx: Int):Int = {
some code here (including the stop condition)
val c = chars(idx)
(c == '(') match{
case true => fun2(idx+1)
case _ => fun2(idx+1)
}
}
我所做的只是改变模式匹配 (我使用 ScalMeter 运行它,所以我相信统计数据)。
谁能解释这种行为?
【问题讨论】:
标签: performance scala pattern-matching