Why aren't my scala futures more efficient?

后端 未结 2 2092
余生分开走
余生分开走 2021-02-12 23:35

I\'m running this scala code on a 32-bit quad-core Core2 system:

def job(i:Int,s:Int):Long = {
  val r=(i to 500000000 by s).map(_.toLong).foldLeft(0L)(_+_)
  pr         


        
2条回答
  •  南旧
    南旧 (楼主)
    2021-02-13 00:22

    Try

    (i to 500000000 by s).view.map(_.toLong).foldLeft(0L)(_+_)
    

    The application of view is supposed to (as I understood id) to avoid repeated iteration and object creation by providing simple wrappers.

    Note also that you can use reduceLeft(_+_) instead of fold.

提交回复
热议问题