Re: about new and delete
In article <cone.1253929234.918558.24568.500@commodore.email-
scan.com>, sam@email-scan.com says...
Alf P. Steinbach writes:
No.
Can you figure out what's wrong with your measurement?
Yes: nothing.
Rather the contrary -- there's quite a lot wrong with it. First of
all, what you originally claimed was supposed to be done was not just
insert some data into the collection, but iterate over the collection
to print out the items. Your test doesn't iterate over the items to
print them out, OR do anything else with them.
Second, since you've done nothing with the contents of the
collection, it's entirely possible (in fact rather likely, if you're
using a good compiler) that most of what you've done is being
optimized away completely. I can't say exactly how much the
particular compiler you're using happens to do in that direction, but
in the end it doesn't matter -- a meaningful benchmark has to
_ensure_ that it's measuring what it's supposed to, and yours does
nothing of the sort.
Third, you're creating an object (either the vector or list) inside
outer loop, so there's a good chance that the creation time is
affecting your results.
Fourth, you're measuring the time for the program as a whole instead
of even attempting to measure it for the part you care about.
Fifth, the total time taken by either program is small enough that
there's little certainty that the times you're getting mean much of
anything. To mean much, you need to increase the total time taken by
(for example) increasing the number of iterations in your outer loop.
Those aren't really the only problems, but they give some idea of how
far off the mark your test really is.
Now, there is one minor problem: if we actually print out the
contents, that printing will almost inevitably dominate the overall
time -- to the point that what we care about will probably be lost in
the noise. At the same time, we do want to ensure that the contents
of the collection is actually _used_, so the work being done can't be
optimized away. One way to meet both of these requirements is to
summarize the contents of the collection (e.g. add up all the values)
and only print out the sum.
One possible outcome of dealing with those glaring problems would be
code like this:
#include <vector>
#include <list>
#include <time.h>
#include <iostream>
#include <numeric>
int main()
{
std::vector<int> n;
clock_t start = clock();
for (size_t j=0; j<900000; j++)
{
n.clear();
for (size_t i=0; i<10; i++)
n.push_back(i);
}
int total = std::accumulate(n.begin(), n.end(), 0);
clock_t total_time = clock()-start;
std::cout << total << "\n";
std::cout << total_time << "\n";
return (0);
}
Three runs of this gives:
D:\C\source>time_lst
45
46
D:\C\source>time_lst
45
46
D:\C\source>time_lst
45
46
Switching to list gives:
D:\C\source>time_lst
45
1435
D:\C\source>time_lst
45
1435
D:\C\source>time_lst
45
1419
There's still a bit of jitter in the timing, but it's of little
relevance -- either way, vector is quite a bit faster. Of course the
exact ratio also depends on the compiler and library. Trying with the
compilers I have handy, list does a bit better than shown above with
some of them -- but even at best, it's still about 15 times slower
than vector.
--
Later,
Jerry.