On 7/11/2011 11:04 AM, Prakash Reddy Bande wrote:
> We were looking at the ways we can optimize our application. Our app
> does a simple sqlite3_exec and sends the callback as below. The data
> is just a map<string, vector<string>  >
> int sqlite3TableCallback(void* data, int ncols, char** values, char** headers)
> {
>              map<string, vector<string>  >&  table = *(( map<string, 
> vector<string>  >*) data);
>              for (int i = 0; i<  ncols; i++)
>              {
>                    if(values[i])
>                          table.Data[headers[i]].push_back(string(values[i]));
>                    else
>                          table.Data[headers[i]].push_back(string());
>              }
>              return 0;
> }
> Here, we wanted to optimize the string construction of string in
> table.Data[headers[i]]. We were happy to notice that the headers were
> pointing to the same address, hence we are planning to enhance the
> callback data so that it can track map<char*, string>, and then in
> the table.Data[headers[i]] we can pass reference to the string
> preventing its construction and destruction. Agreed, there will be
> another look-up in the map, for char* to string.

Personally, I'd build a vector<vector<string> >, just assigning 
sequential numbers of columns. You could also build a vector<string> 
containing column names the first time the callback is called. With 
these two in hand, it's trivial to build a map<string, vector<string> > :

for (int i = 0; i < data.size(); ++i) {

Actually, I'd not use sqlite3_exec at all, but instead a loop based on 
sqlite3_prepare and sqlite3_step.
Igor Tandetnik

sqlite-users mailing list

Reply via email to