Declare an object inside or outside a loop?

前端 未结 16 853
广开言路
广开言路 2020-12-07 17:15

Is there any performance penalty for the following code snippet?

for (int i=0; i         


        
16条回答
  •  余生分开走
    2020-12-07 18:02

    These have different semantics. Which is more meaningful?

    Reusing an object for "performance reasons" is often wrong.

    The question is what does the object "mean"? WHy are you creating it? What does it represent? Objects must parallel real-world things. Things are created, undergo state changes, and report their states for reasons.

    What are those reasons? How does your object model and reflect those reasons?

提交回复
热议问题