The Java logger uses the… Function

See English answer > which of array creation vs string concatenation for logging method has better performance? 3

m_logger.info("ID: {} NAME: {}",id,name);    // 1
m_logger.info("ID: " + id + " NAME: " + name); // 2

In the first case, we basically call the... Function, so each time we create a new object []

I ran past your kit, that's what I saw

My question is, isn't the first case an expensive operation that we should always avoid? But I've seen this in a lot of code What do you get with #1 us?

I think we should use StringBuilder to get the best performance?

Solution

m_logger.info("ID: {} NAME: {}",name);
m_logger.info("ID: {} NAME: {}",name);

This is an argument for laziness If the info logging level is turned off, the string will not be processed In fact, it's not a big deal to construct a new object [n]. N usually (= normal) is < 5

m_logger.info("ID: " + id + " NAME: " + name);

This string is constructed each time

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>