gpt4 book ai didi

c - 在更快的算法和代码检查方面需要帮助 - 将值与数据库数据进行比较

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:26:09 29 4
gpt4 key购买 nike

我需要帮助开发一种更快的算法以用于我的 CGI 文件(使用 C)。我有四个 sqlite3 数据库表,其中包含两个参数 attack_type 和 defend_type。这些表是 mod_no、mod_ne、mod_av 和 mod_se。当函数double modifier(int attack_type, int defend)时,如果其中一个表中同时存在attack_type和defend_type的条目,则sqlite3语句会从数据库中进行选择。根据 sqlite3 语句 yields == 1 的表,该函数将返回数据类型为 double 的特定值。

代码如下:

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;
}

此代码的问题是 (1) 它不返回任何值,并且 (2) 速度很慢。为了测试问题是否出在代码上,我初始化了 eff = 2 以便函数返回 1。CGI 文件运行速度很快。但是当我删除初始值时,它又变慢了。

我需要一个工作函数。我做错了什么?

最佳答案

这是一个可能的优化:

select exists(select atk_typ,pok_typ from mod_no where atk_typ = %d and pok_typ = %d);",mod_tables[i],atk_typ, pok_typ)
union
select exists(select atk_typ,pok_typ from mod_ne where atk_typ = %d and pok_typ = %d);",mod_tables[i],atk_typ, pok_typ)
union
select exists(select atk_typ,pok_typ from mod_av where atk_typ = %d and pok_typ = %d);",mod_tables[i],atk_typ, pok_typ)
union
select exists(select atk_typ,pok_typ from mod_se where atk_typ = %d and pok_typ = %d);",mod_tables[i],atk_typ, pok_typ)

那么你只有 1 个查询并且可以去掉外层的 for 循环。

关于c - 在更快的算法和代码检查方面需要帮助 - 将值与数据库数据进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12585448/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com