Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #6586
| From | Edek <edek.pienkowski@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: std::set preallocate? |
| Date | 2011-06-10 22:57 +0200 |
| Organization | ATMAN - ATM S.A. |
| Message-ID | <isu0g7$tcs$1@node2.news.atman.pl> (permalink) |
| References | <8bb2eabb-fd3f-4355-9704-d430ce964f8d@m10g2000yqd.googlegroups.com> |
On 06/10/2011 10:01 PM, Christopher wrote:
> Got a complaint that allocation is costly for 2000 items to be
> inserted into a set one by one. I need to preallocate, and depend on
> the set growing, if needed, after the number of items exceeds the
> preallocation.
>
> Do I do so like this?
>
> class A
> {
> public:
> A()
> {
> m_values.get_allocator.allocate(2000);
> }
> ~A()
> {
> // Does set still clean up after itself?
> }
>
> typedef std::pair<unsigned, std::smart_ptr> Value;
> typedef std::set<Value> Values;
> Values m_values;
> }
No, not really. Allocators have something called 'rebind'.
Basically a set gets an allocator of T, and rebinds it to allocator
of its own internal type to carry its [meta]data.
Since these are templates, the two allocators differ in c++ type.
If you think you can outdo the standard allocators, you need to
invest into allocator requirements first, to get the formalities right;
then you need to make your allocator perform well. Take a look
at new_allocator (simple allocator just calling ::new).
Maybe there are other ways, but if you know the characteristics
of this set's usage, e.g. a set will contain 2000 elements,
then they will be mainly constant in size, you could optimise by
preallocating. Or preallocating in chunks as needed.
It's just a bit too hairy templated stuff to do so for no reason,
and not so easy to get a really good performance out of it.
Explore first; [1] is a rather easy way to start.
BTW, do you need a set? Maybe a hashset? Maybe a vector?
Edek
[1] http://www.boost.org/doc/libs/1_44_0/libs/pool/doc/interfaces.html
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
std::set preallocate? Christopher <cpisz@austin.rr.com> - 2011-06-10 13:01 -0700
Re: std::set preallocate? Pete Becker <pete@versatilecoding.com> - 2011-06-10 10:32 -1000
Re: std::set preallocate? Edek <edek.pienkowski@gmail.com> - 2011-06-10 22:57 +0200
Re: std::set preallocate? Christopher <cpisz@austin.rr.com> - 2011-06-10 14:51 -0700
Re: std::set preallocate? Carlo Milanesi <carlo.nospam.milanesi@libero.it> - 2011-06-11 00:42 +0200
Re: std::set preallocate? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-06-11 10:44 +0000
Re: std::set preallocate? Edek <edek.pienkowski@gmail.com> - 2011-06-11 18:14 +0200
Re: std::set preallocate? Juha Nieminen <nospam@thanks.invalid> - 2011-06-11 04:52 +0000
csiph-web