Welcome Guest, Not a member yet? Register   Sign In
huge php array
#1

[eluser]Reynolds[/eluser]
Hello guys,

If I load a thousands of data into a PHP array, assuming the data came from iteration in a db result.
Does it eat a lot of memory? and how is it computed? is it feasible? if not what could be the best solution?

The problem is, I need to sort (shown as list on view) a computation result from several fields. But I was thinking if I just put all db result into an array, then compute it, finally sort it. Or should I just create a MYSQL FUNCTION to do the computation? so I could sort without creating a PHP array?

Thanks in advance,
#2

[eluser]Reynolds[/eluser]
is this test code correct?
for just a hundred thousand, it result to 14,076,923.00 bytes
Code:
<?php
    
    
    $top_num = 100000; // hundred thousand
    
    if(isset($_GET['n']))
    {
        $top_num = $_GET['n'];
    }
    $t = array();
    for($i=0;$i<=$top_num;$i++) //
    {
        $t[$i][0] = "skjdahld ahsjdh a;sdjah ;dha ldah sldahdl adhal dhak djahj ahdslj hasdlkhj adlkjh aldkah lskdh adklh aldh adh";
        $t[$i][1] = "skjdahld ahsjdh a;sdjah ;dha ldah sldahdl adhal dhak djahj ahdslj hasdlkhj adlkjh aldkah lskdh adklh aldh adh";
        $t[$i][2] = "skjdahld ahsjdh a;sdjah ;dha ldah sldahdl adhal dhak djahj ahdslj hasdlkhj adlkjh aldkah lskdh adklh aldh adh";
        $t[$i][3] = "skjdahld ahsjdh a;sdjah ;dha ldah sldahdl adhal dhak djahj ahdslj hasdlkhj adlkjh aldkah lskdh adklh aldh adh";
        $t[$i][4] = "skjdahld ahsjdh a;sdjah ;dha ldah sldahdl adhal dhak djahj ahdslj hasdlkhj adlkjh aldkah lskdh adklh aldh adh";
    }
    echo number_format((memory_get_usage()/8),2) . " bytes"; // in bytes
?&gt;
#3

[eluser]richthegeek[/eluser]
PHP uses a linked list for it's arrays, as opposed to what is naturally thought of as an array (that is, "n" fixed sized frames of a specific type, or a list of memory pointers, all sequentially stored in the memory space). The computational expense with a linked list is greater than for a classical array due to the way the data is structured, and so searching through (and thus sorting) a PHP array is expensive. Some people will say that a mergesort algorithm on a linked list acts at the theoretical maximum for sorting of O(n log n) (big Oh) but in general that isn't the case.

MySQL on the other hand should have each column you want to test pre-sorted (if you index the columns, which you should for anything required in a WHERE, ORDER, or GROUP clause) so the cost is greatly reduced so the only cost is from calculation. Secondly MySQL has type strictness on each column so it doesn't have to test the cast on each figure before calculation.

tl;dr : doing everything in MySQL is (most probably) the faster solution.
#4

[eluser]John_Betong[/eluser]
http://johns-jokes.com/c_five_col

The above link shows links to all the jokes on my website in a 5-column format. I was surprised at how fast it took to extract the alphabetical sorted data, add the anchors and display the joke titles (over 1,700).

&nbsp;
http://johns-jokes.com/all-the-jokes

Recently I added the above page utilising jQuery and it is slower by a couple of seconds. Additional PHP processing was necessary to group the joke titles into batches of about 20. I was impressed at the results.

I think that it is far quicker to try and extract the sorted data using MySQL rather than use PHP.
&nbsp;
&nbsp;
&nbsp;




Theme © iAndrew 2016 - Forum software by © MyBB