Re: about new and delete
Sam <s...@email-scan.com> wrote:
I'm sorry that I disappointed you, by failing to come up with the
meaningless results that you were so much looking for.
I wouldn't use the word "disappointing". But the I'm-righter-than-you-
are-and-proud-of-it attitude we can see here isn't going to help
anybody. It certainly doesn't improve your popularity.
Sure thing, Einstein. After increasing the number of iterations by a fact=
or
of ten, the resulting run times for std::list are:
[mrsam@lc2440 tmp]$ time ./t
real 0m2.195s
user 0m2.099s
sys 0m0.000s
[mrsam@lc2440 tmp]$ time ./t
real 0m2.177s
user 0m2.080s
sys 0m0.004s
[mrsam@lc2440 tmp]$ time ./t
real 0m2.245s
user 0m2.080s
sys 0m0.008s
And for std::vector:
[mrsam@lc2440 tmp]$ time ./t
real 0m3.134s
user 0m2.995s
sys 0m0.002s
[mrsam@lc2440 tmp]$ time ./t
real 0m3.113s
user 0m2.989s
sys 0m0.004s
[mrsam@lc2440 tmp]$ time ./t
real 0m3.356s
user 0m2.983s
sys 0m0.010s
Gee, that makes std::vector look so much better!
I'm not really sure what you did. After some testing with G++ 4.3.1
WITHOUT fancy custom allocators or any kind of cheating (like using
vector<>::reserve, vector<>::clear) I came to the conclusion that if
you don't need special list operations that are known to be fast
(splicing, insert, ... etc) a vector is both faster and consumes less
memory. No surprize. Ok, I didn't test the memory consumption part but
I think it should be obvious why this is -- even counting the
temporary memory usage a vector needs when it grows. Each node
consumes at least one pointer in addition (the XOR trick makes an
iterator slightly larger though) and there's the per heap-element
allocation overhead. For testing the speed I created & destroyed an
int-container 5000 times and each time filled it (push_back) with
10.000 elements and later iterated over the elements to compute the
sum. Here are my results:
----------8<---------- list ----------8<----------
sg@home:~/ccpp/heap$ time ./a.out l
real 0m3.332s
user 0m3.228s
sys 0m0.004s
sg@home:~/ccpp/heap$ time ./a.out l
real 0m2.976s
user 0m2.956s
sys 0m0.000s
sg@home:~/ccpp/heap$ time ./a.out l
real 0m3.985s
user 0m3.860s
sys 0m0.004s
----------8<---------- vector ----------8<----------
sg@home:~/ccpp/heap$ time ./a.out v
real 0m0.261s
user 0m0.260s
sys 0m0.000s
sg@home:~/ccpp/heap$ time ./a.out v
real 0m0.306s
user 0m0.280s
sys 0m0.004s
sg@home:~/ccpp/heap$ time ./a.out v
real 0m0.276s
user 0m0.276s
sys 0m0.000s
----------8<--------------------8<----------
I think it's a fair comparison. The use case is similar to the one of
the OP. Again: No tricks, For example, I created and destroyed the
containers 5000 times so that std::vector could not reuse its
allocated memory.
n.clear();
Bzzzt. You just got caught cheating.
As you see, even without clear, reserve (which by the way is
appropriate in the OP's use case) the rather naive program using
vectors outperforms the one which uses std::list by a factor of about
10 in excecution speed.
I have trouble explaining the discrepancy between your results and
mine -- especially since you seem to be using G++ as well. I'm not
saying you're trolling. But it would explain it at least. Maybe you
have a better explanation. I'm all ears.
Thank you for playing. You can go home, now.
Gee, you really know how to talk to people!
Cheers,
SG