Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3937786
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:06:33+00:00 2026-05-20T00:06:33+00:00

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

  • 0

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 much worse than the iterative solution written using Java.

Can anyone explain why? And what is the best solution for such code in Scala (I hope it’s not a Java iterative version which is Scala-fied)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T00:06:34+00:00Added an answer on May 20, 2026 at 12:06 am

    The reason that particular code is slow is because it’s working on primitives but it’s using generic operations, so the primitives have to be boxed. (This could be improved if List and its ancestors were specialized.) This will probably slow things down by a factor of 5 or so.

    Also, algorithmically, those operations are somewhat expensive, because you make a whole list, and then make a whole new list throwing a few components of the intermediate list away. If you did it in one swoop, then you’d be better off. You could do something like:

    list collect (case e if (e*2>10) => e*2)
    

    but what if the calculation e*2 is really expensive? Then you could

    (List[Int]() /: list)((ls,e) => { val x = e*2; if (x>10) x :: ls else ls }
    

    except that this would appear backwards. (You could reverse it if need be, but that requires creating a new list, which again isn’t ideal algorithmically.)

    Of course, you have the same sort of algorithmic problems in Java if you’re using a singly linked list–your new list will end up backwards, or you have to create it twice, first in reverse and then forwards, or you have to build it with (non-tail) recursion (which is easy in Scala, but inadvisable for this sort of thing in either language since you’ll exhaust the stack), or you have to create a mutable list and then pretend afterwards that it’s not mutable. (Which, incidentally, you can do in Scala also–see mutable.LinkedList.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This extremely cool article written in the winter of 2007 shows me this code:
There is a semi-famous article written by Guido himself hinting that reduce() should go
I've got the sample code below from an article written by Scott Mitchell on
From this article, hand-written counted loop is about 3x faster than an enhanced for
Yesterday i gone through some article about EventAggregator, there some shot of code written
Following the sample in article http://cocoawithlove.com/2009/05/variable-argument-lists-in-cocoa.html , I've written some custom handling of variable
I'm trying to stand up a Hudson CI server. I'm following an article written
I have seen in many article it is written that Delay signing and strong
I've written an implementation of Bresenham's algorithm in Python (following the Wikipedia article ),
I read recently, in an article on game programming written in 1996, that using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.