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. 😕
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.
#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.
⚠️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.
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?
👮🏼♀️ 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.