简体   繁体   English

我如何有效地运行一个不会在 wamp 环境中永远执行的 PHP 脚本......?

[英]How do I efficiently run a PHP script that doesn't take forever to execute in wamp enviornemnt...?

I've made a script that pretty much loads a huge array of objects from a mysql database, and then loads a huge (but smaller) list of objects from the same mysql database.我制作了一个脚本,它几乎从 mysql 数据库中加载了大量对象,然后从同一个 mysql 数据库中加载了一个巨大(但更小)的对象列表。

I want to iterate over each list to check for irregular behaviour, using PHP.我想使用 PHP 遍历每个列表以检查不规则行为。 BUT everytime I run the script it takes forever to execute (so far I haven't seen it complete).但是每次我运行脚本时,它都需要永远执行(到目前为止我还没有看到它完成)。 Is there any optimizations I can make so it doesn't take this long to execute...?有没有我可以做的优化,所以执行起来不需要这么长时间......? There's roughly 64150 entries in the first list, and about 1748 entries in the second list.第一个列表中大约有 64150 个条目,第二个列表中大约有 1748 个条目。

This is what the code generally looks like in pseudo code.这就是代码在伪代码中通常的样子。

// an array of size 64000 containing objects in the form of {"id": 1, "unique_id": "kqiweyu21a)_"}
$items_list = [];

// an array of size 5000 containing objects in the form of {"inventory: "a long string that might have the unique_id", "name": "SomeName", id": 1};
$user_list = [];

Up until this point the results are instant... But when I do this it takes forever to execute, seems like it never ends...到目前为止,结果是即时的......但是当我这样做时,它需要永远执行,似乎它永远不会结束......

foreach($items_list as $item)
{
    foreach($user_list as $user)
    {
        if(strpos($user["inventory"], $item["unique_id"]) !== false)
        {
            echo("Found a version of the item");
        }
    }
}

Note that the echo should rarely happen.... The issue isn't with MySQL as the $items_list and $user_list array populate almost instantly.. It only starts to take forever when I try to iterate over the lists...请注意,回声应该很少发生......问题不在于 MySQL,因为 $items_list 和 $user_list 数组几乎立即填充......当我尝试遍历列表时,它只会开始永远......

With 130M iterations, adding a break will help somehow despite it rarely happens...在 130M 迭代中,添加一个中断会以某种方式有所帮助,尽管它很少发生......

foreach($items_list as $item)
{
    foreach($user_list as $user)
    {
        if(strpos($user["inventory"], $item["unique_id"])){
            echo("Found a version of the item");
            break;
        }
    }
}

alternate solutions 1 with PHP 5.6: You could also use PTHREADS and split your big array in chunks to pool them into threads... with break, this will certainly improve it. PHP 5.6 的替代解决方案 1:您还可以使用 PTHREADS 并将您的大数组拆分成块以将它们汇集到线程中...中断,这肯定会改进它。

alternate solutions 2: use PHP7, the performances improvements regarding arrays manipulations and loop is BIG.替代解决方案 2:使用 PHP7,关于数组操作和循环的性能改进很大。

Also try to sort you arrays before the loop.还尝试在循环之前对数组进行排序。 depends on what you are looking at but very oftenly, sorting arrays before will limit a much as possible the loop time if the condition is found.取决于您正在查看的内容,但通常情况下,如果找到条件,之前对数组进行排序将尽可能地限制循环时间。

Your example is almost impossible to reproduce.你的例子几乎不可能重现。 You need to provide an example that can be replicated ie the two loops as given if only accessing an array will complete extremely quickly ie 1 - 2 seconds.您需要提供一个可以复制的示例,即如果只访问一个数组将非常快地完成,即 1 - 2 秒,则给定的两个循环。 This means that either the string your searching is kilobytes or larger (not provided in question) or something else is happening ie a database access or something like that while the loops are running.这意味着您搜索的字符串要么是千字节或更大(未提供问题),要么正在发生其他事情,即数据库访问或循环运行时类似的事情。

You can let SQL do the searching for you.您可以让 SQL 为您进行搜索。 Since you don't share the columns you need I'll only pull the ones I see.由于您不共享所需的列,因此我只会提取我看到的列。

SELECT i.unique_id, u.inventory
FROM items i, users u
WHERE LOCATE(i.unique_id, u inventory)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM