Why Are PHP Function Calls *So* Expensive

Why are PHP function calls *so* expensive?

Function calls are expensive in PHP because there's lot of stuff being done.

Note that isset is not a function (it has a special opcode for it), so it's faster.

For a simple program like this:

<?php
func("arg1", "arg2");

There are six (four + one for each argument) opcodes:


1 INIT_FCALL_BY_NAME 'func', 'func'
2 EXT_FCALL_BEGIN
3 SEND_VAL 'arg1'
4 SEND_VAL 'arg2'
5 DO_FCALL_BY_NAME 2
6 EXT_FCALL_END

You can check the implementations of the opcodes in zend_vm_def.h. Prepend ZEND_ to the names, e.g. for ZEND_INIT_FCALL_BY_NAME and search.

ZEND_DO_FCALL_BY_NAME is particularly complicated. Then there's the the implementation of the function itself, which must unwind the stack, check the types, convert the zvals and possibly separate them and to the actual work...

How Significant Is PHP Function Call Overhead?

The canonical PHP implementation is very slow because it's easy to implement and the applications PHP aims at do not require raw performance like fast function calls.

You might want to consider other PHP implementations.

If you are writing the applications you should be writing in PHP (dump data from DB to the browser over a network) then the function call overhead is not significant. Certainly don't go out of your way to duplicate code because you were afraid using a function would be too much overhead.

Most expensive operations in PHP?

  • serialize() is slow, as is eval(), create_function(), and spawning additional processes via system() and related functions.
  • beware of anything APC can't cache -- conditional includes, eval()ed code, etc.
  • Opening database connections. Always cache your connections and re-use them.
  • Object cloning
  • Regular expressions. Always use the normal string operations over a regular expression operation if you don't need the functionality of a regexp, e.g. use str_replace() over preg_replace() where possible.
  • Logging and disk writes can be slow - eliminate unnecessary logging and file operations

Some micro-optimizations that are good practice, but won't make much difference to your bottom line performance:

  • Using echo is faster than print
  • Concatenating variables is faster than using them inline in a double-quoted string.
  • Using echo with a list of arguments is faster than concatenating the arguments. Example: echo 'How are you ',$name,' I am fine ',$var1 is faster than echo 'How are you '.$name.' I am fine '.$var1
  • Develop with Notices and Warnings turned on. Making sure they don't get triggered saves PHP from having to run error control on them.

how much effect does number of function calls have on performance?

It's premature optimization. You should have functions that make sense based on your domain and the logical separation of tasks in your code. The only time I would ever consider replacing a function with inline code is if it was in a tight loop and a profiler showed it was a problem.

To more directly answer your question, the effect on performance hinges on quite a few things:

  1. How often is the function called?
  2. Is it in a critical section of the code?
  3. How large is/are the function(s)?

Deciding on whether something should be in a function should be driven by logical separation, clarity, maintainability, etc. and not performance.

Is file_exist() in PHP a very expensive operation?

As well as what the other posters have said, the result of file_exists() is automatically cached by PHP to improve performance.

However, if you're already reading user info from the database, you may as well store the information in there. If the user is only allowed one avatar, you could just store a single bit in a column for "has avatar" (1/0), and then have the filename the same as the user id, and use something like SELECT CONCAT(IF(has_avatar, id, 'default'), '.png') AS avatar FROM users

You could also consider storing the actual image in the database as a BLOB. Put it in its own table rather than attaching it as a column to the user table. This has the benefit that it makes your forum very easy to back up - you just export the database.

PHP - Slow performance on function return assignment

It probably have something to do with the fact that you're creating 10.000 arrays; each time incrementing the number of elements of the new array by 1 element.

My guess while you're inside the loop the local variable isn't freed on its own; Therefore I went ahead & tried freeing it using unset which got the results very close.

I know this is not a real world example; but in your code if you have something similar you could get away with it by just freeing (unsetting) the local variable once you're finished with it

here's your test code again:

class one {

private $var;

public function __construct() {
$this->var = array();
}

public function saveIteration($a) {
$this->var[] = $a;
return $this->var;
}

public function getVar() {
return $this->var;
}
}

$one = new one();
$time_start = microtime(true);

for ($i = 0; $i < 10000; $i++) {
$res = $one->saveIteration($i);
unset($res);
}
echo "First took " . number_format(microtime(true) - $time_start, 3) . " sec.".PHP_EOL;

$time_start = microtime(true);

for ($i = 0; $i < 10000; $i++) {
$one->saveIteration($i);
}

$res = $one->getVar();
echo "Second took " . number_format(microtime(true) - $time_start, 3) . " sec.".PHP_EOL;

Note: the only thing I've modify is adding unset in the first example

Result:

  • First took 0.068 sec.
  • Second took 0.062 sec.


Related Topics



Leave a reply



Submit