Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Function call painfully slow

Status
Not open for further replies.

Bubastis

Technical User
Nov 11, 2003
11
US
I'm using php 4.3.8. I have a big class called template which has a function pparse which takes one argument.

In my code, I do a timestamp (using microtime), then I execute $template->pparse('body'). The first line of the pparse function then is another timestamp. Often 4 seconds or more pass between these timestamps. Any suggestion why? I'm running the php on a debian Linux system with apache; this is part of a web page.

Thanks.
 
Here's the function: The crap at the beginning is the timestamp. Not clear to me why what's after it should even matter as far as causing the delay is concerned.

Could there be a problem with general php configuration that might cause such an error?

function pparse($handle){
//time stamp
global $pt1a;
$outcommon = microtime();
$outcommon = explode(" ",$outcommon);
$outcommon = $outcommon[1] + $outcommon[0];
$pt1a = $outcommon;

global $board_config;

$this->xs_startup();
// checking if handle exists
if (empty($this->files[$handle]) && empty($this->files_cache[$handle])) {
die("Template->loadfile(): No files found for handle $handle");
}
// checking if php file exists.
// also check if we have loaded uncompiled code
// (attachment_mod might have put uncompiled code)
if (!empty($this->files_cache[$handle]) && empty($this->uncompiled_code[$handle])) {
// php file exists - running it instead of tpl
$this->execute($this->files_cache[$handle],'');
return true;
}
else
{
if (!$this->loadfile($handle))
{
die("Template->pparse(): Couldn't load template file for handle $handle");
}
// actually compile the template now.
if (empty($this->compiled_code[$handle]))
{
// Actually compile the code now.
if(!empty($this->files_cache2[$handle]) && empty($this->files_cache[$handle]))
{
$this->compiled_code[$handle] = $this->compile2($this->uncompiled_code[$handle], $this->files_cache2[$handle]);
}
else
{
$this->compiled_code[$handle] = $this->compile2($this->uncompiled_code[$handle]);
}
}
// Run the compiled code.

$this->execute('', $this->compiled_code[$handle]);

return true;
}
}
 
There's so much going on in that function that it's impossible to say what is causing the slowdown.

Footprint your function to see exactly what is taking the most time. Then footprint whichever method invocation is taking the most time.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Time for a couple stupid questions:

Given that my timestamping is the first line of the function, why does the rest of the function even matter?

What do you mean by 'footprint'? The timestamping? When I put timestamps all over this function, everything inside runs quickly - the big step in time is going from the line before the function call in the main code to the first line of this function.
 
Footprint". Add some statements like:

print "Made it to point 3";

in various places. This way you can see what is happening where.


The simple calling of a function will not slow PHP down. If you're writing good structured code, your scripts should contain lots of user-defined functions. There must be something happening inside the function which is causing the slowdown.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Actually I take that back. It is possible for the invocation of a function to slow down a script. But it can't be a simple function invocation.

For example, suppose we have two user-defined functions:

function complex_function ($someinput)
{
//do a lot of really cool stuff here
return $someoutput;
}

function simple_function ($someinput)
{
//do one simple thing in here
return $someoutput;
}

And let's say that complex_function() takes 30 seconds to run and simple_function() takes 1 second to run.

Suppose you invoke simple_function() as:

$foo = simple_function(complex_function($somevariable));


It will then be 30 seconds between the time the line above is run and the time the first line of simple_function() runs. This is because PHP must first run complex_function() in order to get a return from it, then pass that returned value to simple_function().

This, however, is not a simple function invocation.


How are you invoking the pparse() function?


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
It's invoked with a simple

$template->pparse('body');

Is it possible that I have php installed incorrectly? (Though I would guess that would cause it to simply fail, as opposed to just be very slow.)

Another oddity is that when I try this from different computers (this is part of a webpage), I have a slow down always at this point, but it's a much different amount of time from the different computers (although the web page is run on the same server). From the office, with a cable connection, it spends about a second between calling the function and executing the first line. From home, on dial-up, it takes around 4 seconds average according to the microtime calls.

Thanks much for your comments, by the way.
 
I know of nothing in the installation or configuration of PHP that could cause this.

In any regard, your code exhibits the same behavior, in differing extremes, on different installations. This indicates to me that you have problems in your script.


But what we're talking about is not a "bare" function call. This is an object method invocation. It is not outside the realm of possibility that class inheritance or something else could be causing the problem.

When you are timing this method invocation, what are you counting as the first line of the method?


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
I call microtime as the first line inside the method:

function pparse($handle){
//time stamp
global $pt1a;
$outcommon = microtime();
$outcommon = explode(" ",$outcommon);
$outcommon = $outcommon[1] + $outcommon[0];
$pt1a = $outcommon;
.
.
.
Rest of function
.
.
.
}

If I put several more microtime() calls throughout pparse, I find that everything else in pparse runs quickly.
 
pparse doesn't get mentioned anywhere else in the class. In general, what might happen when we have a method invocation in a class? What other code could possibly be getting executed before my timestamp? I could include all the code for the class, but it's awfully long.
 
Without seeing your class code, it is impossible to say.

Again, I recommend that you footprint your class methods. A spurious output somewhere can tell you whether something you don't expect is being run.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
I put print statements in every single function in the method (30-40 of them). None gets executed between calling pparse and the print statement in pparse (which is after the timestamp). Not sure where else to look now. . .
 
There is, then, no good reason I can see for the delay.

What version of PHP are you running on what platform? Are you running this script using a standalone or CGI, or as a web server plugin?


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
php4.3.8 with apache 1.3.<something> (though I wonder if I have it configured right). On Debian Linux.
 
There's not much web-server configuration to do with Apache, and only then if you're running PHP as a module. Generally, PHP either works or it does not.

The only possible thing I can think of is virtual memory settings on your server.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
How can I check the virtual memory settings on the server?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top