Viktar Patotski Profile picture
Feb 4 11 tweets 3 min read
⚠️#Java performance tip⚠️

Don't use "+" for string concatenation. Use StringBuilder or StringBuffer instead.
I have created a benchmark to compare the speed of StringBuilder, StringBuffer and "+" concatenation for 2, 3, 5, 10, 50, and 100 strings.
Details in 🧵👇
The scenario is simple:
s/sb = ""/new StringBuffer/Builder();
for (int i = 0; i < size; i++) {
s+=i;/sb.append(i);
}
return sb.toString();

size = 2, 3, 5, 10, 50, and 100

Size hundred produced super strange result, which I can't explain yet. 😕
Concat 2 strings:
❎ s+= i; 25.528 ns/op ~15% slower
✅ stringBuilder.append(i); 21.265 ns/op
✅ stringBuffer.append(i); 21.251 ns/op

The difference is slight, so it is up to you to decide what to use. However, if you want the lowest latency, go with StringBuilder.
Concat 3 strings:
❌ s+= i; 42.559 ns/op ~40% slower
✅ stringBuilder.append(i); 25.461 ns/op
✅ stringBuffer.append(i); 25.810 ns/op

StringBuffer and StringBuilder are 40% faster then "+". 😀
Concat 5 strings:
❌ s+= i; 75.137 ns/op ~2x slower
✅ stringBuilder.append(i); 34.940 ns/op
✅ stringBuffer.append(i); 35.930 ns/op

WoW, StringBuffer and Builer concatenate 2x faster. And we concatenated only 5 strings. 😮
Concat 10 strings:
❌ s+= i; 157.276 ns/op 3x slower
✅ stringBuilder.append(i); 64.173 ns/op ~ 8% slower
✅ stringBuffer.append(i); 65.941 ns/op

10 strings concatenated. StringBuffer and StringBuilder Are 3x better than `s+=i;` 😎
Concat 50 strings:
❌ s+= i; 812.848 3x slower
✅ stringBuilder.append(i); 283.244 ns/op
✅ stringBuffer.append(i); 308.129 ns/op

It seems like we found pattern.
Concat 100 strings:
❌s+= i; 2090 ns/op 3.5x slower
✅ stringBuilder.append(i); 595 ns/op
❌stringBuffer.append(i); 2272 ns/op 3.8x slower

WHAT? 😱 How? I have no explanation at this time.
In general, explanation is: "s+= i;" is slow because the String instance is immutable in Java. Therefore it always creates a new String and copies s1 and s2. StringBuilder/Buffer are mutable. However, SBuffer has all methods synchronized, making it slower than Builder. 🤓
🤯 However, I have no idea how to explain outlier produced by 100. It's reproducible even in non parametrized benchmark and definitely a subject for future investigation and tweets.

Source as always can be found in: github.com/xp-vit/java-lo…
That's a wrap!

If you enjoyed this thread:

1. Follow me @xpvit for more Java, Cloud, and Linux knowledge.
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 2
#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 ‼️
Read 10 tweets
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

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!

:(