初始化一个巨大的数组(50000个条目)

时间:2022-06-01 13:33:08

I'm initializing a huge array of 49605 key=>value pairs) (the array will never be changed again) $boardkey_to_values=Array(97031=>0,97531=>1,409531=>2,410031=>3,410131=>4,472031=>5,472531=>6,472631=>7,472651=>7,484531=>8,485031=>9,485151=>10,485131=>10,...)

我正在初始化一个巨大的49605键=>值对的数组)(该数组永远不会再被更改)$ boardkey_to_values = Array(97031 => 0,97531 => 1,409531 => 2,410031 => 3,410131 => 4,472031 => 5,472531 => 6,472631 => 7,472651 => 7,484531 => 8,485031 => 9,485151 => 10,485131 => 10,...)

Thing is this takes a lot of time for the compiler (40ms in averages)

事情是编译器花了很多时间(平均40ms)

I wondered if they could be a faster solution.

我想知道它们是否可以成为更快的解决方案。

I'm using a big subset of the keys in my programm (15-35k). I was using MySQL before with where_in, but it was even slower (6s in average), I was given the advice to hardcode it, and indeed, it is much faster but I wanted to optimize it even more. See the original post String to Value compare Optimizing MySQL Query

我在我的程序中使用了很大一部分键(15-35k)。我之前使用的是where_in,但是它甚至更慢(平均6s),我得到了硬编码的建议,事实上,它要快得多,但我想更优化它。查看原始文章String to Value比较Optimizing MySQL Query

1 个解决方案

#1


4  

40 ms isnt terribly slow for such a large array. But if this is on the web and multiple people are calling the PHP page, that can slow the server down. You have several options:

对于如此大的阵列,40毫秒并不是非常慢。但是如果这是在网络上并且多个人正在调用PHP页面,那么可能会降低服务器速度。你有几个选择:

  • Use multiple Ajax calls, to populate your array, after the page has rendered, i.e. sets of 10000 every few seconds (This way you can do other stuff on the page and let the array populate in its own time)

    在页面渲染后使用多个Ajax调用来填充数组,即每隔几秒就设置10000个(这样你就可以在页面上做其他的东西并让数组在自己的时间内填充)

  • Use a database, as it will be faster to search/update instead of storing it in an Array.

    使用数据库,因为搜索/更新会更快,而不是将其存储在数组中。

  • Change the program logic to only work with a few values at a time, instead of 49K of them. (kind of like pagination, where only a subset of the data is shown per page)

    将程序逻辑更改为一次仅使用几个值,而不是49K。 (有点像分页,每页只显示一部分数据)

#1


4  

40 ms isnt terribly slow for such a large array. But if this is on the web and multiple people are calling the PHP page, that can slow the server down. You have several options:

对于如此大的阵列,40毫秒并不是非常慢。但是如果这是在网络上并且多个人正在调用PHP页面,那么可能会降低服务器速度。你有几个选择:

  • Use multiple Ajax calls, to populate your array, after the page has rendered, i.e. sets of 10000 every few seconds (This way you can do other stuff on the page and let the array populate in its own time)

    在页面渲染后使用多个Ajax调用来填充数组,即每隔几秒就设置10000个(这样你就可以在页面上做其他的东西并让数组在自己的时间内填充)

  • Use a database, as it will be faster to search/update instead of storing it in an Array.

    使用数据库,因为搜索/更新会更快,而不是将其存储在数组中。

  • Change the program logic to only work with a few values at a time, instead of 49K of them. (kind of like pagination, where only a subset of the data is shown per page)

    将程序逻辑更改为一次仅使用几个值,而不是49K。 (有点像分页,每页只显示一部分数据)