Accessing Big Arrays in PHP

前端 未结 2 463
一个人的身影
一个人的身影 2021-02-05 08:55

I\'ve been doing some profiling on different methods of accessing large(ish) arrays of data in PHP. The use case is pretty simple: some of our tools output data into PHP files

2条回答
  •  没有蜡笔的小新
    2021-02-05 09:47

    Not sure if this is exactly what your looking for but it should help out with speed and memory issues. You can use the fixed spl array:

    $startMemory = memory_get_usage();
    $array = new SplFixedArray(100000);
    for ($i = 0; $i < 100000; ++$i) {
        $array[$i] = $i;
    }
    echo memory_get_usage() - $startMemory, ' bytes';
    

    Read more on big php arrays here: http://nikic.github.com/2011/12/12/How-big-are-PHP-arrays-really-Hint-BIG.html

    Also have you thought about storing the data in a cache/memory? For example you could use mysqlite with the inmemory engine on the first execution then access data from there:

    $pdo = new PDO('sqlite::memory:');
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    // .. Use PDO as normal
    

提交回复
热议问题