Sunday, 28 October 2018

Sequence performance





I am frequently getting getting warnings like this from Intellij Idea in kotlin projects:






And if you accept the hint, Intellij will change this into


So what Idea recommends is that if you are performing two or more computations on collections, then you should turn the collection into a sequence and do the computations on those.

But...

Is this always true?


Apparently not. When you transform one list into another list, sequence's seem to consistently under-perform the usual List operations.







When is it right?


When at the end you have to get another list, it does not seem to be a great choice But when you do not want to create another list, you just want to get a sum or avg, that is when it seems to perform always better.



And yes in this case, the transformed code does perform better.





So, having no confirmation of the performance improvement, for now I keep ignoring these hints for the most of my code-base.

No comments:

Post a Comment