求教导如何优化算法
程序员文章站
2024-01-11 13:45:10
...
求教导怎么优化算法
只是第1,2次选择。还要再做2次。后面基本一样重复第二次志愿的。100个学生运行数据大概花了6-7秒。求指导怎么可以优化下~~谢谢
if($_POST['cal'])
{
//--------------------------------------------第一志愿-------------------------------------------
$sql = "select * from hos;";
$res = mysql_query($sql)or die(mysql_error());
$info = mysql_fetch_array($res)or die(mysql_error());
//echo $info['total']; //得到总医院数
do{ //循环1
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//分界线
if($info['same']==0) //如果不分男女时
{
$hos_id = $info['id']; //医院ID号
//echo $hos_id.",";
$hos_total = $info['total']; //医院总需求人数
//echo $hos_total.".";
$sql_total = "select count(*) as total from stu where first='".$hos_id."' and get=0 ;"; //添该志愿的学生数
$res_total = mysql_query($sql_total);
$info_total = mysql_fetch_array($res_total); //得到填报该志愿的学生数
if($hos_total > $info_total['total'])
{$sql1 = "select id from stu where first='".$hos_id."' and get=0 ORDER BY score DESC ;";}
else {$sql1 = "select id from stu where first='".$hos_id."' and get=0 ORDER BY score DESC limit $hos_total;";}
$res1 = mysql_query($sql1);
$info1 = mysql_fetch_array($res1);
do //循环2
{
$stu_id = $info1['id'];
$sql2 = "UPDATE stu SET result='".$hos_id."',get=1 where id='".$stu_id."' ;";
$res2 = mysql_query($sql2);
}while($info1 = mysql_fetch_array($res1)); //循环2结束
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//分界线
else //如果区分男女-----$info['same']==1;
{
$hos_id = $info['id']; //医院ID号
//男生SQL
$hos_total_men = $info['men']; //医院总需求男生数
$sql_total_men = "select count(*) as total_men from stu where first='".$hos_id."' and sex='f' and get=0 ;"; //添该志愿的男学生数
$res_total_men = mysql_query($sql_total_men);
$info_total_men = mysql_fetch_array($res_total_men); //得到填报该志愿的男学生数
if($hos_total_men > $info_total_men['total_men'])
{$sql1_men = "select id from stu where first='".$hos_id."' and sex='m' and get=0 ORDER BY score DESC ;";}
else {$sql1_men = "select id from stu where first='".$hos_id."'and sex='m' and get=0 ORDER BY score DESC limit $hos_total_men;";}
$res1_men = mysql_query($sql1_men);
$info1_men = mysql_fetch_array($res1_men);
do //循环2
{
//男生更新
$stu_id_men = $info1_men['id'];
$sql2_men = "UPDATE stu SET result='".$hos_id."',get=1 where id='".$stu_id_men."' ;";
$res2_men = mysql_query($sql2_men);
}while($info1_men = mysql_fetch_array($res1_men));
//女生SQL
$hos_total_wom = $info['wom']; //医院总需求女生数
$sql_total_wom = "select count(*) as total_wom from stu where first='".$hos_id."' and sex='f' and get=0 ;"; //添该志愿的男学生数
只是第1,2次选择。还要再做2次。后面基本一样重复第二次志愿的。100个学生运行数据大概花了6-7秒。求指导怎么可以优化下~~谢谢
if($_POST['cal'])
{
//--------------------------------------------第一志愿-------------------------------------------
$sql = "select * from hos;";
$res = mysql_query($sql)or die(mysql_error());
$info = mysql_fetch_array($res)or die(mysql_error());
//echo $info['total']; //得到总医院数
do{ //循环1
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//分界线
if($info['same']==0) //如果不分男女时
{
$hos_id = $info['id']; //医院ID号
//echo $hos_id.",";
$hos_total = $info['total']; //医院总需求人数
//echo $hos_total.".";
$sql_total = "select count(*) as total from stu where first='".$hos_id."' and get=0 ;"; //添该志愿的学生数
$res_total = mysql_query($sql_total);
$info_total = mysql_fetch_array($res_total); //得到填报该志愿的学生数
if($hos_total > $info_total['total'])
{$sql1 = "select id from stu where first='".$hos_id."' and get=0 ORDER BY score DESC ;";}
else {$sql1 = "select id from stu where first='".$hos_id."' and get=0 ORDER BY score DESC limit $hos_total;";}
$res1 = mysql_query($sql1);
$info1 = mysql_fetch_array($res1);
do //循环2
{
$stu_id = $info1['id'];
$sql2 = "UPDATE stu SET result='".$hos_id."',get=1 where id='".$stu_id."' ;";
$res2 = mysql_query($sql2);
}while($info1 = mysql_fetch_array($res1)); //循环2结束
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//分界线
else //如果区分男女-----$info['same']==1;
{
$hos_id = $info['id']; //医院ID号
//男生SQL
$hos_total_men = $info['men']; //医院总需求男生数
$sql_total_men = "select count(*) as total_men from stu where first='".$hos_id."' and sex='f' and get=0 ;"; //添该志愿的男学生数
$res_total_men = mysql_query($sql_total_men);
$info_total_men = mysql_fetch_array($res_total_men); //得到填报该志愿的男学生数
if($hos_total_men > $info_total_men['total_men'])
{$sql1_men = "select id from stu where first='".$hos_id."' and sex='m' and get=0 ORDER BY score DESC ;";}
else {$sql1_men = "select id from stu where first='".$hos_id."'and sex='m' and get=0 ORDER BY score DESC limit $hos_total_men;";}
$res1_men = mysql_query($sql1_men);
$info1_men = mysql_fetch_array($res1_men);
do //循环2
{
//男生更新
$stu_id_men = $info1_men['id'];
$sql2_men = "UPDATE stu SET result='".$hos_id."',get=1 where id='".$stu_id_men."' ;";
$res2_men = mysql_query($sql2_men);
}while($info1_men = mysql_fetch_array($res1_men));
//女生SQL
$hos_total_wom = $info['wom']; //医院总需求女生数
$sql_total_wom = "select count(*) as total_wom from stu where first='".$hos_id."' and sex='f' and get=0 ;"; //添该志愿的男学生数
相关文章
相关视频