0

I have a php file that connects to a DB and grabs some resources for me.

On the while loop I'm appending the data to a php array. Finally, json_encode is used with this resulting array.

For now, I'm using array_push, but for the number of times it is called, the script is extremely slow! I've tried to change to "[]" to append to the last element but I haven't noticed any change on performance.

My question is: Is there a better method (and a lot faster!). I've tried also with array_splice and array_merge, but without luck,

Edit: By commenting the array_push part (but accessing the same number of rows and times) the script is fast. So, this leads me to think that the bottleneck is in the push.

Edit2:

The code

    $Tema=-1;
    $SubTema=-1;
    $Topico=-1;
    $SubTopico=-1;
    $RS = mysql_query($SQL);
    $respArray['items'] = array();
    $resLevel=-1;
    $UniqueKey=1;

    $TemaId = '';
    $TemaOpen = '';
    $TemaActivo = '';
    $TemaIsRecurso = '';
    $TemaTipoRecurso = '';

    $SubtemaId = '';
    $SubtemaOpen = '';
    $SubtemaActivo = '';
    $SubtemaIsRecurso = '';
    $SubtemaTipoRecurso = '';

    $TopicoId = '';
    $TopicoOpen = '';
    $TopicoActivo = '';
    $TopicoIsRecurso = '';
    $TopicoTipoRecurso = '';

    $SubtopicoId = '';
    $SubtopicoOpen = '';
    $SubtopicoIsRecurso = '';
    $SubtopicoTipoRecurso = '';

    while($row = mysql_fetch_array($RS))   
    {

        if(!is_null($row['IDTEMA'])){
            if($Tema!=$row['IDTEMA']){
                $TemaId = $row['IDTEMA'];
                $TemaOpen = '1';
                $TemaActivo = $row['MACTIVO'];
                $TemaTitle = $row['TEMA'];
                $TemaIsRecurso = '0';
                $TemaTipoRecurso = '';

                $Tema=$row['IDTEMA'];
                $SubTema=-1;

                $respArray['items'][] = array ('Id' => $TemaId, 'isRecurso' => '0', 'Level' => '1', 'Title' => $TemaTitle, 'UniqueKey' => $UniqueKey++, 'children_items' => array() );
                continue;
                //array_push($respArray['items'], array ('Id' => $TemaId, 'isRecurso' => '0', 'Level' => '1', 'Title' => $TemaTitle, 'UniqueKey' => $UniqueKey++, 'children_items' => array() ));
                //echo("TEMA ".$TemaTitle."\n");
            }

            if($row['IDSUBTEMA']!=""){
                if($SubTema!=$row['IDSUBTEMA']){
                    $SubtemaId = $row['IDSUBTEMA'];
                    $SubtemaOpen = '1';
                    $SubtemaActivo = $row['MACTIVO'];
                    $SubtemaTitle = $row['SUBTEMA'];
                    $SubtemaIsRecurso = '0';
                    $SubtemaTipoRecurso = '';

                    $SubTema=$row['IDSUBTEMA'];
                    $Topico=-1;


                    $numTemas = count($respArray['items'])-1;
                    $respArray['items'][$numTemas]['children_items'][] = array ('Id' => $SubtemaId, 'isRecurso' => '0', 'Level' => '2', 'Title' => $SubtemaTitle, 'UniqueKey' => $UniqueKey++, 'children_items' => array() );
                    continue;
                }

                if($row['IDTOPICO']!=""){
                    if($Topico!=$row['IDTOPICO']){
                        $TopicoId = $row['IDTOPICO'];
                        $TopicoOpen = '1';
                        $TopicoActivo = $row['MACTIVO'];
                        $TopicoTitle = $row['TOPICO'];
                        $TopicoIsRecurso = '0';
                        $TopicoTipoRecurso = '';

                        $Topico=$row['IDTOPICO'];
                        $SubTopico=-1;

                        $numTemas = count($respArray['items'])-1;
                        $numSubTemas = count($respArray['items'][$numTemas]['children_items'])-1;
                        $respArray['items'][$numTemas]['children_items'][$numSubTemas]['children_items'][] = array ('Id' => $TopicoId, 'isRecurso' => '0', 'Level' => '3', 'Title' => $TopicoTitle, 'UniqueKey' => $UniqueKey++, 'children_items' => array() );
                        continue;
                    }

                    if($row['IDSUBTOPICO']!=""){
                        if($SubTopico!=$row['IDSUBTOPICO']){
                            $SubtopicoId = $row['IDTOPICO'];
                            $SubtopicoOpen = '1';
                            $SubtopicoActivo = $row['MACTIVO'];
                            $SubtopicoTitle = $row['SUBTOPICO'];
                            $SubtopicoIsRecurso = '0';
                            $SubtopicoTipoRecurso = '';

                            $SubTopico=$row['IDSUBTOPICO'];

                            $numTemas = count($respArray['items'])-1;
                            $numSubTemas = count($respArray['items'][$numTemas]['children_items'])-1;
                            $numTopicos = count($respArray['items'][$numTemas]['children_items'][$numSubTemas]['children_items'])-1;
                            $respArray['items'][$numTemas]['children_items'][$numSubTemas]['children_items'][$numTopicos]['children_items'][] = array ('Id' => $SubtopicoId, 'Level' => '4', 'isRecurso' => '0', 'Title' => $SubtopicoTitle, 'UniqueKey' => $UniqueKey++, 'children_items' => array() );
                            continue;
                        }
                        if($row['IDRECURSO']!=""){

                            $resId = $row['IDRECURSO'];
                            $resLevel = '5';
                            $resFic = ($row['IDCATEGORIA']==$BD->ID_CATEGORIA_PUBSEC) ? $row['FICSRC'] . "$" . $row['FICVOL'] : ((($row['IDCATEGORIA']==$BD->ID_CATEGORIA_LINKS) || ($row['IDCATEGORIA']==$BD->ID_CATEGORIA_PAGINAS)) ? $row['FICSRC'] : $row['FICHEIRO']);
                            $resIcon = $row['ICON'];
                            $resCat = $row['IDCATEGORIA'];
                            $resFormato =  $row['IDFORMATO'];
                            $resProf = (($row['IDDESTINATARIO']==3) || ($row['IDDESTINATARIO']==4)) ?  1 : 0;
                            $resInfo = $row['INFORMACAO'];
                            $resDown = $row['GRAVACAO'];
                            $resFicD = $row['FICDOWNLOAD'];
                            $resGuia = $row['GUIA'];
                            $resVol = $row['VOLUME'];
                            $resIdFavorito = $row['IDFAVORITO'];
                            $resActivo = $row['MACTIVO'];
                            $resTempoGasto = $row['TEMPOGASTO'];
                            $resNumSessoes = $row['NUMSESSOES'];
                            $resLastDate = $row['LASTDATE'];
                            $resScore = $row['SCORE'];
                            $resProgresso = $row['PROGRESSO'];
                            $resTitle = utf8_encode($row['RECURSO']);
                            $resIsRecurso = '1';

                            $numTemas = count($respArray['items'])-1;
                            $numSubTemas = count($respArray['items'][$numTemas]['children_items'])-1;
                            $numTopicos = count($respArray['items'][$numTemas]['children_items'][$numSubTemas]['children_items'])-1;
                            $numSubTopicos = count($respArray['items'][$numTemas]['children_items'][$numSubTemas]['children_items'][$numTopicos]['children_items'])-1;

                            $respArray['items'][$numTemas]['children_items'][$numSubTemas]['children_items'][$numTopicos]['children_items'][$numSubTopicos]['children_items'][] = array (
                                    'Id' => $resId, 
                                    'Level' => $resLevel,
                                    'Fic' => $resFic,
                                    "Icon" => $resIcon,
                                    "Cat" => $resCat,
                                    "Formato" => $resFormato,
                                    "Prof" => $resProf,
                                    "Info" => $resInfo,
                                    "Down" => $resDown,
                                    "FicD" => $resFicD,
                                    "Guia" => $resGuia,
                                    "Vol" => $resVol,
                                    "IdFavorito" => $resIdFavorito,
                                    "Activo" => $resActivo,
                                    "TempoGasto" => $resTempoGasto,
                                    "NumSessoes" => $resNumSessoes,
                                    "LastDate" => $resLastDate,
                                    "Score" => $resScore,
                                    "Progresso" => $resProgresso,
                                    'Title' => $resTitle,
                                    'isRecurso' => '1',
                                    'colorIcon' => '#FF2200',
                                    'tipoRecurso' => 'tipo_recurso',
                                    'UniqueKey' => $UniqueKey++,
                                    'children_items' => null );
                                    continue;

                        }

                    }else{
                        if($row['IDRECURSO']!=""){
                            $resId = $row['IDRECURSO'];
                            $resLevel = '4';
                            $resFic = ($row['IDCATEGORIA']==$BD->ID_CATEGORIA_PUBSEC) ? $row['FICSRC'] . "$" . $row['FICVOL'] : ((($row['IDCATEGORIA']==$BD->ID_CATEGORIA_LINKS) || ($row['IDCATEGORIA']==$BD->ID_CATEGORIA_PAGINAS)) ? $row['FICSRC'] : $row['FICHEIRO']);
                            $resIcon = $row['ICON'];
                            $resCat = $row['IDCATEGORIA'];
                            $resFormato =  $row['IDFORMATO'];
                            $resProf = (($row['IDDESTINATARIO']==3) || ($row['IDDESTINATARIO']==4)) ?  1 : 0;
                            $resInfo = $row['INFORMACAO'];
                            $resDown = $row['GRAVACAO'];
                            $resFicD = $row['FICDOWNLOAD'];
                            $resGuia = $row['GUIA'];
                            $resVol = $row['VOLUME'];
                            $resIdFavorito = $row['IDFAVORITO'];
                            $resActivo = $row['MACTIVO'];
                            $resTempoGasto = $row['TEMPOGASTO'];
                            $resNumSessoes = $row['NUMSESSOES'];
                            $resLastDate = $row['LASTDATE'];
                            $resScore = $row['SCORE'];
                            $resProgresso = $row['PROGRESSO'];
                            $resTitle = utf8_encode($row['RECURSO']);
                            $resIsRecurso = '1';

                            $numTemas = count($respArray['items'])-1;
                            $numSubTemas = count($respArray['items'][$numTemas]['children_items'])-1;
                            $numTopicos = count($respArray['items'][$numTemas]['children_items'][$numSubTemas]['children_items'])-1;

                            $respArray['items'][$numTemas]['children_items'][$numSubTemas]['children_items'][$numTopicos]['children_items'][] = array (
                                    'Id' => $resId, 
                                    'Level' => $resLevel,
                                    'Fic' => $resFic,
                                    "Icon" => $resIcon,
                                    "Cat" => $resCat,
                                    "Formato" => $resFormato,
                                    "Prof" => $resProf,
                                    "Info" => $resInfo,
                                    "Down" => $resDown,
                                    "FicD" => $resFicD,
                                    "Guia" => $resGuia,
                                    "Vol" => $resVol,
                                    "IdFavorito" => $resIdFavorito,
                                    "Activo" => $resActivo,
                                    "TempoGasto" => $resTempoGasto,
                                    "NumSessoes" => $resNumSessoes,
                                    "LastDate" => $resLastDate,
                                    "Score" => $resScore,
                                    "Progresso" => $resProgresso,
                                    'Title' => $resTitle,
                                    'isRecurso' => '1',
                                    'colorIcon' => '#FF2200',
                                    'tipoRecurso' => 'tipo_recurso',
                                    'UniqueKey' => $UniqueKey++,
                                    'children_items' => null );
                                    continue;
                        }
                    }



                }else{
                    if($row['IDRECURSO']!="" && $row['IDSUBTEMA']!=""){
                        $resId = $row['IDRECURSO'];
                        $resLevel = '3';
                        $resFic = ($row['IDCATEGORIA']==$BD->ID_CATEGORIA_PUBSEC) ? $row['FICSRC'] . "$" . $row['FICVOL'] : ((($row['IDCATEGORIA']==$BD->ID_CATEGORIA_LINKS) || ($row['IDCATEGORIA']==$BD->ID_CATEGORIA_PAGINAS)) ? $row['FICSRC'] : $row['FICHEIRO']);
                        $resIcon = $row['ICON'];
                        $resCat = $row['IDCATEGORIA'];
                        $resFormato =  $row['IDFORMATO'];
                        $resProf = (($row['IDDESTINATARIO']==3) || ($row['IDDESTINATARIO']==4)) ?  1 : 0;
                        $resInfo = $row['INFORMACAO'];
                        $resDown = $row['GRAVACAO'];
                        $resFicD = $row['FICDOWNLOAD'];
                        $resGuia = $row['GUIA'];
                        $resVol = $row['VOLUME'];
                        $resIdFavorito = $row['IDFAVORITO'];
                        $resActivo = $row['MACTIVO'];
                        $resTempoGasto = $row['TEMPOGASTO'];
                        $resNumSessoes = $row['NUMSESSOES'];
                        $resLastDate = $row['LASTDATE'];
                        $resScore = $row['SCORE'];
                        $resProgresso = $row['PROGRESSO'];
                        $resTitle = utf8_encode($row['RECURSO']);
                        $resIsRecurso = '1';

                        $numTemas = count($respArray['items'])-1;
                        $numSubTemas = count($respArray['items'][$numTemas]['children_items'])-1;

                        $respArray['items'][$numTemas]['children_items'][$numSubTemas]['children_items'][] = array (
                                'Id' => $resId, 
                                'Level' => $resLevel,
                                'Fic' => $resFic,
                                "Icon" => $resIcon,
                                "Cat" => $resCat,
                                "Formato" => $resFormato,
                                "Prof" => $resProf,
                                "Info" => $resInfo,
                                "Down" => $resDown,
                                "FicD" => $resFicD,
                                "Guia" => $resGuia,
                                "Vol" => $resVol,
                                "IdFavorito" => $resIdFavorito,
                                "Activo" => $resActivo,
                                "TempoGasto" => $resTempoGasto,
                                "NumSessoes" => $resNumSessoes,
                                "LastDate" => $resLastDate,
                                "Score" => $resScore,
                                "Progresso" => $resProgresso,
                                'Title' => $resTitle,
                                'isRecurso' => '1',
                                'colorIcon' => '#FF2200',
                                'tipoRecurso' => 'tipo_recurso',
                                'UniqueKey' => $UniqueKey++,
                                'children_items' => null );
                                continue;
                    }
                }

            }else{

                if($row['IDRECURSO']!=""){
                    $resId = $row['IDRECURSO'];
                    $resLevel = '2';
                    $resFic = ($row['IDCATEGORIA']==$BD->ID_CATEGORIA_PUBSEC) ? $row['FICSRC'] . "$" . $row['FICVOL'] : ((($row['IDCATEGORIA']==$BD->ID_CATEGORIA_LINKS) || ($row['IDCATEGORIA']==$BD->ID_CATEGORIA_PAGINAS)) ? $row['FICSRC'] : $row['FICHEIRO']);
                    $resIcon = $row['ICON'];
                    $resCat = $row['IDCATEGORIA'];
                    $resFormato =  $row['IDFORMATO'];
                    $resProf = (($row['IDDESTINATARIO']==3) || ($row['IDDESTINATARIO']==4)) ?  1 : 0;
                    $resInfo = $row['INFORMACAO'];
                    $resDown = $row['GRAVACAO'];
                    $resFicD = $row['FICDOWNLOAD'];
                    $resGuia = $row['GUIA'];
                    $resVol = $row['VOLUME'];
                    $resIdFavorito = $row['IDFAVORITO'];
                    $resActivo = $row['MACTIVO'];
                    $resTempoGasto = $row['TEMPOGASTO'];
                    $resNumSessoes = $row['NUMSESSOES'];
                    $resLastDate = $row['LASTDATE'];
                    $resScore = $row['SCORE'];
                    $resProgresso = $row['PROGRESSO'];
                    $resTitle = utf8_encode($row['RECURSO']);
                    $resIsRecurso = '1';

                    $numTemas = count($respArray['items'])-1;

                    $respArray['items'][$numTemas]['children_items'][] = array (
                        'Id' => $resId, 
                        'Level' => $resLevel,
                        'Fic' => $resFic,
                        "Icon" => $resIcon,
                        "Cat" => $resCat,
                        "Formato" => $resFormato,
                        "Prof" => $resProf,
                        "Info" => $resInfo,
                        "Down" => $resDown,
                        "FicD" => $resFicD,
                        "Guia" => $resGuia,
                        "Vol" => $resVol,
                        "IdFavorito" => $resIdFavorito,
                        "Activo" => $resActivo,
                        "TempoGasto" => $resTempoGasto,
                        "NumSessoes" => $resNumSessoes,
                        "LastDate" => $resLastDate,
                        "Score" => $resScore,
                        "Progresso" => $resProgresso,
                        'Title' => $resTitle,
                        'isRecurso' => '1',
                        'colorIcon' => '#FF2200',
                        'tipoRecurso' => 'tipo_recurso',
                        'UniqueKey' => $UniqueKey++,
                        'children_items' => null );
                        continue;
                } 

            }


        }else{
            if($row['IDRECURSO']!=""){
                $resId = $row['IDRECURSO'];
                $resLevel = '1';
                $resFic = ($row['IDCATEGORIA']==$BD->ID_CATEGORIA_PUBSEC) ? $row['FICSRC'] . "$" . $row['FICVOL'] : ((($row['IDCATEGORIA']==$BD->ID_CATEGORIA_LINKS) || ($row['IDCATEGORIA']==$BD->ID_CATEGORIA_PAGINAS)) ? $row['FICSRC'] : $row['FICHEIRO']);
                $resIcon = $row['ICON'];
                $resCat = $row['IDCATEGORIA'];
                $resFormato =  $row['IDFORMATO'];
                $resProf = (($row['IDDESTINATARIO']==3) || ($row['IDDESTINATARIO']==4)) ?  1 : 0;
                $resInfo = $row['INFORMACAO'];
                $resDown = $row['GRAVACAO'];
                $resFicD = $row['FICDOWNLOAD'];
                $resGuia = $row['GUIA'];
                $resVol = $row['VOLUME'];
                $resIdFavorito = $row['IDFAVORITO'];
                $resActivo = $row['MACTIVO'];
                $resTempoGasto = $row['TEMPOGASTO'];
                $resNumSessoes = $row['NUMSESSOES'];
                $resLastDate = $row['LASTDATE'];
                $resScore = $row['SCORE'];
                $resProgresso = $row['PROGRESSO'];
                $resTitle = utf8_encode($row['RECURSO']);
                $resIsRecurso = '1';

                $respArray['data']['items'][] = array (
                        "Id" => $resId,
                        'Level' => $resLevel,
                        "Fic" => $resFic,
                        "Icon" => $resIcon,
                        "Cat" => $resCat,
                        "Formato" => $resFormato,
                        "Prof" => $resProf,
                        "Info" => $resInfo,
                        "Down" => $resDown,
                        "FicD" => $resFicD,
                        "Guia" => $resGuia,
                        "Vol" => $resVol,
                        "IdFavorito" => $resIdFavorito,
                        "Activo" => $resActivo,
                        "TempoGasto" => $resTempoGasto,
                        "NumSessoes" => $resNumSessoes,
                        "LastDate" => $resLastDate,
                        "Score" => $resScore,
                        "Progresso" => $resProgresso,
                        'Title' => $resTitle,
                        'isRecurso' => '1',
                        'colorIcon' => '#FF2200',
                        'tipoRecurso' => 'tipo_recurso',
                        'UniqueKey' => $UniqueKey++,
                        'children_items' => null );
                        continue;

            }
        }
    }

    mysql_close($link);

    echo json_encode($respArray) ;
} 

