I’ve got two files,file a around 5mb, and file b around 66 mb. I need to find out if there’s any occurnaces of the lines in file a, inside file b, and if so write them to file c.
This is the way I’m currently handling it:
ini_set("memory_limit","1000M");
set_time_limit(0);
$small_list=file("a.csv");
$big_list=file_get_contents("b.csv");
$new_list="c.csv";
$fh = fopen($new_list, 'a');
foreach($small_list as $one_line)
{
if(stristr($big_list, $one_line) != FALSE)
{
fwrite($fh, $one_line);
echo "record found: " . $one_line ."<br>";
}
}
The issue is its been running(successfully) for over an hour and its maybe 3,000 lines into the 160,000 in the smaller file. Any ideas?
Build arrays with hashes as indices:
Read in file
a.csvline by line and store ina_hash[md5($line)] = array($offset, $length)Read in file
b.csvline by line and store inb_hash[md5($line)] = trueBy using the hashes as indices you will automagically not wind up having duplicate entries.
Then for every hash that has an index in both a_hash and b_hash read in the contents of the file (using offset and length you stored in a_hash) to pull out the actual line text. If you’re paranoid about hash collisions then store offset/length for b_hash as well and verify with stristr.
This will run a lot faster and use up far, far, FAR less memory.
If you want to reduce memory requirement further and don’t mind checking duplicates then:
Read in file
a.csvline by line and store ina_hash[md5($line)] = falseRead in file
b.csvline by line, hash the line and check if exists in a_hash.If
a_hash[md5($line)] == falsewrite toc.csvand seta_hash[md5($line)] = trueSome example code for the second suggestion: