Thomas
Thomas

Reputation: 6196

Which is better for performance in these simple examples?

private bool _invalidated
public void Invalidate()
{
    _invalidated = true;
}

or

public void Invalidate()
{
    if(!_invalidated)
    {
        _invalidated = true;
    }
}

Which would be faster?

This will be called many many times.

Upvotes: 0

Views: 96

Answers (2)

Smarties89
Smarties89

Reputation: 561

As Andreas says, it depends. And as I4V says, paraphrased, it doesn't matter.

I would go for the first, less code and prettier(of course subjective).

Upvotes: 0

Andreas
Andreas

Reputation: 560

The first is less instructions, and contains no conditional, so it's preferable. However, the second would probably be optimized away by a sufficiently smart compiler / JITer.

Upvotes: 1

Related Questions