Browsed by
Tag: benchmark

Benchmark: concatenate lists

Benchmark: concatenate lists

Recently, I came across this piece of code that concatenates two lists using Guava’s Lists utility class and the Stream API: Lists.newArrayList(collectionOriginal,collectionValue) .stream() .flatMap(Collection::stream) .toList(); As this piece of code is in the hot path of the application and therefore called very frequently, I wondered if this was the best way to concatenate two lists. Two lists can be concatenated in many different ways: With the above code With Stream.concat() With Stream.of() With ListUtils from Apache Commons Collection With ArrayList.addAll()…

Read More Read More

Benchmark : conversion from long to byte[]

Benchmark : conversion from long to byte[]

I’ve been using Kafka a lot lately, and in Kafka a lot of things are byte arrays, even headers! As I have many components that exchange messages, I added headers to help with message tracking, including a timestamp header which has the value System.currentTimeMillis(). So I had to transform a long into a byte array; in a very naive way, I coded this: String.valueOf(System.currentTimeMillis()).getBytes(). But instantiating a String each time a header is created does not seem very optimal to…

Read More Read More