【发布时间】:2016-07-11 00:52:38
【问题描述】:
嘿,所以我构建了一个过滤组合列表的脚本,它只输出不重复超过 2 次的组合,但它的超级慢是我的脚本:
<?php
ini_set('max_execution_time', '-1');
ini_set('memory_limit', '-1');
$fileCombo = file("ww.txt", FILE_IGNORE_NEW_LINES);
$output = fopen("workpless.txt", "a") or die("Unable to open file!");
//all domains of the entire list
$domains = array();
//only domains that repeat themself less than 2 times
$less = array();
//takes the combo list explode it to domain names
foreach ($fileCombo as $combo) {
$pieces = explode(":", $combo);
$email = explode("@", $pieces[0]);
//import domains to array
$domains[] = strtolower($email[1]);
}
//count each string in the array
$ac = array_count_values($domains);
//this foreach just filter all the domains that not repeat themself over 2 times
foreach ($ac as $email => $item) {
if($item <= 2) {
$less[] = $email;
}
}
/* this foreach is the one that makes all the trubles,
it takes all the domains that the last foreach filtered
and its runing it 1 by 1 on the entire combo list to get
the actual combo */
foreach ($less as $find) {
$matches = array_filter($fileCombo, function($var) use ($find) { return preg_match("/\b$find\b/i", $var); });
foreach ($matches as $match) {
$data = $match . PHP_EOL;
fwrite($output, $data);
}
}
fclose($output);
?>
伪代码(我能做的最好的):
file1:
exaple@example.com:password
exaple@example.com:password
exaple@example.com:password
exaple@example1.com:password
exaple@example2.com:password
array "fileCombo" load file1 into the array
splitting each line by ":" so you will get [0]example@example.com, [1]password
splitting value [0] by "@" so you will get [0]example, [1]example.com
putting value [1] into new array called "domains"
counting how many duplicates of each domain
putting all the domains that have less than 2 dupes inside new array that called "less"
runing 1 by 1 each domain in "less" array on "fileCombo" array
if "less" value was found inside "fileCombo" array value Than
write the entire line from "fileCombo" into a text file
这个脚本用于 2~5M 行的大文件,这就是为什么我需要优化它(当你在上面运行 20k 行时它很快)。
【问题讨论】:
-
使用数据库....
-
这需要同样的时间,并不是我每次都使用同一个列表,我必须将它加载到数据库中,这也需要很长时间
标签: php arrays optimization