Search code examples
phpmysqlperformancemysqliunset

When should I unset in PHP to improve performance


I am using the following code:

for ($x = 0; $x < count($response[0]); $x++) { //approx count 50-60

    $t = $response[0][$x];
    $query = "INSERT INTO tableX (time,title) VALUES ('$date','$t')";

    if ($query_run = mysqli_query($mysql_connect, $query)) {

    //Call some functions if the insertion happens

    }
}

mysqli_close($mysql_connect);

Title in the table is a pimary key. I am going to call some functions only when the insertion is successful i.e., no existing title is provided. The title and date are derived from a csv file.

How can I improve the performance of this code? When should I use unset to save CPU memory cycles?


Solution

  • This question is entirely based on the wrong premises.

    First of all, nowhere using unset will save you a CPU cycle,but will rather cousume them.

    Besides, there is not much place to put unset anyway.

    Finally, there are no real issues with performance with this code. If you are experiencing any, you should meausere them, detect a real bottleneck, and then fix it, instead of poking around barking at random trees.

    What should be your actual concern, instead of fictional performance issues, is that your code wide open to sql injections and syntax errors of all sorts.