[SOLVED] Multiple include calls

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
Defkon1
Forum Newbie
Posts: 19
Joined: Thu Aug 09, 2007 9:13 am

[SOLVED] Multiple include calls

Post by Defkon1 »

hello everybody

i have a function that needs 5 small classes and 1 big class to execute properly: so i've inserted that files for inclusion in the code of the function.

at the moment it needs about 20-30sec only for include all those classes... how can i speed up the process?

thanks in advance
Last edited by Defkon1 on Fri Aug 17, 2007 5:18 am, edited 1 time in total.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Multiple include calls

Post by superdezign »

Defkon1 wrote:at the moment it needs about 20-30sec only for include all those classes... how can i speed up the process?
I doubt the problem is in the inclusion if they are just classes. Try timing it.

Code: Select all

$startTime = microtime();
include "path/to/file.php";
echo "Time to include file.php: " . (microtime() - $start);
Defkon1
Forum Newbie
Posts: 19
Joined: Thu Aug 09, 2007 9:13 am

Post by Defkon1 »

thanks for your reply.

this is my test script:

Code: Select all

<?php
echo "Start: ".date("H:i:s", time())."\n";  //start time

//timing start env
$startEngine = microtime();
include('startengine.php')
$echo "start engine: ".(microtime()-$startEngine)."\n";


//timing classes inclusions;
$startTime = microtime();
include ('/class/classe1.php');
include ('/class/classe2.php');
include ('/class/classe3.php');
include ('/class/classe4.php');
include ('/class/classe5.php');
include ('/class/classe6.php'); 
echo "classes: ".(microtime()-$startEngine)."\n";

echo "End: ".date("H:i:s", time())."\n";  //endtime
?>
and the output is:

Code: Select all

Start: 17:05:03
start engine: 0.030983
classes: -0.051248
End: 17:06:04
about 1 minute... :(
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

How big are the small classes and one big class? What are the specs of the server? Why is your classes directory in the root directory?
(#10850)
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post by miro_igov »

Add echo "Class #1: ".(microtime()-$startEngine)."\n"; after each include and you will see what makes it loading slow.
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

Large classes in PHP is usually a bad thing because they can take a LONG time to create the object. This is because every time an object is created the code is re-parsed and memory is allocated for the new object. PHP is not fast at doing this with large classes.

I would venture to bet that each of those classes you are loading has code that is being executed immediately upon loading. This would explain why you are seeing large execution times. Including files, no matter how large, is extremely FAST but if there is code that is executed after the file is included that will give the APPEARANCE the include is show when in fact it is the included file executing that is low.
Defkon1
Forum Newbie
Posts: 19
Joined: Thu Aug 09, 2007 9:13 am

Post by Defkon1 »

@arborint
small classes are about 20-30 lines, with a couple of properties and relative setter/getter methods
the big one is about 700 lines, with several methods...

@miro_igov
single classes timing (the big one is the last one)

Code: Select all

Start: 11:20:25

start engine: 0.090657 
class1: 0.10196 
class2: 0.117399 
class3: -0.745825 
class4: 0.106746 
class5: 0.039889 
class6: 0.087155 

End: 11:21:26
@AKA Panama Jack
in this test script i've just included those classes, without using them, without instantiating any object from them...


thank you all for your replies... :)
Defkon1
Forum Newbie
Posts: 19
Joined: Thu Aug 09, 2007 9:13 am

Post by Defkon1 »

sorry, i'm an idiot.

a bad path in the include function... i've used a require instead of a include to find the error.

:oops: :oops: :oops: :oops: :oops:



please close this topic and bury me under six feet of asp code lines... :oops:
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

By the way, you might want to have a look at what microtime() is returning ;) It returns a string with a space in the middle so doing maths on it won't work ;)

In PHP 5.1+ you can call microtime(true) to get a floating point number, but in older versions you need to explode() the string, add the chunks then (float) cast the result. You can see you even got a negative value back in your logic and your figures will be way out.
Defkon1
Forum Newbie
Posts: 19
Joined: Thu Aug 09, 2007 9:13 am

Post by Defkon1 »

d11wtq wrote: In PHP 5.1+ you can call microtime(true) to get a floating point number, but in older versions you need to explode() the string, add the chunks then (float) cast the result. You can see you even got a negative value back in your logic and your figures will be way out.
thanks for this extra useful info! ;-)
Post Reply