第82讲:Scala中List的ListBuffer实现高效的遍历计算
来源:互联网 发布:ubuntu 15.04 下载 编辑:程序博客网 时间:2024/06/05 04:59
我们通过对比下列4组程序,对比,发现优缺点
第一组: 递归
代码
def main(args: Array[String]) { val data = 1 to 20000 val currntTime =System.currentTimeMillis() increase(data.toList) println("used time=" + (System.currentTimeMillis() - currntTime)) } def increase(list:List[Int]):List[Int] = list match { case List() => List() case head2 :: tail => (head2 + 1) :: increase(tail) }
运行结果:
Exception in thread “main” java.lang.StackOverflowError
at scala.collection.LinearSeqOptimizedclass.lengthCompare(LinearSeqOptimized.scala:261)atscala.collection.immutable.List.lengthCompare(List.scala:84)atcom.ifly.edu.scala.list.ListBufferInternals .increase(ListBuffer_Internals.scala:19)
at com.ifly.edu.scala.list.ListBuffer_Internals.increase(ListBufferInternals.scala:20)atcom.ifly.edu.scala.list.ListBufferInternals .increase(ListBuffer_Internals.scala:20)
at com.ifly.edu.scala.list.ListBuffer_Internals.increase(ListBufferInternals.scala:20)atcom.ifly.edu.scala.list.ListBufferInternals .increase(ListBuffer_Internals.scala:20)
at
优点: 简单
缺点: 当数据过大时,不停创建堆栈,内存消耗大
第二组: 循环
代码:
def main(args: Array[String]) { val data = 1 to 20000 val currntTime =System.currentTimeMillis() increase_for(data.toList) println("used time=" + (System.currentTimeMillis() - currntTime)) } //循环 def increase_for(list:List[Int]) :List[Int] = { var result = List[Int]() for(element <- list){ result = result::: List(element) } result }
运行结果
数据大小为20000
used time=2611
Process finished with exit code 0
数据大小为2000000
used time= NIL (运行很长时间,没有结果),难以忍受
Process finished with exit code 0
优点: 规避递归,数据多少不受什么影响
缺点: 产生很多临时List结果,当数据过大时,效率降低严重
第三组: For 循环 结合map处理
代码
//list 的map function def increase_for2(list:List[Int]) :List[Int] ={ println("list map ") list map(el => el +1) }
运行结果
数据大小:2000000
list map
used time=2268
Process finished with exit code 0
数据大小:2000000
used time=2268
Process finished with exit code 0数据大小:2000000
数据大小:2000000
used time=48356
Process finished with exit code 0
优点: 不产生中间结果,比使用List的::: 方法快
缺点:
第四组: 使用ListBuffer
代码:
def main(args: Array[String]) { val data = 1 to 2000000 val currntTime =System.currentTimeMillis() increase_ListBuffer(data.toList) println("used time=" + (System.currentTimeMillis() - currntTime)) } //listBuffer def increase_ListBuffer(list:List[Int]) :List[Int]={ import scala.collection.mutable.ListBuffer var result = ListBuffer[Int]() for(element <- list){ result += element+1 } result.toList }
运行结果
数据大小为2000000
used time=2284
Process finished with exit code 0
数据大小为20000000
Exception in thread “main” java.lang.OutOfMemoryError: GC overhead limit exceeded
at scala.collection.mutable.ListBuffer.plus eq(ListBuffer.scala:168)
at scala.collection.mutable.ListBuffer.plus eq(ListBuffer.scala:45)
at scala.collection.generic.Growableplusanonfun plus eq1.apply(Growable.scala:48)atscala.collection.generic.Growable anonfun plus pluseq 1.apply(Growable.scala:48)
at scala.collection.immutable.Range.foreach(Range.scala:141)
at scala.collection.generic.Growableclass. plusplus eq(Growable.scala:48)
at scala.collection.mutable.ListBuffer.plus pluseq(ListBuffer.scala:176)atscala.collection.mutable.ListBuffer. plusplus eq(ListBuffer.scala:45)
at scala.collection.TraversableLikeclass.to(TraversableLike.scala:629)atscala.collection.AbstractTraversable.to(Traversable.scala:105)atscala.collection.TraversableOnce class.toList(TraversableOnce.scala:257)
at scala.collection.AbstractTraversable.toList(Traversable.scala:105)
at com.ifly.edu.scala.list.ListBuffer_Internals$.main(ListBuffer_Internals.scala:11)
at com.ifly.edu.scala.list.ListBuffer_Internals.main(ListBuffer_Internals.scala)
优点: 数据在一定量的情况,效率非常高
缺点:
小结
ListBuffer 既可以规避递归,也可以 规避 创建中间结果,效率可靠
参考资料:
百度网盘:http://pan.baidu.com/share/home?uk=4013289088#category/type=0
微信号:18610086859
DT大数据微信公众账号:DT_Spark
DT大数据梦工厂交流群:① 462923555 ②418110145 ③437123764
- 第82讲:Scala中List的ListBuffer是如何实现高效的遍历计算的?
- 第82讲:Scala中List的ListBuffer实现高效的遍历计算
- Scala深入浅出进阶经典 第82讲:Scala中List的ListBuffer是如何实现高效的遍历计算的
- 第82讲:Scala中List的ListBuffer是如何实现高效的遍历计算的?学习笔记
- Scala中List的ListBuffer实现高效的遍历计算
- Scala中使用ListBuffer高效遍历List
- Scala中ListBuffer高效遍历
- Scala深入浅出进阶经典 第84讲:Scala中List和ListBuffer设计实现思考
- 王家林亲授《DT大数据梦工厂》第84讲:Scala中List和ListBuffer设计实现思考
- 第84讲:Scala中List和ListBuffer设计实现思考学习笔记
- 第84讲:Scala中List和ListBuffer设计实现思考
- 第84讲:Scala中List和ListBuffer设计实现思考
- Scala深入浅出进阶经典 第83讲:Scala中List的实现内幕源码揭秘
- 第83讲:Scala中List的实现内幕源码揭秘学习笔记
- 【DT大数据梦工厂视频】《第83讲:Scala中List的实现内幕源码揭秘》
- 第83讲:Scala中List的实现内幕源码揭秘
- scala进阶28-实现List高效遍历
- 第81讲:Scala中List的构造是的类型约束逆变、协变、下界
- CODEVS 1003 并查集缩点+最小生成树
- 树莓派3——摄像头之Apache服务器安装
- ImageView 圆形图片
- javascript对象
- JavaScript学习全栈教程-分享
- 第82讲:Scala中List的ListBuffer实现高效的遍历计算
- HBase shell的常用命令
- MCU低功耗设计(三)产品
- TP实现首页静态方法
- Python学习笔记:Python函数参数
- VC的编译器cl用法初步
- 《Node入门》读书笔记——用Node.js开发一个小应用
- hdu 5392-Infoplane in Tina Town(质因子分解求最小公倍数)
- HDU 2111 Saving HDU(贪心,01背包)