Is repeatedly instantiating an anonymous class wasteful?

后端 未结 3 720
借酒劲吻你
借酒劲吻你 2021-02-14 02:32

I had a remark about a piece of code in the style of:

Iterable upperCaseNames = Iterables.transform(
    lowerCaseNames, new Function

        
3条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-14 02:52

    Short answer: No - don't worry.

    Long answer: it depends how frequently you're instantiating it. If in a frequently-called tight loop, maybe - though note that when the function is applied it calls String.toUpperCase() once for every item in an Iterable - each call presumably creates a new String, which will create far more GC churn.

    "Premature optimization is the root of all evil" - Knuth

提交回复
热议问题