duanjihe5180 2013-02-25 09:39 采纳率: 100%
浏览 72
已采纳

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

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,...)

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

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

  • 写回答

1条回答 默认 最新

  • duanla3319 2013-02-25 09:45
    关注

    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:

    • 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)

    • 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)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条