Viktar Patotski Profile picture
Feb 11 8 tweets 3 min read
⚠️#Java performance tip⚠️
Use primitive types instead of wrappers.

Java primitive types:
📍byte
📍short
📍int
📍long
📍float
📍double
📍char
📍boolean

More details and explanation in 🧵👇
To show the difference in performance between primitive and wrapper objects I have created a benchmark which just creates an array of "int" and "Integer" and fills them with an index. Sizes of array: "10", "100", "1000", "10000"

Results: 👇
Array of size 10:
❌ arrayOfWrappers 26.131 ns/op - 2.7x slower
✅ arrayOfPrimitives 9.462 ns/op
Array of size 100:
❌ arrayOfWrappers ns/op - 2.7x slower
✅ arrayOfPrimitives 9.462 ns/op
Array of size 1000:
❌ arrayOfWrappers 26.131 ns/op - 2.7x slower
✅ arrayOfPrimitives 9.462 ns/op
Why primitive is more effective? Reasons:
- int requires 4 bytes, while Integer 16
- Integer is an object and creating objects is expensive
- primitive types reside in stack and fast to access, while objects are stored in heap.

🤓
That's a wrap!

If you enjoyed this performance tip:

1. Follow me @xpvit for more Java, Cloud, and Linux knowledge.
2. RT the tweet below to share this thread with your audience
Code can be found as usually on my GitHub: github.com/xp-vit/java-lo…

• • •

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 10
The Scout Rule

Always leave the code better than you found it.

-- (Robert C. Martin (Uncle Bob))
Based on the "Scout Rule", which is "always leave the campground cleaner than you found it", the Scout Rule in programming is simply "always leave the code cleaner than you found it".

This was introduced in the first chapter of the book Clean Code by Bob Martin.
The rule suggests that developers should perform 'optimistic refactoring', which means to endeavour to improve the overall quality of the code when you work on it if you see a mistake, attempt to fix it or clean it up.
Read 4 tweets
Feb 8
The Law of Triviality

This law suggests that groups will give far more time and attention to trivial or cosmetic issues rather than serious and substantial ones.
The common fictional example used is that of a committee approving plans for nuclear power plant, who spend the majority of their time discussing the structure of the bike shed, rather than the far more important design for the power plant itself.
It can be difficult to give valuable input on discussions about very large, complex topics without a high degree of subject matter expertise or preparation. However, people want to be seen to be contributing valuable input.
Read 6 tweets
Feb 4
⚠️#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.
Read 11 tweets
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

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!

:(