简体   繁体   中英

PHP - Function with foreach slowing down site

I'm working on a project that connects to an external API. I have already made the connection and I've implemented several functions to retrieve the data, that's all working fine.

The following function however, works exactly like it should, only it slows down my website significantly ( 25 seconds + ).

Is this because of the nested foreach loop? And what can i do to refactor the code?

/**
 * @param $acti
 */
function getBijeenkomstenFromAct ($acti) {
    $acties = array();
    foreach ($acti as $act) {
        $bijeenkomsten = $this->getBijeenkomstenFromID($act['id']);
        if (in_array('Doorlopende activiteit', $act['type'])) {
            foreach ($bijeenkomsten as $bijeenkomst) {
                $acties[] = array(
                    'id'         => $act['id'],
                    'Naam'       => $act['titel'],
                    'interval'   => $act['interval'],
                    'activiteit' => $bijeenkomst['activiteit'],
                    'datum'      => $bijeenkomst['datum']
                );
            }
        } else {
            $acties[] = array (
                'id'            => $act['id'],
                'type'          => $act['type'],
                'activiteit'    => $act['titel'],
                'interval'      => $act['interval'],
                'dag'           => $act['dag'],
                'starttijd'     => $act['starttijd'],
                'eindtijd'      => $act['eindtijd']

            );
        }
    }
    return $acties;
}

The function "getBijeenkomstenfromID" is working fine and on it's own not slow at all. Just to be sure, here is the function:

/**
 * @param $activitieitID
 *
 * @return mixed
 *
 */
public function getBijeenkomstenFromID($activitieitID) {
    $options = array(
        //'datumVan'      => date('Y-m-d'),
        'activiteit'    => array (
            'activiteit'    => $activitieitID
        ),
        'limit'     => 5,
        'datumVan'  => date(("Y-m-d"))
    );
    $bijeenkomsten = $this->webservice->activiteitBijeenkomstOverzicht($this->api_key, $options);

    return $bijeenkomsten;
}

It looks like you're calling on the API from within the first foreach loop, which is not efficient.

Every time you do this:

$bijeenkomsten = $this->getBijeenkomstenFromID($act['id']);

you're adding a lot of "dead" time to your script since you have to put on with network latency, the time you need to allow for the API to actually do the work and transmit it back to you. Even though this may be quick (let's say 100ms total), if your first foreach loop iterates 100 times, you already have accumulated 10 seconds of waiting, and that's before getBijeenkomstenFromAct ($acti) has done any real processing.

The best practice here would be to split this if possible. My suggestion:

Make getBijeenkomstenFromID($activitieitID) run asynchronously on its own for all the IDs you need to lookup in the API. The key here is for it to run as a separate process and then have it pass the array it constructs to getBijeenkomstenFromAct so that it can loop and process it happily.

So yes, basically I'm suggestion that you orchestrate your process backwards for efficiency's sake

Look into curl_multi : http://php.net/manual/en/function.curl-multi-exec.php

It will let you call an external API asynchronously and process the returns all at once. Be aware that APIs often have their own limitations on asynchronous calls, and common sense dictates that you probably shouldn't be hammering a website with 200 separate calls. But if your number of calls is under a dozen or two (and the API allows it), curl_multi will do nicely.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM