Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > What is faster? C++ vector sort or sort in database

Reply
Thread Tools

What is faster? C++ vector sort or sort in database

 
 
Siemel Naran
Guest
Posts: n/a
 
      04-28-2004
"Dave Moore" <(E-Mail Removed)> wrote in message

> Instead of std::vector, why not use a std::map<Key, record>, where key
> is the data to be used as a sorting criterion. If the compare
> operation is already defined (like for a built-in data type), then you
> can just use subscripting or the insert function to make the map,
> which will automatically be sorted. If you need to define a user
> specific compare operation, you can specify the appropriate functor as
> a template parameter or ctor argument.


But lookup and iteration on a sorted vector is faster than on a map. If
you're doing a lot of lookup, but not much inserting of new rows (or you are
inserting lots of new rows but don't care that the new rows are sorted which
is IMHO acceptable), then vector is probably faster.


> Also, I don't know anything about SQL's interaction with C++ in
> particular, but if it is possible to get only pointers to the records
> (IOW std::map<Key, record *>), that will avoid copying the records,
> which will probably save time.
>
> Oh .. I just re-read your post and saw you said "values from the
> database" ... if you really just want an ordered list of comparable
> values, you can use std::set (instead of std::map) as above ... this
> might even be more efficient.
>
> HTH, Dave Moore



 
Reply With Quote
 
 
 
 
Dave Moore
Guest
Posts: n/a
 
      04-28-2004
"Siemel Naran" <(E-Mail Removed)> wrote in message news:<LvQjc.58845$(E-Mail Removed)>...
> "Dave Moore" <(E-Mail Removed)> wrote in message
>
> > Instead of std::vector, why not use a std::map<Key, record>, where key
> > is the data to be used as a sorting criterion. If the compare
> > operation is already defined (like for a built-in data type), then you
> > can just use subscripting or the insert function to make the map,
> > which will automatically be sorted. If you need to define a user
> > specific compare operation, you can specify the appropriate functor as
> > a template parameter or ctor argument.

>
> But lookup and iteration on a sorted vector is faster than on a map. If
> you're doing a lot of lookup, but not much inserting of new rows (or you are
> inserting lots of new rows but don't care that the new rows are sorted which
> is IMHO acceptable), then vector is probably faster.
>


Ok, I hadn't thought about that ... one reason I choose std::map's for
this sort of thing is because pointers and references are not
invalidated when elements are inserted to or erased from it. That way
my std::map<Key, record *> can grow and shrink without worrying about
the pointers being clobbered. I typically use technique for building
associative arrays of fairly large objects, where copying is
expensive. However my arrays never get that big, so that is probably
why I don't notice performance hits for lookup and/or iteration.

Thanks for the clarification,

Dave Moore
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Database Database Database Database scott93727@gmail.com Computer Information 0 09-27-2012 02:43 AM
DataBase DataBase DataBase DataBase scott93727@gmail.com Computer Information 0 09-26-2012 09:40 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Initializing vector<vector<int> > and other vector questions... pmatos C++ 6 04-26-2007 05:39 PM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM



Advertisments