简体   繁体   English

搜寻JSON的更好方法

[英]Better way to crawl json

I have a function : 我有一个功能:

function personnesDispo($date){
  $mnsDispos = mnsDispos();
  for($j=0;$j<sizeof($mnsDispos);$j++){
    if($mnsDispos[$j]['date'] == $date){
      $mnsToday[$date][] = $mnsDispos[$j]['id'];
    }
  }
  $final = array_values(array_unique($mnsToday[$date]));
  return $final;
}

This function, crawls a json object. 此函数将检索json对象。 A big json object. 一个大的json对象。 It takes so long for the function to crawl the json file, that I mostly have a 408 error.. 该函数爬取json文件花费的时间太长,以至于我大多数人都遇到408错误。

Here is my json data : https://files.olivierlam.fr/json.txt 这是我的json数据: https : //files.olivierlam.fr/json.txt

I can't find a better way for my function to be faster 我找不到更好的方法来使我的功能更快

My second function (I think it is this one that takes too long) : 我的第二个功能(我认为这是花费太长时间的功能):

function personneDispoToday($id,$date,$heure){
  $mnsDispos = mnsDispos();
  for($j=0;$j<sizeof($mnsDispos);$j++){
    if($mnsDispos[$j]['date'] == $date AND $mnsDispos[$j]['heure'] == $heure AND $mnsDispos[$j]['id'] == $id){
      $dispo = [
        'id' => $mnsDispos[$j]['id'],
        'dispo' => $mnsDispos[$j]['dispo']
      ];
      return $dispo;
      break;
    }
  }
}

This function, is used to display 1 or 2. But is almost similar to the first function. 此功能用于显示1或2。但几乎与第一个功能相似。 I think that, combining the two function in one might help 我认为将两个功能合而为一可能会有所帮助

I dont see why your code is taking so long, but maybe there is processing you are not showing us. 我不明白您的代码为什么要花这么长时间,但是也许正在处理中,您没有向我们展示。

This code takes quite a bit less than a second to decode the JSON String and loop through the complete sample JSON and capture the new array of data for the date passed as a paramter 这段代码花了不到一秒钟的时间来解码JSON字符串并遍历完整的示例JSON,并捕获新的数据数组作为参数传递的日期

function personnesDispo(&$j_array, $date){
    $today = [];
    foreach($j_array as $obj) {
        if ( $obj->date == $date ){
            $today[$date][] = $obj;
        }
        $c++;
    }
    return $today;
}

$startTime = microtime(true);

$j_array = json_decode($s); // $s was your sample JSON String

$result = personnesDispo($j_array, '25-05-2019');
print_r($result);

$endTime = microtime(true);
$execution_time = ($endTime - $startTime)/60;
echo '<b>Total Execution Time:</b> '.$execution_time.' Mins'.PHP_EOL;
echo 'Array objects of size = ' . count($j_array);

The result is 结果是

Array
(
    [25-05-2019] => Array
        (
            [0] => stdClass Object
                (
                    [prenom] => Guillaume
                    [nom] => HUART
                    [date] => 25-05-2019
                    [heure] => 06:00:00
                    [dispo] => 1
                    [id] => 1
                )

            [1] => stdClass Object
                (
                    [prenom] => Guillaume
                    [nom] => HUART
                    [date] => 25-05-2019
                    [heure] => 07:00:00
                    [dispo] => 1
                    [id] => 1
                )

            [2] => stdClass Object
                (
                    [prenom] => Guillaume
                    [nom] => HUART
                    [date] => 25-05-2019
                    [heure] => 08:00:00
                    [dispo] => 1
                    [id] => 1
                )

    ... lots more

           [111] => stdClass Object
                (
                    [prenom] => Charly
                    [nom] => PLAIGNAUD
                    [date] => 25-05-2019
                    [heure] => 23:00:00
                    [dispo] => 1
                    [id] => 51
                )

            [112] => stdClass Object
                (
                    [prenom] => Charly
                    [nom] => PLAIGNAUD
                    [date] => 25-05-2019
                    [heure] => 24:00:00
                    [dispo] => 1
                    [id] => 51
                )

        )

)
<b>Total Execution Time:</b> 0.00014141400655111 Mins
Array objects of size = 2244

Even using a slightly rewritten version of the second function, when the JSON data is passed around as a parameter to each function instead of rebuilt inside each function the timing does not change much 即使使用稍微重写的第二个函数版本,当将JSON数据作为参数传递给每个函数,而不是在每个函数内部重建时,时序也不会发生太大变化

function personnesDispo(&$j_array, $date){
    $today = [];
    foreach($j_array as $a) {

        if ( $a->date == $date ){
            $today[$date][] = $a;
        }
    }
    return $today;
}

function personneDispoToday(&$j_array,$id,$date,$heure){
    foreach($j_array as $a) {
        if($a->date == $date AND $a->heure == $heure AND $a->id == $id){
              $dispo = [
                'id' => $a->id,
                'dispo' => $a->dispo
              ];
              return $dispo;
              break;
        }
    }
}

$startTime = microtime(true);

$j_array = json_decode($s);

$result = personnesDispo($j_array, '25-05-2019');
print_r($result);

$result2 = personneDispoToday($j_array,51,'25-05-2019','24:00:00');
print_r($result2);


$endTime = microtime(true);
echo "Start Time $startTime" . PHP_EOL;
echo "End Time $endTime" . PHP_EOL;

$execution_time = ($endTime - $startTime)/60;

echo "Execution Time $execution_time" . PHP_EOL;

echo '<b>Total Execution Time:</b> '.$execution_time.' Mins'.PHP_EOL;
echo 'Array objects of size = ' . count($j_array);

RESULTS 结果

Same big array as before plus

Array
(
    [id] => 51
    [dispo] => 1
)
Start Time 1556899734.5787
End Time 1556899734.5871
Execution Time 0.00014118353525798
<b>Total Execution Time:</b> 0.00014118353525798 Mins
Array objects of size = 2244

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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