Thank you

jose
  • 2,685
  • 4
  • 36
  • 48
  • 3
    Are you certain the array_push part is the bottleneck and not the query itself? It sounds very unlikely that is the case. Anyways, for a loop in which you are adding one element at a time, [`[]` is faster](http://www.php.net/array_push), but I really don't think that's your problem. – Paolo Bergantino Jun 15 '12 at 16:26
  • 1
    It's far more likely that your slowness is at the DB level than the PHP level. Why don't you post the code for your loop. – Jason McCreary Jun 15 '12 at 16:26
  • I've edited the question, thank you – jose Jun 15 '12 at 16:29
  • How many rows are we talking about here? – Paolo Bergantino Jun 15 '12 at 16:30
  • @PaoloBergantino 267 rows in this case – jose Jun 15 '12 at 16:33
  • @jose, all this is speculation without seeing code. Please post your code for a proper answer. – Jason McCreary Jun 15 '12 at 16:35
  • @jose - Do the rows have columns that are BLOB or something similar with huge amounts of data? – Paolo Bergantino Jun 15 '12 at 16:38
  • @PaoloBergantino I've updated the question with the code. Thank you – jose Jun 15 '12 at 16:40
  • could you please mark in the code where according to your tests the problem is? BTW, 267 rows is a really small number! – Walter Tross Jun 15 '12 at 16:48
  • Yes, indeed a small number :) Well, the problem is on each push operation, i.e. $respArray['items'][$numTemas]['children_items'][] ... If I comment this lines the script runs very fast – jose Jun 15 '12 at 16:54
  • 1
    could it be that you have only a small amount of memory available, that is eaten away by all those arrays of arrays? (arrays are more memory-hungry than one would like - if I remember well, an empty array takes up nearly 1kB). Try using memory_get_usage before and after array filling, and try to find out how much physical memory you have available. – Walter Tross Jun 15 '12 at 17:03
  • wait a second, you timed the script WITHOUT the json_encode() and without output transfer times, right? – Walter Tross Jun 15 '12 at 17:13
  • @WalterTross hum .. yes – jose Jun 15 '12 at 17:19
  • 1
    With (e.g.) XDebug, you can [profile](http://stackoverflow.com/q/4254110/) the script and find out with certainty exactly where it's spending its time, and how much. – outis Oct 17 '12 at 20:49
  • Consider using objects instead of assoc arrays. – SOFe Jan 02 '16 at 14:28

0 Answers0