473,473 Members | 1,589 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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 the sql-statement or if i fetch them into the
vector unsorted and sort the vector then?
Thanx for your help
Greetz

Jens
Jul 20 '05 #1
4 4510

"JerryJ" <je****@gmx.de> wrote in message
news:e9**************************@posting.google.c om...
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 the sql-statement or if i fetch them into the
vector unsorted and sort the vector then?
Thanx for your help
Greetz

Jens


Well, the easiest thing to do would be to test it, of course, but there's
probably no point in writing your own sorting code when the database will do
it for you. Unless perhaps you have some unusual sorting logic which can't
be handled in the database.

Simon
Jul 20 '05 #2
To add to Simon's response, a lot of work has gone into database sorting
algorithms over the years so database sorting is fairly efficient. One of
the advantages the DBMS has over your code is that it may be able to exploit
indexes to facilitate the task.

This isn't to say that you can't sometimes sort faster in code but I would
specify the ORDER BY unless you have critical performance requirements and
your performance testing shows that sorting in your application is the best
method..

--
Hope this helps.

Dan Guzman
SQL Server MVP

"JerryJ" <je****@gmx.de> wrote in message
news:e9**************************@posting.google.c om...
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 the sql-statement or if i fetch them into the
vector unsorted and sort the vector then?
Thanx for your help
Greetz

Jens

Jul 20 '05 #3
Dan Guzman (da*******@nospam-earthlink.net) writes:
To add to Simon's response, a lot of work has gone into database sorting
algorithms over the years so database sorting is fairly efficient. One
of the advantages the DBMS has over your code is that it may be able to
exploit indexes to facilitate the task.

This isn't to say that you can't sometimes sort faster in code but I
would specify the ORDER BY unless you have critical performance
requirements and your performance testing shows that sorting in your
application is the best method..


One argument for sorting client-side it scales better. But this would
only be relevant you are retrieving a lot of data, and there are many users
who are make concurrent retrievals.

Another factor is that the server may be better equipped with CPU than
the client, and thus be faster.

In the general case, I definitely agree with the recommendation to use
ORDER BY.

--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #4
I'd like to know your opinion about the following:

cache a hashmap of all the objects with their ID being the key
when you make your query with the order by, only fetch the IDs, and
create a new Vector by retrieving the elements from the HashMap

it seems to me that it benefits the database sorting capacities, while
avoiding the overhead of recreating the objects every time

of course it incurs some caching management on the client side, but I
think it would be worth it

let me know what you think

Yvan
Jul 20 '05 #5

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>
3
by: Lasse Skyum | last post by:
I'm new to STL, so this might be a laugh... How do a sort the elemens in a vector? There is a sort() function on list, but not on vector... right? But why? Suppose I would like a...
2
by: Sims | last post by:
Hi, I have a structure as follow struct sIntStructure { int m_nNumber; // // A few more variables //
11
by: JerryJ | last post by:
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...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.