Home Backend Development PHP Tutorial php程序优化求提议,执行速度太慢

php程序优化求提议,执行速度太慢

Jun 13, 2016 pm 12:45 PM
array file mysql nbsp

php程序优化求建议,执行速度太慢

本帖最后由 zhuzhaodan 于 2013-06-12 18:06:26 编辑 现有四级词库4000单词txt文档,格式如下
accent n.口音,腔调;重音 
acceptable a.可接受的,合意的 
acceptance n.接受,验收;承认 
access n.接近;通道,入口 
accessory n.同谋,从犯;附件 
accident n.意外的;事故 
accidental a.偶然的;非本质的 

现要把英汉分离后分别插入mySQL数据库内,代码如下
<?php<br />
header("Content-type: text/html; charset=utf-8");<br />
$file = dirname(__FILE__)."/siji.txt";  //四级词库文件<br />
if(!file_exists($file)){<br />
	echo 'Not exist';<br />
}<br />
else {<br />
	$a = array();//存放英汉对照对儿的数组<br />
	$lines = file($file);//读取txt到数组,一行为一个英汉对照对<br />
	foreach ($lines as $k=>$v){<br />
		if(preg_match('/[a-zA-Z]/',$v))//有的行是标题之类的,不是英汉对照,判断后不加到数组a里面<br />
			$a[$k] = trim($v);//因为有换行符,去掉<br />
	}<br />
	$b = array();//2维数组,$b[n][0]为英文,$b[n][1]为释义<br />
	foreach($a as $k=>$v){//把a数组的英汉分离,填充到b数组的第二维内<br />
		preg_match('/([a-zA-Z]*)\s(.*)/',$v,$matches);//正则英汉分离,matches[1]是英文,matches[2]是释义中文<br />
		$b[$k][0] = $matches[1];<br />
		$b[$k][1] = $matches[2];<br />
	}<br />
	$dsn = 'mysql:host=localhost;dbname=test1';<br />
	$db = new PDO($dsn,'root','',array(PDO::MYSQL_ATTR_INIT_COMMAND => 'set names utf8'));<br />
	foreach($b as $k=>$v){//插入数据库<br />
		$db->exec("INSERT INTO siji (en,cn) VALUES ('$v[0]','$v[1]')");//数据库3个字段,id,en是英文,cn是中文<br />
	}<br />
		<br />
}
Copy after login


程序执行时间2分钟左右,求优化建议~我自己感觉正则那部分应该优化不了多少,主要感觉插进数据库这里费时太多,各位高人有方法请不吝赐教,现在程序执行完毕需要120秒,希望最终优化后能到10秒内,谢谢,4级词库一共才4000对词组,120秒太长了

最终效果图:


高人们,因为我比较新手,所以不管从代码上,还是整体思路上都使这个程序运行缓慢,求优化建议,特别是数据库这里,还有数组这里,或者有更好的办法。100分在这里,谢谢!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP's big data structure processing skills PHP's big data structure processing skills May 08, 2024 am 10:24 AM

PHP's big data structure processing skills

How to optimize MySQL query performance in PHP? How to optimize MySQL query performance in PHP? Jun 03, 2024 pm 08:11 PM

How to optimize MySQL query performance in PHP?

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

How to use MySQL backup and restore in PHP?

How to insert data into a MySQL table using PHP? How to insert data into a MySQL table using PHP? Jun 02, 2024 pm 02:26 PM

How to insert data into a MySQL table using PHP?

What are the application scenarios of Java enumeration types in databases? What are the application scenarios of Java enumeration types in databases? May 05, 2024 am 09:06 AM

What are the application scenarios of Java enumeration types in databases?

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

How to fix mysql_native_password not loaded errors on MySQL 8.4

How to use MySQL stored procedures in PHP? How to use MySQL stored procedures in PHP? Jun 02, 2024 pm 02:13 PM

How to use MySQL stored procedures in PHP?

How to create a MySQL table using PHP? How to create a MySQL table using PHP? Jun 04, 2024 pm 01:57 PM

How to create a MySQL table using PHP?

See all articles