Validation of PHP file

Asked

Viewed 68 times

1

I have a term insertion system via CSV files, example:

09999999;José
08888888;Maria

I get this file to move to the server and then I open this file to insert into the database. My problem is that I need to validate the insertion, I cannot insert repeat phones in the same file and for that I use this code for this:

$valida1 = array_search($numero1, $validaNumeroLista);

if (empty($valida1) ) 
{
    array_push($validaNumeroLista, $numero1);
}

After that I make an insertion in the bank, the problem is that the insertion time has increased a lot.

For example:

Before entering this validation, a file with up to 20,000 lines would take about 5 to 7 seconds. Now with 1,000 lines it takes more than 2 minutes. Above 2,000 lines it is impossible to insert.

Have any tips on how to improve this performance?

1 answer

0


Use the array as a hash table instead of a normal array. The way you are using, with the array_push, is getting all the indices sequential, something like this:

[0] => "09999999",
[1] => "08888888",
[2] => "07777777"

This implies that when you do array_search in this array, it will cost O(n). This means that on average you will have to go through half the array to find the element, and so it grows a lot with the size of the Array.

Instead use your phone number as a key, like this:

if (!array_key_exists($numero1, $validaNumeroLista){
    $validaNumeroLista[$numero1] = true;
}

Notice how now the existence test was done with the function array_key_exists, that checks if a certain key exists in the array. If there is no new entry for that same key with the value true. The value could be any other because we are only interested in registering that key as existing.

Another way to test if the key doesn’t exist would be:

if (!isset($validaNumeroLista[$numero1])){

By logging in the keys we make the array to be used as a hash table, and then look as follows:

["09999999"] => true,
["08888888"] => true,
["07777777"] => true

In this case the key search is of constant cost O(1), and therefore it will be much faster

  • Thanks Isac, for the performance information and the resolution, in my case worked with the option: if (!array_key_exists($numero1, $validaNumeRista)){ $validaNumerList[$numero1] = true; }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.