Streams in scala seem like the perfect alternative for lists. They support pretty much every major functionality and they evaluate faster. Why on earth then would one choose to go back to Lists? Is there any special case in which lists would run faster? Is there a case where streams don't run and lists do?
Asked
Active
Viewed 117 times
2 Answers
2
Seems like Streams have a larger memory footprint. Sounds like they keep an extra data cache to perform faster evaluations.
Also, a good reference: Stream vs Views vs Iterators
-
You sacrifice more memory for ideal computations and faster performance. ? – Bula Jun 17 '14 at 20:35
-
Yes. Here's another good reference: http://stackoverflow.com/questions/5159000/stream-vs-views-vs-iterators – SWPhantom Jun 17 '14 at 20:36
-
But this raises the question would you ever want to use Lists if you are not pressured by memory? – Bula Jun 17 '14 at 20:40
-
That's a good question, which sort of answers itself. If you're 100% not constrained by Memory/Computation/Latency/etc, then you should definitely use the data structure that's the fastest. If the data you're working with becomes too big, however, and you start memory thrashing (http://en.wikipedia.org/wiki/Thrashing_(computer_science)), the program will crawl to a stop. – SWPhantom Jun 17 '14 at 20:49
-
3Streams aren't faster, they're *lazy*, it's not the same thing. If you evaluate the *whole* stream, it should be slower than evaluating the corresponding list (because the list version will have less *branches* and sometimes a better cache locality). – ArtemGr Jun 18 '14 at 14:22
-
2+1 to @ArtemGr regarding the laziness of Streams. It's important to pick your data structures based on what behavior you're looking for. Pick a `Stream[T]` if you want to defer evaluation of elements until they're needed. Pick a `Vector[T]` if you plan on accessing it by index or appending to the end. Pick a `List[T]` if you're dealing with a purely functional algorithm that only ever needs to deal with the list in terms of head::tail. Which is to say nothing about `Set` or `Map` or `Stack` or `Queue`. Each data structure is typically optimized for a specific use. – KChaloux Jun 18 '14 at 20:06
1
For this question about avoiding unnecessary computation, I made some benchmarks. Of all the answers, streams had the worst performance by far: 8x slower than Iterator, and more than 2x slower than the next-slowest answer. Streams seem to add a lot of overhead.

Community
- 1
- 1

Ben Kovitz
- 4,920
- 1
- 22
- 50