Php program optimization proposal, execution speed is too slow

Source: Internet
Author: User
Php program optimization advice, execution speed is too slow This post was last edited by zhuzhaodan from 2013-06-, the existing four-level Dictionary 4000 words txt document, the format is as follows accent & nbsp; n. accent, accent; accent & nbsp; acceptable & nb php program optimization suggestions, execution speed is too slow
At the end of this post, zhuzhaodan edited the txt file of the 4000 words in the four-level Dictionary at 18:06:26 on. The format is as follows:
Accent n. accent, accent; accent
Acceptable a. acceptable and acceptable
Acceptance n. accept, accept; acknowledge
Access n. approaching; channel, Portal
Accessory n. accomplice; Attachment
Accident n. unexpected; accident
Accidental a. accidental; non-essential

After separating English and Chinese, insert them into the mySQL database. the code is as follows:
 Header ("Content-type: text/html; charset = utf-8 ");
$ File = dirname (_ FILE _). "/siji.txt"; // level 4 Dictionary file
If (! File_exists ($ file )){
Echo 'not exist ';
}
Else {
$ A = array (); // array of English-Chinese pairs
$ Lines = file ($ file); // read txt to the array, one line is an English-Chinese contrast
Foreach ($ lines as $ k => $ v ){
If (preg_match ('/[a-zA-Z]/', $ v) // Some rows are headers and the like, instead of English-Chinese comparisons. after judgment, they are not added to array.
$ A [$ k] = trim ($ v); // remove the line break
}
$ B = array (); // A 2-dimensional array. $ B [n] [0] indicates English, and $ B [n] [1] indicates meaning.
Foreach ($ a as $ k => $ v) {// separates the English and Chinese characters of array a and fills it in the second dimension of array B.
Preg_match ('/([a-zA-Z] *) \ s (. *)/', $ v, $ matches); // regular English-Chinese separation. matches [1] is in English and matches [2] is in Chinese.
$ B [$ k] [0] = $ matches [1];
$ B [$ k] [1] = $ matches [2];
}
$ Dsn = 'MySQL: host = localhost; dbname = test1 ';
$ Db = new PDO ($ dsn, 'root', '', array (PDO: MYSQL_ATTR_INIT_COMMAND => 'set names utf8 '));
Foreach ($ B as $ k => $ v) {// Insert a database
$ Db-> exec ("insert into siji (en, cn) VALUES ('$ v [0]', '$ v [1]')"); // three fields in the database: id, en (English), cn (Chinese)
}

}


The execution time of the program is about 2 minutes. find optimization suggestions ~ I personally think that the regular expression part should not be optimized much. It mainly seems that it takes too much time to insert it into the database. if you have any ideas, please kindly advise. it takes 120 seconds to complete the program execution, I hope that the final optimization will take 10 seconds. thank you. there are only 4000 pairs of phrases in the 4-level lexicon, and 120 seconds is too long.

Finally:


High-tech people, because I am a newbie, make the program run slowly in terms of code and overall thinking, and seek optimization suggestions, especially for databases and arrays, or there is a better way. 100 points here. thank you!
Share:
------ Solution --------------------
May not be able to meet the requirements, but it must be faster than yours
$ Ar = array_map ('trim', file('siji.txt ', FILE_IGNORE_NEW_LINES
------ Solution --------------------
FILE_SKIP_EMPTY_LINES ));
Foreach ($ ar as $ v ){
Preg_match ('/([a-zA-Z] *) \ s (. *)/', $ v, $ r );
If (isset ($ r [2]) $ B [] = "('$ r [1]', '$ r [2]')";
}
$ Dsn = 'MySQL: host = localhost; dbname = test1 ';
$ Db = new PDO ($ dsn, 'root', '', array (PDO: MYSQL_ATTR_INIT_COMMAND => 'set names utf8 '));
$ Db-> exec ('Insert INTO siji (en, cn) VALUES '. join (', ', $ B ));
 

------ Solution --------------------
1. format the content into an insert SQL statement similar to #1 and save it as *. SQL
2. directly use mysql to import the preceding *. SQL. php can use exec
------ Solution --------------------
Sorry, insert missing t
MySQL uses multiple insert statements for export. the import speed is very fast. Generally, there is no problem with the 2000 rows of a statement.
------ Solution --------------------
When using parameter binding, PDO: quote (escape special characters) will always be executed, and your data does not actually contain special characters
The displayed time is the execution time of the PDO: quote.

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.