Declaring variables in loops.

Started by
15 comments, last by maximAL 18 years, 2 months ago
Quote:Original post by Thevenin
Quote:Original post by ender7771
When you declare x and then pass it as an argument, you are only passing the value, not the location. It doesn't matter where it was declared.


Yuppers, but is there a performance hit for putting it in the for loop.

I know that the C# compiler will optimize just about everything you write, but that doesn't always produce an acceptable solution; this part of the code is in the heart of my graphics procedures so I'm not even worried about "premature optimization".


Java (and I would assume C#/.NET) uses a generational garbage collector that is heavily optimized for very short-lived objects, those being the most common.

So while I would say that if it's simple to reuse the object, I would, but I wouldn't be too concerned about it unless you really were trying to squeeze the last bit of performance out of the loop - in which case you can find out by trying both and see which is faster, which will give you a conclusive answer.
Advertisement
Quote:Original post by Skeleton_V@T
If [the object is mutable], my choice depends on the complexity of the constructing object:
  • For fundamental types, I'd prefer putting it inside the loop.
  • For other types that are more complex than Vector3, I'd prefer putting it outside.

  • For objects that are more 'complex' than simple Vectors, you've got to choose based on more than construction. If your assignment semantics are more expensive than construction/destruction, and efficiency of the loop is a concern, declaring/initializing the object inside the loop would be preferrable. Often this isn't the case, but it probably should be pointed out anyway.
    :stylin: "Make games, not war.""...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid
    holy hell, how often do you guys need "premature optimization" yelled at you?

    variables are declared as local as possible <PERIOD>
    everything else is ugly performance tweaking, thats the last thing to worry about. and the compiler will most likely optimize it anyway.
    ------------------------------------------------------------Jawohl, Herr Oberst!
    Quote:Original post by Ra
    In either case (primitive or non-primitive type) the compiler should will optimize it.
    Non-primitive type's constructor may have such side-effects that this optimization would lead to different behaviour.
    Quote:Original post by maximAL
    holy hell, how often do you guys need "premature optimization" yelled at you?

    variables are declared as local as possible <PERIOD>
    everything else is ugly performance tweaking, thats the last thing to worry about. and the compiler will most likely optimize it anyway.
    Who are you to say whether the original poster is optimizing prematurely or not? OP even said: "this part of the code is in the heart of my graphics procedures so I'm not even worried about "premature optimization"." Sometimes ugly performance tweaking is necessary.
    Quote:Original post by maximAL
    holy hell, how often do you guys need "premature optimization" yelled at you?

    Believe it or not, there are those that read this forum for which understanding how your objects are created/assigned/destroyed is not premature at all. Rather, I'd suggest that blanket adherence to one particular method, without regard for, IMO, important issues such as those I mentioned above is much worse than premature optimization (for built-in types, this is of course a non-issue, and has nothing to do with optimization - although there are certain circumstances where the state of an uninitialized variable is a concern).
    :stylin: "Make games, not war.""...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid
    i didn't just mean the op. there are other posts, too, promoting thoughtless use of that.
    ------------------------------------------------------------Jawohl, Herr Oberst!

    This topic is closed to new replies.

    Advertisement