Skip to content

Tag: performance

Split by regex vs multiple one char splits performance

I compared splitting string by regex and by multiple one char splits, using this benchmark and got these results Why splitting by regex is slower than splitting by multiple individual characters, even though they produce the same result? Note: I ran the code on JDK 14.0.2 I used JMH 1.28 Answer String.split implementation has the optimized fast path for splitting

Function chaining by returning parameter in Java

Seen regularly in the code and quite often used myself mostly to save myself a few lines. so that you can daisy chain the otherwise purely imperative methods in one-liners like as opposed to the classical way There is of course some potential for abuse if you go over board with the daisy chaining like return a(b(c(d(),e(),f(g())), h(i()),j()));, but let’s

short -> int -> long type promotion: is there any overhead?

If, for example, I define method’s return type/parameter as char, but both the caller and implementation actually immediately use it as an int, is there any overhead? If I understand correctly, the values on the stack are 32-bits aligned anyway, as are the ‘registers’ (I’m sorry, I’m not well versed in the byte code). A word of explanation: I am

Count the semiprime numbers in the given range [a..b]

I am solving Codility problem CountSemiprimes: Count the semiprime numbers in the given range [a..b]. Task description A prime is a positive integer X that has exactly two distinct divisors: 1 and X. The first few prime integers are 2, 3, 5, 7, 11 and 13. A semiprime is a natural number that is the product of two (not necessarily