Jump to content

how does php interpreter work? (Trying to optimize code)


Goat

Recommended Posts

let's say I have this  code:

<?php
mysql_connect(yadda yadda yadda....);
mysql_select_db(yadda yadda...);
$arr = mysql_fetch_assoc(mysql_query('SELECT yadda yadda'));

if($arr['timestamp']<=$_REQUEST['timestamp'])
{die();}

///////
/// if table timestamp is more recent than _REQUEST timestamp, execute this huge lump of code:
/// HUGE LUMP OF CODE STARTS HERE

include('something big');
include('something else big');

/// yadda yadda......
/// yadda yadda......
/// yadda yadda.....
?>

 

Bottom line, I have very short code that extracts some timestamp from some database and checks if there is something new. If there is nothing new, it dies. If there is, it executes massive code. Now the question is this:

 

Does the interpeter first interpret whole code and then execute it (slow) or does it interpret and execute code line by line,thus skipping whole huge code when he doesn't need it?

 

regards,

 

Goat

Link to comment
Share on other sites

The interpreter first tries to compile entire script (that's why you get syntax error messages even in parts of code that are never run).

 

okay thanks. But can you tell me how to circumvent that? Put part of the code into another file and then include it inside else statement?

Link to comment
Share on other sites

Thanks Mchl and AlexWD for extensive info. I haven't quite figured out what is smonkcaptain saying but thank to him for replaying.

 

Idea: Maybe I put include() inside eval()? I know eval is usually very slow, but I have heard that if you call function from it, it's faster. So something like:

 

eval("include('big_file.php');");

 

Eval itself is slow, but I think big_file.php will be interpreted and executed separately... right? I am developing AJAX chat in pure php so I am obsessed about speed right now.

 

Thoughts?

Link to comment
Share on other sites

That's not really a good idea either. You're really not going to see much of a speed difference anyway, nothing noticeable. As Mchl said, there are other ways that you should be seeking to optimize your code.

Link to comment
Share on other sites

Well I tried some things and code with include('something.php'); and eval("include(something.php');"); has same speed. I know eval is seen as bad by many, but I don't see why I should not take advantage of it.

Link to comment
Share on other sites

The thing with eval is this.

PHP interpreter starts compiling your script, line by line. It gets to middle of it and encouters eval(). What it does? It needs to dump all code that was compiled and start a new taking into account the code to be evaluated in eval. So in fact you're slowing your script down.

Link to comment
Share on other sites

Can't see how's that different from

include('big_file.php');

 

Interpreter can't figure out ahead of time (unless it has AI in it or something) that you want to include big_file.php, therefore it can't interpret that file until it gets to it. Therefore, script from OP will die without interpreting that file unless that file is really needed. Which is exactly what I wanted to do in the first place.

Link to comment
Share on other sites

You still do not quite understand how it works.

 

look at this code

 

<?php
mysql_connect(yadda yadda yadda);
mysql_select_db(yadda yadda);
$arr = mysql_fetch_assoc(mysql_query('SELECT yadda yadda'));

if($arr['timestamp']<=$_REQUEST['timestamp'])
{
  die(); // big_file.php never gets included and is thus never interpreted, speeding up script a little
}
else
{
  $a='big_file.php'; // cheats interpeter so it doesn't know right away we want big_file.php
  include($a); // now that we actually need big_file.php , it gets included, interpreted and executed
}

?>

 

Sorry if I misunderstood you (and you are rather knowledgeable guy so I wouldn't want to), but you seem to be saying that interpreter will find out ahead of time (before getting to else part) that I am including big_file.php and convert it to byte-code (thus needlessly slowing down execution when that slice of code is unnecessary).

Link to comment
Share on other sites

A quick experiment shows that an include() statement is evaluated at runtime and the content in the included file is parsed/tokenized/interpreted at that point in time.

 

Create a main file (a.php) that outputs something and includes a file (b.php) that intentionally contains a fatal parse error -

 

a.php -

<?php
echo '1';
include('b.php');
echo '2';
?>

 

b.php -

<?php
a parse error;
?>

 

Running this produces the output from the first echo statement, followed by the parse error in the b.php file -

1

Parse error: syntax error, unexpected T_STRING in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\b.php on line 2

 

Edit: Also by putting the include() statement inside of an if(){} statement that is first true (produces the parse error), then false (no parse error and produces the expected 12 output from the echo statements in the main file) confirms if and when the code in an include statement is parsed.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.