PHP Program Optimization request, execution speed is too slow

Source: Internet
Author: User
Tags dsn
PHP Program Optimization recommendations, execution speed is too slow
This post was last edited by Zhuzhaodan on 2013-06-12 18:06:26

Existing Four-level thesaurus 4000 wordsTXT document in the following format
Accent N. accent, Tone, accent
Acceptable A. Acceptable, agreeable
Acceptance N. Acceptance, acceptance, acknowledgement
Access N. Close, channel, entrance
Accessory N. Accomplice, accessory, accessory
Accident N. An accident;
Accidental A. Accidental; non-essential

Now to separate the English and Chinese into the MySQL database, the code is as follows
 
  Header ("content-type:text/html; Charset=utf-8 ");
$file = DirName (__file__). " /siji.txt "; Class Four Thesaurus File
if (!file_exists ($file)) {
echo ' not exist ';
}
else {
$a = array ();//store Arrays of English-Chinese control pairs
$lines = file ($file);//Read txt to array, a behavior of an English-Chinese control
foreach ($lines as $k = = $v) {
if (Preg_match ('/[a-za-z]/', $v))//Some rows are headings or the like, not English-Chinese control, not added to the array a after judgment
$a [$k] = Trim ($v);//Because there are newline characters, remove
}
$b = Array (),//2 array, $b [n][0] in English, $b [n][1] for interpretation
foreach ($a as $k + $v) {//separates the English-Chinese of a array into the second Venetian of the B-array
Preg_match ('/([a-za-z]*) \s (. *)/', $v, $matches);//Regular English-Chinese separation, matches[1] is English, matches[2] is an interpretation of 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 Database
$db->exec ("INSERT into Siji (EN,CN) VALUES (' $v [0] ', ' $v [1] ') ');//Database 3 fields, Id,en is English, CN is Chinese
}

}


Program execution time about 2 minutes, to optimize the suggestion ~ I feel that the part should be optimized not much, the main feeling inserted into the database here too much time, you have a method please enlighten, now program execution completed need 120 seconds, hope that the final optimization can reach 10 seconds, thank you, 4-level thesaurus altogether only 4000 pairs of phrases, 120 seconds too long

Eventually:


High people, because I am more novice, so regardless of the code, or the overall idea of the program to make this slow, to optimize the recommendations, especially DatabaseHere, there are ArrayHere, or have better way.。 100 points here, thank you!

Share to:


------Solution--------------------
Not necessarily to meet the requirements, but certainly faster than yours

------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 a similar to the INSERT SQL statement, save to *.sql
2, directly using MySQL to import the above *.sql, PHP can use the exec
------Solution--------------------
Sorry, there's a missing t on the insert.
The MySQL Export statement format is made with multiple lines of insert. The import speed is fast. General one statement 2000 rows records no problem.
------Solution--------------------
With parameter binding, Pdo::quote (Escape special characters) will always be executed, ignoring your data actually does not contain special characters
The extra time is Pdo::quote's execution time.
  • 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.