problem with speed using include(in loop)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rainerpl
Forum Newbie
Posts: 16
Joined: Mon Jun 20, 2011 12:46 pm

problem with speed using include(in loop)

Post 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?
Dodon
Forum Commoner
Posts: 64
Joined: Wed Aug 03, 2011 4:11 am
Location: Netherlands

Re: problem with speed using include(in loop)

Post 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.
rainerpl
Forum Newbie
Posts: 16
Joined: Mon Jun 20, 2011 12:46 pm

Re: problem with speed using include(in loop)

Post 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>
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: problem with speed using include(in loop)

Post 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.
Post Reply