Connecting Tech Pros Worldwide Forums | Help | Site Map

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

JerryJ
Guest
 
Posts: n/a
#1: Jul 22 '05
I have to fetch values from a database and store them in a vector in
my c++ application.

What is faster? if i fetch the values sorted from the database with a
'order by' clause in the sql-statement or if i fetch them into the
vector unsorted and sort the vector then?


Thanx for your help


Greetz

Jens

John Harrison
Guest
 
Posts: n/a
#2: Jul 22 '05

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



"JerryJ" <jerryj@gmx.de> wrote in message
news:e9013551.0404240119.8a52dbf@posting.google.co m...[color=blue]
> I have to fetch values from a database and store them in a vector in
> my c++ application.
>
> What is faster? if i fetch the values sorted from the database with a
> 'order by' clause in the sql-statement or if i fetch them into the
> vector unsorted and sort the vector then?
>
>
> Thanx for your help
>[/color]

There is only one way to answer that, try them both and time it.

john


Mike Wahler
Guest
 
Posts: n/a
#3: Jul 22 '05

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



"JerryJ" <jerryj@gmx.de> wrote in message
news:e9013551.0404240119.8a52dbf@posting.google.co m...[color=blue]
> I have to fetch values from a database and store them in a vector in
> my c++ application.
>
> What is faster? if i fetch the values sorted from the database with a
> 'order by' clause in the sql-statement or if i fetch them into the
> vector unsorted and sort the vector then?[/color]

The faster method will be the one which consumes
the least amount of time. Only one way to determine
that.

-Mike


Julie
Guest
 
Posts: n/a
#4: Jul 22 '05

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


JerryJ wrote:[color=blue]
>
> I have to fetch values from a database and store them in a vector in
> my c++ application.
>
> What is faster? if i fetch the values sorted from the database with a
> 'order by' clause in the sql-statement or if i fetch them into the
> vector unsorted and sort the vector then?
>
> Thanx for your help
>
> Greetz
>
> Jens[/color]

That question can not be generally answered, there are just too many external
factors that can determine performance.

Your only option is to examine the performance empirically between each method
and make a decision at that point.
Mateusz Łoskot
Guest
 
Posts: n/a
#5: Jul 22 '05

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


On 4/24/2004 11:19 AM, JerryJ wrote:[color=blue]
> What is faster? if i fetch the values sorted from the database with a
> 'order by' clause in the sql-statement or if i fetch them into the
> vector unsorted and sort the vector then?[/color]

I'm not going to answer your question directly, but
I'd do use 'order by' in SQL statement rather than vector sort function.
You may increase performance creating some indexes in your database
(indexes are one of the factor influencing the performance).

If you are creating client-side solution and your application
queries database working on other machine it would be better
to sort results on the 'server-side' using SQL.

Greets

--

Mateusz Łoskot
mateusz at loskot dot net
Cy Edmunds
Guest
 
Posts: n/a
#6: Jul 22 '05

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


"JerryJ" <jerryj@gmx.de> wrote in message
news:e9013551.0404240119.8a52dbf@posting.google.co m...[color=blue]
> I have to fetch values from a database and store them in a vector in
> my c++ application.
>
> What is faster? if i fetch the values sorted from the database with a
> 'order by' clause in the sql-statement or if i fetch them into the
> vector unsorted and sort the vector then?
>
>
> Thanx for your help
>
>
> Greetz
>
> Jens[/color]

I agree with the other posters who said to time it, but in general I would
say that if you have a decent database program it should not be necessary to
duplicate basic SQL functionality for performance reasons. If it *does*
prove necessary, change database programs.

--
Cy
http://home.rochester.rr.com/cyhome/


Rakesh Kumar
Guest
 
Posts: n/a
#7: Jul 22 '05

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


There are many factors out here -

i) Where is your DB server ? Is it on a different machine than yours.
When you get the values and use vector, you are doing it on the DB
client, whereas executing DB queries would mean you are doing things on
the server.
Generally , the server is much more powerful ( people invest more on
servers than on clients). Hence it would be better to do the task on the
DB itself, rather than in C++ .


ii) Assuming still that the DB and the C++ code run on the same machine,
it would still make sense to have it on the DB, since the DB engine
generally comes up with optimizations to improve locality of reference
when they execute the code ( to reduce 'thrashing' in particular).
While, of course, that could be done in C++ , you need to decide if
the time spent on that is worth investing.

HTH


JerryJ wrote:
[color=blue]
> I have to fetch values from a database and store them in a vector in
> my c++ application.
>
> What is faster? if i fetch the values sorted from the database with a
> 'order by' clause in the sql-statement or if i fetch them into the
> vector unsorted and sort the vector then?
>
>
> Thanx for your help
>
>
> Greetz
>
> Jens[/color]

--
Rakesh Kumar
** Remove nospamplz from my email address for my real email **
Siemel Naran
Guest
 
Posts: n/a
#8: Jul 22 '05

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


"Rakesh Kumar" <dreamzlion_nospamplz@yahoo.com> wrote in message
news:408b1721
[color=blue]
> Generally , the server is much more powerful ( people invest more on
> servers than on clients). Hence it would be better to do the task on the
> DB itself, rather than in C++ .[/color]

OTOH, the server is serving thousands of people, but the client serves just
one person. So it could still be faster to do the sorting on the client
side.


Siemel Naran
Guest
 
Posts: n/a
#9: Jul 22 '05

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


"JerryJ" <jerryj@gmx.de> wrote in message
[color=blue]
> I have to fetch values from a database and store them in a vector in
> my c++ application.
>
> What is faster? if i fetch the values sorted from the database with a
> 'order by' clause in the sql-statement or if i fetch them into the
> vector unsorted and sort the vector then?[/color]

Generally, sorting on server is faster. Moreover, using ORDER BY in the
select statement could encourage the database to use the right index and do
no sort at all, so your performance will be super-fast. This is especially
invaluable for large datasets.

OTOH, if you read a list of names, then the user clicks the last name to
sort by last name, then the clicks first name to sort by first name, then
clicks city to sort by city, etc, then it might be better to do all the
sorting on the client side.

Of course, this means loading all the data into the std::vector, which might
not be possible or advised because there are so many records.

Sorry, no fixed answer to the question.


Dave Moore
Guest
 
Posts: n/a
#10: Jul 22 '05

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


jerryj@gmx.de (JerryJ) wrote in message news:<e9013551.0404240119.8a52dbf@posting.google.c om>...[color=blue]
> I have to fetch values from a database and store them in a vector in
> my c++ application.
>
> What is faster? if i fetch the values sorted from the database with a
> 'order by' clause in the sql-statement or if i fetch them into the
> vector unsorted and sort the vector then?
>
>
> Thanx for your help
>
>
> Greetz
>
> Jens[/color]

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.

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
Siemel Naran
Guest
 
Posts: n/a
#11: Jul 22 '05

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


"Dave Moore" <dtmoore@rijnh.nl> wrote in message
[color=blue]
> 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.[/color]

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.

[color=blue]
> 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[/color]


Dave Moore
Guest
 
Posts: n/a
#12: Jul 22 '05

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


"Siemel Naran" <SiemelNaran@REMOVE.att.net> wrote in message news:<LvQjc.58845$um3.1133335@bgtnsc04-news.ops.worldnet.att.net>...[color=blue]
> "Dave Moore" <dtmoore@rijnh.nl> wrote in message
>[color=green]
> > 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.[/color]
>
> 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.
>[/color]

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
Closed Thread