I need help in developing a faster algorithm to be used in my CGI file (using C). I have four sqlite3 database tables that contains two parameters attack_type and defend_type. These tables are mod_no, mod_ne, mod_av, and mod_se. When the function double modifier(int attack_type, int defend), an sqlite3 statement selects from the database if an entry of attack_type and defend_type exists together in one of the tables. Depending on which table the sqlite3 statement yields == 1, the function will return a specific value of data type double.
Here is the code:
double modifier(int attack_type, int defend_type)
{
sqlite3 *conn;
sqlite3_stmt *res;
sqlite3_open("MP1.sl3", &conn);
int i,n;
int eff;
char temp[MAXLENGTH];
char mod_tables[4][8] = {"mod_no","mod_ne","mod_av","mod_se"};
for (i = 0; i <=3; i++) {
printf(temp, "select exists(select atk_typ,pok_typ from %s where atk_typ = %d and pok_typ = %d);",mod_tables[i],atk_typ, pok_typ);
sqlite3_prepare_v2(conn,temp,MAXLENGTH,&res,NULL);
while(sqlite3_step(res) != SQLITE_ROW) {
n = sqlite3_column_int(res,0);
}
sqlite3_finalize(res);
if (n == 1) eff = i;
if (eff == i) break;
}
sqlite3_close(conn);
return eff/(double)2;
}
The problem with this code is (1) It does not return any value, and (2) It is slow. To test if the problem is with the code, I initialized eff = 2 so that the function returns 1. The CGI file ran fast. But when I removed the initial value, it returned to being slow.
I need to have a working function. What am I doing wrong?
Here is one possible optimisation:
Then you only have 1 query and can get rid of the outer for loop.