0

I have a code like this:

        $res = array ();
            $i2 = 0;
            for ($i = 0; $i <10; $i++) {
                if ($table ['users'] [$i] ['value'] == null) {
                    break;
                }
                $res ['users'] [$i2] ['value'] = $table ['users'] [$i] ['value'];
                $res ['users'] [$i2] ['user_id'] = $table ['users'] [$i] ['user_id'];
                $res ['users'] [$i2] ['name'] = $table ['users'] [$i] ['name'];
                $i2 ++;
            }
            $mesto;
            for ($i3 = 0; $i3 <count ($table ['users']); $i3 ++) {
                if ($user_id == $table ['users'] [$ i3] ['user_id']) {
                    $mesto = $ i3 + 1;
                    break;
                }
            }
            $res ['currentTop'] = $mesto;
            $json = json_encode ($res, JSON_UNESCAPED_UNICODE);
            echo $json;
  • $table * - This is decoded json, table has an array users, in which objects, each with three fields * value, user_id, name * JSON looks like this:
{
"users": [{
"value": "994954359439",
"user_id": 2,
"name": "Anton Piskorskyi"
}, {
"value": 99999,
"user_id": 99999,
"name": "Anton Piskorskyi"
}, {
"value": 99998,
"user_id": 99998,
"name": "Anton Piskorskyi"
}, {
"value": 99997,
"user_id": 99997,
"name": "Anton Piskorskyi"
}, {
"value": 99996,
"user_id": 99996,
"name": "Anton Piskorskyi"
}, {
"value": 99995,
"user_id": 99995,
"name": "Anton Piskorskyi"
}, {
"value": 99994,
"user_id": 99994,
"name": "Anton Piskorskyi"
}, {
"value": 99993,
"user_id": 99993,
"name": "Anton Piskorskyi"
}, {
"value": 99992,
"user_id": 99992,
"name": "Anton Piskorskyi"
}, {
"value": 99991,
"user_id": 99991,
"name": "Anton Piskorskyi"
}]
}

Themselves such objects can be 100,000 or more. The above code takes the first 10 objects from the array and by * user_id * looks for the index of the object in which this * user_id * is located. In short, the code is used to display the top 10 players and the current position of the user. Those. this piece of code will be called quite a few times in a small amount of time. I have intel I3-71 on my computer ... and when I start spamming the opening of the page many times, the processor is loaded by ~ 50% (this is for 100,000 objects), if you put this code on the VDS, then with a large online VDS can not to export, hence the question arises: how can all this be optimized? namely this function:

   $mesto;
            for ($i3 = 0; $i3 <count ($table ['users']); $i3 ++) {
                if ($user_id == $table ['users'] [$i3] ['user_id']) {
                    $mesto = $i3 + 1;
                    break;
                }
            }
  • P.S.* Objects I call this in Json:
{
"value": "994954359439",
"user_id": 2,
"name": "Anton Piskorskyi"
}

antoshka
  • 13
  • 4
  • 1
    What is "this function"? If there is an array with 100.000 elements, parsing the code might already take some time. What have you tried to inspect the performance? Any kind of profiling, using XDebug or Blackfire, or poor-mans-profiling using `echo` to check which parts of that code take long? – Nico Haase Oct 22 '21 at 07:55
  • 4
    You're reading this 100k+ JSON file every time and parse it every time and then do the calculations every time? Using a database instead of a JSON file as storage would be a first step to improving performance here… – deceze Oct 22 '21 at 07:55
  • Where does the JSON data you 're storing in `$table` come from? – Marcel Oct 22 '21 at 08:02
  • @marcel data is taken from a file – antoshka Oct 22 '21 at 08:49

0 Answers0