473,246 Members | 1,944 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,246 software developers and data experts.

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

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
Jul 22 '05 #1
11 2960

"JerryJ" <je****@gmx.de> wrote in message
news:e9*************************@posting.google.co m...
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


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

john
Jul 22 '05 #2

"JerryJ" <je****@gmx.de> wrote in message
news:e9*************************@posting.google.co m...
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?


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

-Mike
Jul 22 '05 #3
JerryJ wrote:

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


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.
Jul 22 '05 #4
On 4/24/2004 11:19 AM, JerryJ wrote:
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?


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
Jul 22 '05 #5
"JerryJ" <je****@gmx.de> wrote in message
news:e9*************************@posting.google.co m...
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


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/
Jul 22 '05 #6
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:
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


--
Rakesh Kumar
** Remove nospamplz from my email address for my real email **
Jul 22 '05 #7
"Rakesh Kumar" <dr******************@yahoo.com> wrote in message
news:408b1721
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++ .


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.
Jul 22 '05 #8
"JerryJ" <je****@gmx.de> wrote in message
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?


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.
Jul 22 '05 #9
je****@gmx.de (JerryJ) wrote in message news:<e9*************************@posting.google.c om>...
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


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
Jul 22 '05 #10
"Dave Moore" <dt*****@rijnh.nl> 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

Jul 22 '05 #11
"Siemel Naran" <Si*********@REMOVE.att.net> wrote in message news:<Lv*********************@bgtnsc04-news.ops.worldnet.att.net>...
"Dave Moore" <dt*****@rijnh.nl> 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
Jul 22 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Zhang Le | last post by:
Hi, I did a small benchmark of matrix-vector multiply operation using Numeric module. I'm a bit suprised to find matrix*col-vector is much faster than row-vector*matrix. I wonder whether other...
2
by: {AGUT2} {H}-IWIK | last post by:
Guys, I have these headers: #include <stdlib> #include <math> #include <iostream> #include <iomanip> #include <fstream> #include <vector> #include <string> #include <algorithm>
4
by: JerryJ | last post by:
hi there, 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...
2
by: Sims | last post by:
Hi, I have a structure as follow struct sIntStructure { int m_nNumber; // // A few more variables //
10
by: tjgable | last post by:
I have some code that builds fine on .Net 2001(?).. VC++ 7.0. But with gcc 3.42 in MinGW and MS VC++ 6.0 it does not. I can understand VC++ not working, but isn't gcc standard yet? Here is the...
10
by: Extremest | last post by:
I know there are ways to make this a lot faster. Any newsreader does this in seconds. I don't know how they do it and I am very new to c#. If anyone knows a faster way please let me know. All...
7
by: Steve Bergman | last post by:
I'm involved in a discussion thread in which it has been stated that: """ Anything written in a language that is 20x slower (Perl, Python, PHP) than C/C++ should be instantly rejected by users...
28
by: n00m | last post by:
Both codes below read the same huge(~35MB) text file. In the file 1000000 lines, the length of each line < 99 chars. Stable result: Python runs ~0.65s C : ~0.70s Any thoughts?
3
by: utab | last post by:
Dear all, I have the below template (1) which I would like to implement to accept either a vector or an array. I thought on it some time and came with a solution as in (2) , however the call of...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.