Jump to content


Photo

Script taking WAY too long...


  • Please log in to reply
4 replies to this topic

#1 ChambeRFienD

ChambeRFienD
  • Members
  • PipPip
  • Member
  • 25 posts

Posted 17 August 2006 - 07:04 PM

Well, I'm writing a script to check a list of domains in a CSV file to see if they are being parked at GoDaddy or not. The script works, it just takes FOREVER to check just 2 domain names...

<h1>CSV Domain Check</h1>

<?php
$row = 1;
$handle = fopen("file.csv", "r");
while (($data = fgetcsv($handle, 500, ",")) !== FALSE) {
$num = count($data);
for ($c=0; $c < $num; $c++) {
	fopen($data[$c], "r");
	$handle2 = fopen($data[$c], "r");
	$contents = stream_get_contents($handle2, 1024);
	$matchPattern = '.*Coming Soon!.*';
	if (ereg($matchPattern, $contents, $result)) {
			echo $data[$c] . "<br />\n"; 
		} else { 
		}
	}
}
fclose($handle);
?> 

Anyone have any idea on how to make this go faster?

#2 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 19 August 2006 - 12:17 PM

I'ts probably because your using ereg() on HUGE strings (especially the sites that actually have content).

Use strpos('Coming Soon!').

#3 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 19 August 2006 - 01:51 PM

Why do you open everything twice?

	fopen($data[$c], "r");
	$handle2 = fopen($data[$c], "r");

Also you never close $handle2
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#4 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 19 August 2006 - 02:04 PM

<h1>CSV Domain Check</h1>

<?php
$row = 1;
$handle = fopen("file.csv", "r");
while (($data = fgetcsv($handle, 500, ",")) !== FALSE) {
$num = count($data);
for ($c=0; $c < $num; $c++) {
	$handle2 = fopen($data[$c], "r");
	$contents = stream_get_contents($handle2, 1024);
	if (preg_match('/.*Coming Soon!.*/', $contents, $result)) {
			echo $data[$c] . "<br />\n"; 
		} else { 
		}
	}
	fclose($handle2);
}
fclose($handle);
?>

Try that.

#5 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 19 August 2006 - 02:47 PM

Simpler and faster:

- file_get_contents() is faster than using fopen() and stream_get_contents().
- strpos() is A LOT faster than ereg() or preg_match() (Don't use those unless nessecary).

<?php
$row = 1;
$fp = fopen("file.csv", "r");
while (($data = fgetcsv($fp, 500, ",")) !== false) {
	$num = count($data);
	for ($c=0; $c < $num; $c++) {
		$contents = file_get_contents($data[$c]);
		if (strpos($contents,'Coming Soon!') !== false) {
				echo $data[$c] . "<br />\n";
		}
	}
}
fclose($fp);
?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users