How to optimize algorithms

Source: Internet
Author: User
How to optimize the algorithm is only the first, second choice. You have to perform the operation twice. The second volunteer is basically the same. The running data of 100 students took about 6 to 7 seconds. How can I optimize it ~~ Thank you if ($ _ POST ['Cal ']) {// -------------------------------------------- volunteer ------- how to optimize the algorithm
Only the first and second selections. You have to perform the operation twice. The second volunteer is basically the same. The running data of 100 students took about 6 to 7 seconds. How can I optimize it ~~ Thank you.


If ($ _ POST ['Cal '])
{
// ---------------------------------------------- First volunteer -------------------------------------------
$ SQL = "select * from hos ;";
$ Res = mysql_query ($ SQL) or die (mysql_error ());
$ Info = mysql_fetch_array ($ res) or die (mysql_error ());
// Echo $ info ['total']; // Obtain the General Hospital count.
Do {// Loop 1
// ++ ++ // Demarcation line
If ($ info ['same'] = 0) // if you are not a man or a woman
{
$ Hos_id = $ info ['id']; // hospital id
// Echo $ hos_id .",";
$ Hos_total = $ info ['total']; // total hospital count
// Echo $ hos_total .".";
$ SQL _total = "select count (*) as total from stu where first = '". $ hos_id. "' and get = 0;"; // add the number of students for this volunteer
$ Res_total = mysql_query ($ SQL _total );
$ Info_total = mysql_fetch_array ($ res_total); // you can specify the number of students you want.
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 // Loop 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); // The End of loop 2
}
// ++ ++ // Demarcation line

Else // If you distinguish between male and female ----- $ info ['same'] = 1;
{
$ Hos_id = $ info ['id']; // hospital id
// Boys' SQL
$ Hos_total_men = $ info ['Men']; // total hospital demand for boys
$ SQL _total_men = "select count (*) as total_men from stu where first = '". $ hos_id. "'and sex = 'f' and get = 0;"; // Number of male students added to the volunteer
$ Res_total_men = mysql_query ($ SQL _total_men );
$ Info_total_men = mysql_fetch_array ($ res_total_men); // The number of male students who entered the volunteer information.
If ($ hos_total_men> $ info_total_men ['total _ Men'])
{$ Sqlbench men = "select id from stu where first = '". $ hos_id. "' and sex = 'm' and get = 0 order by score DESC ;";}
Else {$ sql0000men = "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 );
$ Infotailmen = mysql_fetch_array ($ res1_men );
Do // Loop 2
{
// Boys' updates
$ Stu_id_men = $ infotailmen ['id'];
$ Sql2_men = "UPDATE stu SET result = '". $ hos_id. "', get = 1 where id = '". $ stu_id_men ."';";
$ Res2_men = mysql_query ($ sql2_men );
} While ($ infotailmen = mysql_fetch_array ($ ressponmen ));
// Girl SQL
$ Hos_total_wom = $ info ['wom ']; // total hospital demand for girls
$ SQL _total_wom = "select count (*) as total_wom from stu where first = '". $ hos_id. "'and sex = 'f' and get = 0;"; // Number of male students added to the volunteer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.