Scala performance question

后端 未结 7 1233
Happy的楠姐
Happy的楠姐 2021-02-03 11:45

In the article written by Daniel Korzekwa, he said that the performance of following code:

list.map(e => e*2).filter(e => e>10)

is muc

相关标签:
7条回答
  • 2021-02-03 12:15

    Basically it's traversing a list twice. Once for multiplying every element with two. And then another time to filter the results.

    Think of it as one while loop producing a LinkedList with the intermediate results. And then another loop applying the filter to produce the final results.

    This should be faster:

    list.view.map(e => e * 2).filter(e => e > 10).force
    
    0 讨论(0)
提交回复
热议问题