J
J
Jony13372016-12-31 12:41:00
PHP
Jony1337, 2016-12-31 12:41:00

Is there any way to optimize this php code?

Hello.
I don't know if this code is shit or it just needs a lot of RAM on the server to execute it.

ini_set('max_execution_time', 900);
for ($i = 3000000 ; $i <= 3000120; $i++)  {
 $next = 'https://example.ru/novosti/politica.php?id='.$i;
 $curl_handle=curl_init();
curl_setopt($curl_handle, CURLOPT_URL,$next);
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_USERAGENT, 'Your application name');
$query = curl_exec($curl_handle);
curl_close($curl_handle);
 $homepage = $query; 
  if (strpos($homepage, 'window.trackErrorPage') !== false) 
{

  $inexistente [] = $i;
} 

else {
  $existente [] = $i;
}

}

I timed it here, if it performs 1 million iterations, then it will take 4k hours, which is a bit much.
What's the matter in hardware, in shit code?
If in hardware you can recommend hosting where they give servers for a test at least 1 day, what would be faster?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Сергей Семенко, 2016-12-31
@abler98

Тут ещё зависит от сервера, на который отправляются запросы. Возможно дело в этом? А вообще такое количество запросов лучше выполнять в несколько потоков.

R
Rsa97, 2016-12-31
@Rsa97

And you open a browser, in it open the development tools, the "network" tab and then open one of your pages. Check out the download time.
Let's say this Toaster page loaded ~300ms for me, respectively, a million pages will be loaded in 300'000 seconds, or about three and a half days just to get the pages itself, without taking into account the costs of the script.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question