Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ STL: Container Recreation or Reuse after clearing?

In programming we face various situations where we are required to make use of intermediate STL containers as the following example depicts:

while(true)
{
    set < int > tempSet;

    for (int i = 0; i < n; i ++)
    {
        if (m.size() == min && m.size() <= max)
        {
            tempSet.insert(i);
        }
    }
    //Some condition testing code
}

Or

set < int > tempSet;

while(true)
{
    for (int i = 0; i < n; i ++)
    {
        if (m.size() == min && m.size() <= max)
        {
            tempSet.insert(i);
        }
    }
    tempSet.clear();

    //Some condition testing code
}

Which approach is better in terms of time and space complexity considering the present state of C++ compliers?

like image 242
VarunGupta Avatar asked Oct 19 '08 23:10

VarunGupta


4 Answers

The first version is correct. It is simpler in almost every way. Easier to write, easier to read, easier to understand, easier to maintain, etc....

The second version may be faster, but then again it may not. You would need to show that it had a significant advantage before using it. In most non-trivial cases I would guess that there would not be a measurable performance difference between the two.

Sometimes in embedded programming it is useful to avoid putting things on the stack; in that case the second version would be correct.

Use the first version by default; use the second only if you can give a good reason for doing so (if the reason is performance, then you should have evidence that the benefit is significant).

like image 99
ejgottl Avatar answered Nov 14 '22 07:11

ejgottl


I say the first one is more bug-proof. You're not going to have to remember to clear it (it'll just go out of scope and be done). :-)

Performance-wise there isn't much of a difference, but you should nevertheless measure both cases and see for yourself.

like image 7
Chris Jester-Young Avatar answered Nov 14 '22 08:11

Chris Jester-Young


If this is a question of set/map/list, it won't make any difference. If it is a question of vector/hash_set/hash_map/string, the second will either be faster or the same speed. Of course, this difference in speed will only be noticeable if you are doing a very large number of operations (10,000 ints pushed into a vector 10,000 times was about twice as fast - 3 seconds and some change vs. 7 seconds.).

If you are doing something like storing a struct/class in your data structure instead of a pointer to one, this difference will be even greater because on each resize, you have to copy all of the elements.

Again, in almost every case, it won't matter - and in the cases where it does matter, it will show up if you are optimizing, and you care about every bit of performance.

like image 4
hazzen Avatar answered Nov 14 '22 08:11

hazzen


The second may be slightly better time-wise, but the difference will be extremely minimal -- the code still has to go through and release each of the items in the set, regardless of whether it's re-creating it or clearing it.

(Space-wise they'll be identical.)

like image 2
Head Geek Avatar answered Nov 14 '22 07:11

Head Geek