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"
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
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.
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.
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. 😕
#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);
@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).
#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.