Welcome Guest, Not a member yet? Register   Sign In
Extremely long execution time
#1

[eluser]Unknown[/eluser]
I benchmarked this code and it takes ~25 seconds to execute
There is a lot of data inside $kategorije array (it is a multi-leve array containing mysql query results (around 1000 rows of data total).
In other parts of web application I handle a lot more data (10000+ rows) without problems...
Could the nested foreach statements cause such a slowdown?
I can't really see any other way to output data in this manner (hierarchy).
Any ideas?

Code:
foreach($kategorije as $k)
            {
                echo '<li style="cursor: pointer; border-bottom: 1px solid #ccc; margin: 5px;"><b style="color: black;">' . $n . ') </b> ' . $k['ime'] . '</li>';
                    if($podkat)
                    {
                        foreach($podkat as $p)
                        {
                            if($p['kategorija']==$k['id'])
                            {
                                $unijeta = FALSE;
                                foreach($opasnost as $x)
                                {
                                    if($x['ime']==$p['id'])
                                    {
                                        if($x['rmjesto']==$rmjesto) $unijeta = TRUE;
                                    }
                                }
                                
                                echo '<li style="cursor: pointer;';
                                if($unijeta==TRUE) echo 'color: red; margin-left: 20px; font-size: 11px;';
                                else echo 'padding: 2px 0 3px 0;border-bottom: 1px solid gray; display: none;"';
                                echo ' id="drugilvl"  title="' . $p['id'] . '" class="' . $p['kategorija'] . '"';
                                if($unijeta==TRUE) echo '>'.$p['ime'].'</li>';
                                else echo 'onClick="forma(this)">'.$p['ime'] . '</li>';
                            }
                        }
                    }
                    $n++;
            }

Thx in advance...
Edit: I have just noticed something -
variable $opasnost contains around ~25 000 rows...
But again - ~25 seconds?
#2

[eluser]stef25[/eluser]
Nested foreach will absolutely cause problems like this. I had the same problem on a site of mine. With 10 rows of test data I didn't notice it but with 300 rows of real data it took 5+ sec to load the page. And that was with only 2 levels of foreach, you have three.

If the first array contains 1000 elements and the second one does too then you have 1000 x 1000 iterations = 1.000.000 loops. Add another 1000 in your third array and you have a billion iterations. Put a counter on it to make sure I'm not wrong.

If you change your code to foreach($kategorije as $k) print_r($k) then I'm sure it will be less than a sec. Since you have no queries, it can not be much else than nested foreaches Smile
#3

[eluser]danmontgomery[/eluser]
http://ellislab.com/codeigniter/user-gui...hmark.html




Theme © iAndrew 2016 - Forum software by © MyBB