Skip to content

Instantly share code, notes, and snippets.

@AleksMeshkov
Created February 14, 2014 07:02
Show Gist options
  • Save AleksMeshkov/8996902 to your computer and use it in GitHub Desktop.
Save AleksMeshkov/8996902 to your computer and use it in GitHub Desktop.
<?php
//BAD PHP runs out of memory on 120 iterations
public function fire()
{
set_time_limit(0);
DB::connection('mongodb')->disableQueryLog();
ini_set('xdebug.max_nesting_level', 10000);
$this->info("Done:\n");
function getCities($offset = 1)
{
return file_get_contents('https://api.vk.com/method/database.getCities?need_all=1&country_id=1&count=1000&offset=' . $offset);
}
$i = 1;
function parse($i = 1)
{
$_arr = json_decode(getCities($i), true);
if (count($_arr['response']) > 0)
{
DB::collection('cities')->insert($_arr['response']);
$i ++;
echo("\t" . $i * 1000 . "records \n");
parse($i);
}
else
{
echo("Finished.");
}
}
parse();
}
// GOOD ONE. PHP never runs out of memory. Garbage collector works exellent. AVG Mem usage: <= 30 MB
public function fire()
{
set_time_limit(0);
DB::connection('mongodb')->disableQueryLog();
$this->info("Done:\n");
$i = 1;
while (true)
{
$_arr = json_decode(file_get_contents('https://api.vk.com/method/database.getCities?need_all=1&country_id=1&count=1000&offset=' . $i), true);
if (count($_arr['response']) == 0)
{
break;
}
DB::collection('cities')->insert($_arr['response']);
// echo every 500 000 insert
if ($i % 500 == 0)
{
echo("\t" . $i * 1000 . "records \n");
}
$i ++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment