Viktar Patotski Profile picture
Feb 2 10 tweets 3 min read
#Java performance Tip: `parallelStream()` is way slower than regular `stream()` on small amount of data. I have created a simple benchmark and run it on my 8-CPU laptop. The difference is 160x for some cases. 😲

Details 🧵 👇
👷The setup is to run the same code for streams of 10, 1000, 10_000, 100_000, and 1000_000 items long in standard and parallel modes:
list.stream()
.filter(i -> i == -1)
.findAny()
.orElse(0);
10 items stream:
✅stream()
❌parallelStream() 161x SLOWER ‼️
1000 items stream:
✅stream()
❌parallelStream() 7x SLOWER
10_000 items stream:
✅stream()
❌parallelStream() 1.5x SLOWER
100_000 items stream (winner change):
❌stream() 1.5x SLOWER
✅parallelStream()
1000_000 items stream (winner changed):
❌stream() 2.4x SLOWER
✅parallelStream()
Are you wondering why it is so?
When running a parallel stream, the code starts multiple threads and splits data across them. Starting threads is not a very cheap operation; therefore, on smaller streams, parallel processing is slower than gains from multi-threading.
Note, that stream sizes where classical/parallel performs better should not be taken as is to your code. These numbers depend a lot on:
- CPU
- complexity of operations your stream performs

As always code could be found in my GitHub:

github.com/xp-vit/java-lo…
That's a wrap!

If you enjoyed this thread:

1. Follow me @xpvit for more of these
2. RT the tweet below to share this thread with your audience

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Viktar Patotski

Viktar Patotski Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @xpvit

Feb 1
The Law of Leaky Abstractions

"All non-trivial abstractions, to some degree, are leaky."

by @spolsky

joelonsoftware.com/2002/11/11/the…
@spolsky This law states that abstractions, which are generally used in computing to simplify working with complicated systems, will in certain situations 'leak' elements of the underlying system, this making the abstraction behave in an unexpected way.
@spolsky An example might be loading a file and reading its contents. The file system APIs are an abstraction of the lower level kernel systems, which are themselves an abstraction over the physical processes relating to changing data on a magnetic platter (or flash memory for an SSD).
Read 7 tweets
Jan 31
#Java performance Tip: When need to copy items from one array to anoyther one, don't use loops. Use the more efficient Arrays.copyOf or System.arraycopy method instead.

Performance test details in 🧵👇
Given:
☕ int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30};

❌ int[] arr2 = new int[30];
for (int i = 0; i < arr.length; i++) {
arr2[i] = arr[i];
}
✅ int[] arr2 = new int[30];
System.arraycopy(arr, 0, arr2, 0, arr.length);

~25% faster ⚡
Read 6 tweets
Jan 12
⚠️Thread alert! ⚠️
Relational databases are still the most popular data storage for various projects. Here are some tips on how to scale relational databases to support more throughput, serve more clients, or get faster query processing. 🧵:
1. Update the version
Newer versions of traditional SQL databases typically come with performance improvements. Even if the newer database system is not faster as a direct replacement, there might be new features available that we can take advantage of.
2. Indexing
Properly indexing the database can significantly improve query performance and reduce the load on the database. Be sure to index columns that are used for FK joins or that are frequently searched or sorted.
Read 18 tweets
Jan 10
Functional correctness of software is essential, but Non-Functional Requirements (NFRs) are usually forgotten during development. Here are the top 10 crucial NFRs you should care about 🧵 👇
1. Performance
Focuses on the system's speed, efficiency, and workload. It covers how fast the system should process requests and respond to them.
2. Scalability
Can the system respond to changes in demand? How will the system pull on additional resources to handle the additional load?
Read 12 tweets
Jan 4
👮🏼‍♀️ FBI: "Cyber Criminals Impersonating Brands Using Search Engine Advertisement Services to Defraud Users"

The FBI recommends individuals take the following precautions: 🧵👇
📍Before clicking on an advertisement, check the URL to make sure the site is authentic. A malicious domain name may be similar to the intended URL but with typos or a misplaced letter.
📍Rather than search for a business or financial institution, type the business’s URL into an internet browser’s address bar to access the official website directly.
Read 6 tweets
Jan 3
Top 6 free @intellijidea plugins you don't want to miss. 🧵 👇
Key Promoter X - The Key Promoter X helps you learn essential shortcuts while working. When you use the mouse on a button inside the IDE, the Key Promoter X shows you the keyboard shortcut you should have used instead.

Absolute Must❗
👉 rli.to/UDW0z
Grep Console - plugin which allows grepping, tail, filtering, and highlighting any output in the console. It can do everything you need.

👉 rli.to/2piFi Image
Read 8 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(