|
*sometimes*. And it may very well depend on the platform.
I have a communications system that transmits data using tags and a parser. I have upwards of 500 data elements that can go back and forth. The initial implementation used a simple linear search for finding a passed tag. As time passed, the tag set became larger and larger.
Hey, this is a good place for a mapped data set, allowing me to find the tag quickly I thought. The map turned out to be 3 times slower than the linear search. Never did figure out why. Showed the sample code to someone who loves the STL and has a lot more experience than I do. No idea. Someday I'll get back to it.
Charlie Gilley
<italic>Stuck in a dysfunctional matrix from which I must escape...
"Where liberty dwells, there is my country." B. Franklin, 1783
“They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759
|
|
|
|
|
My little experiment gives me opposite results:
#include <iostream>
#include <map>
using namespace std;
constexpr int Indices = 4096;
constexpr int Size = 512;
extern const int idx[Indices];
extern const string tag [Size];
int linear_search(const string & s )
{
for (int n = 0; n<Size; ++n)
if ( s == tag[n]) return n;
return -1;
}
constexpr int Iterations = 1000000;
int main()
{
std::map<string, int> mtag;
for (int n=0; n<Size; ++n)
{
mtag.insert(make_pair(tag[n], n));
}
int sum = 0;
for (int n=0; n<Iterations; ++n)
{
int i = idx[n % Indices];
const string & s = tag[i];
int k;
#ifdef LINEAR_SEARCH
k = linear_search( s );
#else
auto it = mtag.find(s);
k = it->second;
#endif
sum+=k;
}
cout << "sum " << sum << endl;
} Where
tag is an array of 512 randomly generated strings (having length betwween 4 and 12)idx is an array of 4096 randomly generated indices (for quickly gatering a candidate)
Output
g++ -D LINEAR_SEARCH -Wall lookup.cpp -o lookup_linear_search
g++ -Wall lookup.cpp -o lookup_map
time ./lookup_linear_search
sum 252632422
real 0m1,821s
user 0m1,811s
sys 0m0,008s
time ./lookup_map
sum 252632422
real 0m0,297s
user 0m0,297s
sys 0m0,000s
|
|
|
|
|
Hmm, your code is surprising close to mine.
What compiler are you using? I have to use VS2008 for embedded work, so maybe the STL implementation is suspect. I'll have to dust it off. Your results are what I would have expected.
Charlie Gilley
<italic>Stuck in a dysfunctional matrix from which I must escape...
"Where liberty dwells, there is my country." B. Franklin, 1783
“They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759
|
|
|
|
|
Quote: What compiler are you using? The good one.
The hint is in the command line[^]:
g++ -D LINEAR_SEARCH -Wall lookup.cpp -o lookup_linear_search
|
|
|
|
|
Charlie Gilley
<italic>Stuck in a dysfunctional matrix from which I must escape...
"Where liberty dwells, there is my country." B. Franklin, 1783
“They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759
|
|
|
|
|
how do you incorporate the GUI in C++?
|
|
|
|
|
You can use the standard Win32 controls, or if your are writing MFC code, then there are classes for every situation.
|
|
|
|
|
And if you are on Linux, then you probably want to look at gtk or QT. I'm not sure what Mac uses these days.
There's dozens of tool-kits available for Linux and Windows, many are cross-platform, too. Use google to find something you like, and suits your needs.
Keep Calm and Carry On
|
|
|
|
|
I think you meant to post that to the OP.
|
|
|
|
|
Apparenlty am using linux distro.
|
|
|
|
|
"Apparently"? So are you not sure?
Assuming it is linux then I suggest you google for "linux gui" as there are different options available. See also the message from k5054 above.
|
|
|
|
|
Use wxWidgets[^] GUI library, then your code will work on Windows, Linux and MacOS.
|
|
|
|
|
If you're a young guy, I'll take your answer too. I work in the land of embedded systems where nothing ever dies. Some of the systems I support use CE 4.2/5.0 and Embedded Visual C++ - a bastardized clone of VC++ 6.0 (I think). In EVC++ I can place my cursor on something I am interested in and press help - and I get help.
Years pass, and now I also support a variant using WEC7 and VS2008. No matter how many times I have installed VS2008, help is trashed. It just does not work. I have spend days over the past years trying to resolve this issue. I want to do something stupid simple like click on CString and press F1 to pull up the class description. Nope, never happens.
Any of you resolve this issue? Spoilers?
Charlie Gilley
<italic>Stuck in a dysfunctional matrix from which I must escape...
"Where liberty dwells, there is my country." B. Franklin, 1783
“They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759
|
|
|
|
|
AFAIR, the latest VC++ edition that worked with the MSDN help good enough was VC++0.
In every new VS edition it worked either worse either didn't work at all.
|
|
|
|
|
0? lol, I agree. It's totally bizarre. I'll keep digging, maybe write my first article.
Charlie Gilley
<italic>Stuck in a dysfunctional matrix from which I must escape...
"Where liberty dwells, there is my country." B. Franklin, 1783
“They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759
|
|
|
|
|
Hi C++ Gurus,
Below code is generating error code -2. Its working fine for sql queries like "select * from emp", but where as in Insert statement its returning error code. Please help.
Regards,
Suresh
#include "database.h"
int main(){
SQLRETURN ret = 0;
SQLHENV env;
SQLHDBC dbc;
SQLHSTMT stmt;
SQLSMALLINT msg_len = 0;
SQLCHAR sql_state[6], message[256];
SQLINTEGER native_error = 0;
try{
ret = SQLAllocEnv(&env);
ret = SQLAllocConnect(env, &dbc);
ret = SQLConnect(dbc, (SQLCHAR*)"KarTarDB", strlen("KarTarDB"), (SQLCHAR*)"sa",2,(SQLCHAR*)"KarTarPwd",9);
if(ret == SQL_INVALID_HANDLE || ret < 0) {
cout << "Connection open failure." << endl;
ret = SQLGetDiagRec(SQL_HANDLE_STMT, stmt, 1, sql_state, &native_error, message, sizeof(message), &msg_len);
cout << ret << endl;
} else{
char sql[100]="INSERT INTO emp(name, age, dob) VALUES ('john', 23, '010101')";
ret = SQLPrepare(stmt,(unsigned char*)sql, SQL_NTS);
ret = SQLExecute(stmt);
if(ret == SQL_ERROR || ret < 0) {
cout << "ResultSet Error: " << ret << endl;
}else{
cout << "Done" << endl;
}
}
}catch(...) {
cout << "Database error.." << endl;
}
return 0;
}
|
|
|
|
|
What is -2? According to MSDN SQLExecute Function - SQL Server | Microsoft Docs it can return the following:
Quote: SQL_SUCCESS, SQL_SUCCESS_WITH_INFO, SQL_NEED_DATA, SQL_STILL_EXECUTING, SQL_ERROR, SQL_NO_DATA, SQL_INVALID_HANDLE, or SQL_PARAM_DATA_AVAILABLE.
Besides
Quote: When SQLExecute returns either SQL_ERROR or SQL_SUCCESS_WITH_INFO, an associated SQLSTATE value can be obtained by calling SQLGetDiagRec with a HandleType of SQL_HANDLE_STMT and a Handle of StatementHandle.
|
|
|
|
|
Hi Victor, Thanks for your response.
As you can see in the code, I have already used SQLGetDiagRec.
ret = SQLGetDiagRec(SQL_HANDLE_STMT, stmt, 1, sql_state, &native_error, message, sizeof(message), &msg_len)
But even that is returning -2 and not giving any other details, hence I commented it. Am I missing anything? How can I get more details from ret object apart from -2.
Regards,
Suresh
|
|
|
|
|
SureshBL wrote: But even that is returning -2 and not giving any other details, hence I commented it. Am I missing anything? How can I get more details from ret object apart from -2.
Well, -2 means SQL_INVALID_HANDLE.
So you need to go one step back and check why the handle is invalid!
|
|
|
|
|
Thanks
Do you see anything wrong with my Code?
As mentioned querying table is working fine with the connection, but Insert statement is making handle invalid.
SQLSTATE: l NativeError: 0 ErrMsg: 0x6cfce0
Apologies, I am learning c++ now. So I am not expert to understand lots of bits and pieces to debug the issue in every step.
Regards,
Suresh
modified 19-May-20 17:07pm.
|
|
|
|
|
Can't say I've ever used this, but shouldn't the SQLHSTMT variable 'stmt' be initialized with something?
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
modified 19-May-20 17:31pm.
|
|
|
|
|
Ok got you. So I have to assign dbc to stmt?
ret = SQLAllocHandle(SQL_HANDLE_STMT, dbc, &stmt);
|
|
|
|
|
Great Its working now. Thank you very much..
|
|
|
|
|
You're welcome.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
|
|
|
|
|
Does anybody know where kernel32.lib could be found?
|
|
|
|
|