Reputation: 164
i´d like to know if there is a quicker way to copy my data from a mysqlpp::storequeryresult to a std::vector.
My example is as follows: I store my Query Result with query.store() in StoreQueryResult and my result is e.g. a table with one column with doubles in it. Now I want to copy those doubles into a std::vector. The way I´m doing it right now is to access every single double with the [][] operator and copy it to my vector in a for-loop.
This works but it is very time consuming since i´m copying like 277000 double in a loop. Is there a way to just copy the column to my vector? The thing is my other functions use std::vectors in their parameterlists. Alternatively i could change my functions to call a StoreQueryResult i guess, but i´d prefere a std::vector.
Here is my simplified code:
void foo()
{
vector<double> vec;
mysqlpp::StoreQueryResult sqr;
Query query;
query << "SELECT * FROM tablename";
sqr = query.store();
vec.reserve(sqr.num_rows());
vec.resize(sqr.size());
for(int i=0; i != vec.size(); i++)
{
vec[i] = sqr[i]["my_column"];
}
}
I want something like:
vec = sqr["my_column"] // when my_column is a field with doubles
Thx in advance.
Martin
Upvotes: 2
Views: 1323
Reputation: 5848
It is possible that you wish to create a vector, but then only some values will actually be accessed and used.
In which case we may delay the conversion from mysqlpp::String
to another datatype:
std::vector<mysqlpp::String> data(res.num_rows());
for(size_t i=0, n=res.num_rows(); i<n; ++i)
{
data[i] = std::move(res[i]["value"]);
}
Several things are happening here:
vector
that stores mysqlpp::String
. It is an interesting datatype that can be converted to many others. In your case you were using operator double () const
.++i
rather than i++
; they don't add up to many cycles, but should be used, to keep the code in the spirit of optimisation.If then you have something like:
double sum = 0.0;
for(size_t i=0, n=data.num_rows(); i<n; i+=2)
{
sum+=double(data[i]);
}
You will only run the conversion routine on ½ of your values.
Of course, if you plan to use the resultant vector several times, you will actually start running the same conversions again and again. So this "optimisation" will actually hurt performance.
Upvotes: 0
Reputation: 385174
Ultimately, if you need to copy then you need to copy, and whether you write the loop yourself or get a library function to do it isn't particularly relevant.
What you can do is pre-reserve
enough space in the destination vector to avoid repeated re-allocations and copies:
vec.reserve(sqr.num_rows());
Upvotes: 0