Page 1 of 1

problem with speed using include(in loop)

Posted: Fri Sep 16, 2011 6:51 am
by rainerpl
Hi

I have a simple php page that uses classes to display user profiles as search results. On one page am showing 20 profiles(which are very simple, just age/location/name).

To display the users i include a profile template file inside a class(20 times) and that class fills in the template data according to each user. But...it takes about 18+ seconds to load the page. (which is less than 1kb in size);
if i comment out the include, then it loads instantly.
Is include function really that slow?

Would file_get_contens() and string_replace be faster way to fill in template data?

Re: problem with speed using include(in loop)

Posted: Fri Sep 16, 2011 7:34 am
by Dodon
Hard to guess, maybe there are a lot of loops inside the template you try to include? Maybe post relevant parts of the code here so we can have a look.

Re: problem with speed using include(in loop)

Posted: Fri Sep 16, 2011 7:42 am
by rainerpl
Well, there isnt much going on in the script. Very simple files
The Template class simply holds the data that is used by the included file.
This Template class gets instantiated 20 times exactly, so there arent any run-away loops(at least i hope so)

Code: Select all

<?php
class Template {
	var $data;
	var $tplPath;
	function __construct($path,$data,$display){
		$this->data = $data;
		$this->tplPath = TEMPLATE_SYS_PATH."tpl/".$path;
		if($display==true){
			$this->display();
		}
	}
	
	function display(){	
		include $this->tplPath;//commenting this out makes it work instantly otherwise up to 20 sec
		//tpl = file_get_contents($this->tplPath);
		//eval($tpl);
	}
}
?>

And this is the template file that gets included, very simple and basic.

Code: Select all

<?php
//All variables can be accessed through an array $this->data
//this template requires variables 
//age,location,bodyType,fullProfileLink
 ?>
 <div class="user_mini_profile">
	<table>
		<tr>
			<td>
				<a href="<?=$this->data["fullProfileLink"];?>"><img src="<?=$this->data["photo"];?>"></a>
			</td>
			<td>
				<table>
					<tr><td>Age:<?=$this->data["age"];?></td></tr>
					<tr><td>Location:<?=$this->data["location"];?></td></tr>
					<tr><td>Body type:<?=$this->data["bodyType"];?></td></tr>
				</table>
			</td>
			<td style="vertical-align:top;">
				<a href="<?=$this->data["fullProfileLink"];?>"><div style="float:right;margin-right:6px;">Full Profile</div></a>
			</td>
		</tr>
	</table>
</div>

Re: problem with speed using include(in loop)

Posted: Fri Sep 16, 2011 1:08 pm
by califdon
It does not surprise me that reading 20 files might take nearly 20 seconds. Disk operations are notoriously slow and if your server is busy, one second per disk access doesn't sound unreasonable to me. I think that is not an efficient way to manipulate data. Could you not use a simple database for your data, then use a simple loop to display the profiles? You could use SQLite (http://www.sqlite.org/) without worrying about a database server, assuming that you are using PHP5. This would also provide much more flexible manipulation of your data, should you need that.