0

Is method B any faster than method A below? I have legacy code written with method A and I need to know if there would be a performance increase if I modified it to use method B or is it just semantics?

Method A (Using pointer)

for(vector<TriggerAlgorithm*>::iterator it = _pTrigs.begin(); it != _pTrigs.end(); ++it)
{
    TriggerAlgorithm* pTrig = *it;   // Pointer to the Trigger
    pTrig->doSomething();            // Use pointer to do something
}

Method B (Using reference)

for(vector<TriggerAlgorithm*>::iterator it = _pTrigs.begin(); it != _pTrigs.end(); ++it)
{
    TriggerAlgorithm& trig = *it;   // Reference to the Trigger
    trig.doSomething();             // Use reference to do something
}

Thanks!

2
Contributors
1
Reply
2
Views
4 Years
Discussion Span
Last Post by Ancient Dragon
0

There is no difference as far as efficiency, references are actually implemented as pointers. The difference is in safety because references can not be changed once set.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.