kth.sePublications
Change search
CiteExportLink to record
Permanent link

Direct link
Cite
Citation style
  • apa
  • ieee
  • modern-language-association-8th-edition
  • vancouver
  • Other style
More styles
Language
  • de-DE
  • en-GB
  • en-US
  • fi-FI
  • nn-NO
  • nn-NB
  • sv-SE
  • Other locale
More languages
Output format
  • html
  • text
  • asciidoc
  • rtf
A Performance Comparison of Java Streams and Imperative Loops
KTH, School of Electrical Engineering and Computer Science (EECS).
2023 (English)Independent thesis Advanced level (degree of Master (Two Years)), 20 credits / 30 HE creditsStudent thesisAlternative title
En prestandajämförelse av Java streams och imperativa loopar (Swedish)
Abstract [en]

The Stream API was added in Java 8. With the help of lambda expressions (anonymous functions), streams enable functional-style operations on sequences of elements. In this project, we evaluate how streams perform in comparison to imperative loops in terms of execution time, from the perspective of how streams are commonly used in public GitHub repositories. Additionally, two algorithms are implemented with and without streams, to assess the impact of stream usage on algorithmic performance. Parallel streams are only examined briefly due to their infrequent usage. We find that sequential streams in general are slower than imperative loops. However, stream performance heavily relies on how many elements are being processed, which is referred to as input size. For input sizes smaller than 100, most stream pipelines are several times slower than imperative loops. Meanwhile, for input sizes between 10 000 and 1 000 000, streams are on average only 39% to 74% slower than loops, and in some cases, they even slightly outperform them. Additionally, we observe that using streams when implementing algorithms in some cases leads to much slower execution times, while in other cases, it barely affects the execution time at all. We conclude that stream performance primarily depends on input size, presumably because of the high overhead abstraction cost of creating streams, but their performance also depends on other factors, such as operation type and pipeline length.

Abstract [sv]

Med Java 8 introducerades streams. Med hjälp av lambda-uttryck (anonyma funktioner) möjliggör streams användandet av funktionella operationer på sekvenser av element. I detta projekt mäter vi hur streams presterar i jämförelse med imperativa loopar med hänsyn till exekveringstid, från perspektivet av hur streams vanligen används i publika GitHub-projekt. Parallella streams undersöks endast i begränsad utsträckning, på grund av hur sällan de används. Resultaten visar att streams överlag är långsammare än imperativa loopar. Skillnaden i prestanda beror dock starkt på indatastorleken, det vill säga hur många element som streamen bearbetar. För indatastorlekar mindre än 100 element är streams ofta flera gånger långsammare än deras imperativa motsvarigheter. Samtidigt är streams i genomsnitt endast 39% till 74% långsammare än imperativa motsvarigheter för indatastorlekar mellan 10 000 och 1 000 000 element, och i några fall är de till och med något snabbare än imperativ kod. Vidare observerar vi att användning av streams vid implementation av algoritmer i vissa fall leder till mycket längre exekveringstider, medan det i andra fall knappt påverkar exekveringstiden alls. Vi drar slutsatsen att prestandan av streams främst beror på indatastorlek, men också på andra faktorer, såsom operationstyp och hur många operationer som används i en pipeline.

Place, publisher, year, edition, pages
Stockholm: KTH Royal Institute of Technology , 2023. , p. 70
Series
TRITA-EECS-EX ; 2023:495
Keywords [en]
Java streams, performance, benchmarking, loops, functional programming
Keywords [sv]
Java streams, prestanda, loopar, funktionell programmering
National Category
Computer Sciences Computer Engineering Computer and Information Sciences
Identifiers
URN: urn:nbn:se:kth:diva-332122OAI: oai:DiVA.org:kth-332122DiVA, id: diva2:1783234
Subject / course
Computer Science
Educational program
Master of Science in Engineering - Computer Science and Technology
Supervisors
Examiners
Available from: 2023-08-03 Created: 2023-07-19 Last updated: 2023-08-03Bibliographically approved

Open Access in DiVA

fulltext(1492 kB)2719 downloads
File information
File name FULLTEXT01.pdfFile size 1492 kBChecksum SHA-512
8820ba0ef9c4b18839224c1c1f5b0a6e959efc9e5f10eac9cffcf33e223cd7ea0221ece67c90dc65276eb49db2a14f7b650ab67519a6523c3caf4a42b158ec5c
Type fulltextMimetype application/pdf

Search in DiVA

By author/editor
Åkerfeldt, Magnus
By organisation
School of Electrical Engineering and Computer Science (EECS)
Computer SciencesComputer EngineeringComputer and Information Sciences

Search outside of DiVA

GoogleGoogle Scholar
Total: 2721 downloads
The number of downloads is the sum of all downloads of full texts. It may include eg previous versions that are now no longer available

urn-nbn

Altmetric score

urn-nbn
Total: 3051 hits
CiteExportLink to record
Permanent link

Direct link
Cite
Citation style
  • apa
  • ieee
  • modern-language-association-8th-edition
  • vancouver
  • Other style
More styles
Language
  • de-DE
  • en-GB
  • en-US
  • fi-FI
  • nn-NO
  • nn-NB
  • sv-SE
  • Other locale
More languages
Output format
  • html
  • text
  • asciidoc
  • rtf