<?php
define('IN_SOFT', true);
define('NOTCONNECTED', true);
define('CODE_CREATEUR', 27);
include('./text_file.class.php');
set_time_limit(0);
/*
$file = fopen('./allCountries.txt', 'r');
$res = fread($file, 1024 * 1024 * 50);
fclose($file);
$file = fopen('./piece_allCountries.txt', 'w');
fwrite($file, $res);
fclose($file);
*/
// piece_allCountries.txt is build on the 1st 50 MO of allCountries.txt file
// @see: http://download.geonames.org/export/dump/
if(in_array('-1', $argv))
{
// load the file onto memory
$aTheLines = file('./piece_allCountries.txt');
var_dump(memory_get_usage() / (1024 * 1024)); // -> float(147.34832763672)(MO)
$start = microtime(true);
// access all the line(s) (Fastest but not memory optimized, for lazy (or small file))
foreach($aTheLines as $line)
{
// do something with line...
// var_dump(memory_get_usage() / (1024 * 1024)); // -> float(147.34832763672)(MO)
// never decrease neither increase
}
echo 'All lines read in: ' . (microtime(true) - $start) . "\n";
var_dump(memory_get_usage() / (1024 * 1024)); // -> float(147.34832763672)(MO)
}
else if(in_array('-2', $argv))
{
// load the file onto memory
$aTheLines = file('./piece_allCountries.txt');
var_dump(memory_get_usage() / (1024 * 1024)); // -> float(147.34832763672)(MO)
$start = microtime(true);
// access all the line(s) (memory optimized but slowest)
for($i = 0 ; $i < count($aTheLines) ; $i++)
{
// do something with line -> $aTheLines[$i]
// then we remove the line we juste used to release some memory
unset($aTheLines[$i]);
// var_dump(memory_get_usage() / (1024 * 1024)); // -> float(147.34832763672) (MO)
// slightly decrease
}
echo 'All lines read in: ' . (microtime(true) - $start) . "\n";
var_dump(memory_get_usage() / (1024 * 1024)); // -> float(72.710220336914)(MO)
}
else if(in_array('-3', $argv))
{
// $file_path = './allCountries.txt';
$file_path = './piece_allCountries.txt';
$txtFile = new CTextFile();
if(!$txtFile->Open($file_path))
{
echo 'Impossible d\'ouvrir le fichier: ' . $file_path . "\n";
return false;
}
var_dump(memory_get_usage() / (1024 * 1024)); // -> < 1MO
$txtFile->SetBufferLength(10 * 1024); // 10Ko
// $txtFile->SetLineFeed("\r"); // under Mc -> eq: Set MacintoshLineFeed
// $txtFile->SetLineFeed("\r\n"); // under windows -> eq: SetWindowsLineFeed
$txtFile->SetLineFeed("\n"); // under linux -> eq: SetLinuxLineFeed
$line = 1;
$start = microtime(true);
while($txtFile->Eof() === false)
{
$txtFile->ReadLine();
// echo $txtFile->ReadLine() . "\n";
if( ($line % 100000) == 0) // display the memory usage every 100000 lines
{
echo "\t";
var_dump(memory_get_usage() / (1024 * 1024)); // -> < 1MO
}
$line++;
}
echo 'All lines read in: ' . (microtime(true) - $start) . "\n";
var_dump(memory_get_usage() / (1024 * 1024)); // -> < 1MO
$txtFile->Close();
}
else if(in_array('-4', $argv))
{
// test with a custom line separator
$file_path = './text_file.class.php';
$txtFile = new CTextFile();
if(!$txtFile->Open($file_path))
{
echo 'Impossible d\'ouvrir le fichier: ' . $file_path . "\n";
return false;
}
$txtFile->SetLineFeed('qwerty'); // fake line feed -> The whole file should be read...
$txtFile->SetBufferLength(512); // 10 chars
$line = 1;
$start = microtime(true);
while($txtFile->Eof() === false)
{
echo $txtFile->ReadLine() . "\n";
$line++;
}
echo 'All lines read in: ' . (microtime(true) - $start) . "\n";
var_dump(memory_get_usage() / (1024 * 1024)); // -> < 1MO
$txtFile->Close();
}
else if(in_array('-5', $argv))
{
include('./textfilereader.class.php'); // By Hamid Ghorashi
// $file_path = './allCountries.txt';
$file_path = './piece_allCountries.txt';
var_dump(memory_get_usage() / (1024 * 1024)); // -> < 1MO
$test = new TextFileReader($file_path);
$line = 1;
$start = microtime(true);
$res = $test->readLine();
while($res != NULL)
{
if( ($line % 100000) == 0) // display the memory usage every 100000 lines
{
echo "\t";
var_dump(memory_get_usage() / (1024 * 1024)); // -> < 1MO
}
$line++;
$res = $test->readLine();
}
echo 'All lines read in: ' . (microtime(true) - $start) . "\n";
var_dump(memory_get_usage() / (1024 * 1024)); // -> < 1MO
}
TextFileReader
?>
|