0

I am currently working on a field level ACL function that will be under really heavy use.

Each data field has to pass through this function before being rendered.

Performance is thus a key issue for this function

  • Does it matter weather I use 1 vs true or 0 vs false?

    ( I know these are not 100% equivalent, I just want to know the performance implications )

  • Would I gain any performance by changing variable name verbosity?

  • Does shorthands like ?:; instead of if else have any impact?

    if($x){ $x; } else { $y; }  VS  $x?$x:$y;
    

Normally I try to keep my code as readable as possible but this is an abnormal case.

I want to squeeze every possible millisecond of performance out of this function.

In addition, any links to pages that compare performance of similar PHP functions would be greatly appreciated.

Dieter Gribnitz
  • 5,062
  • 2
  • 41
  • 38
  • 2
    If you're really trying to squeeze out the very last bit of performance: [writing a php extension in C isn't that hard](http://php.net/manual/en/internals2.structure.php) and may offer serious performance benefits. – fvu Jul 24 '13 at 14:08
  • I'm curious what the background to this question is... It's more interesting then the actual question, because 99,9% of all cases I believe it doesn't matter. – bestprogrammerintheworld Jul 24 '13 at 14:43
  • Some background: I have been working on a framework 24/7 for the last two years that auto-generate code. It takes the concept of convention over configuration to another level. Basically my app has the ability to learn now. It generates code for the admin and frontend web MVC and also for cross platform phone and tablet apps that interface via REST. A core system feature is 100% access control where the admin can set access on the page, row, column and field level. This applies to users and user groups. All access to data has to be passed through the core acl function before being returned. – Dieter Gribnitz Jul 25 '13 at 06:35

2 Answers2

2

I doubt that 1 vs true and 0 vs false has any appreciable performance impact, but if you want to know for sure you should create a benchmark.

Variable name length should not make any difference. The compiler turns them all into internal pointers when it parses the script.

Shorthand notations also shouldn't make much difference, the compiler should generate similar code for both.

Your concerns all seem to be based on the assumption that the code is being interpreted line by line. PHP is compiled, not interpreted.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Without accelerator (but especially in performance-critical situations not using an accelerator would be criminally dumb) a script's runtime = parse time + execution time. Long vars and general code wordiness will affect parse time but not execution time. – fvu Jul 24 '13 at 14:14
  • @fvu But parsing is a one-time action at the beginning of script execution. If performance is an issue, it is presumably in code that's run in large loops. The initial parse time is negligible compared to the execution time of the loops. – Barmar Jul 24 '13 at 14:18
  • Should probably have started by saying that I am already getting phenomenal speed and performance off of the function. This is actually more a point of curiosity than anything else. Since true and false are 4 character strings and 1 and 0 are 1 character bool values I thought it might have some minor impact on the way ram is allocated. Given that the function is being executed a couple of 1000 times. Will run a couple of benchmarks over the weekend when I have more time and post the results. – Dieter Gribnitz Jul 25 '13 at 05:51
  • `true` isn't a character string, `"true"` is. After the program is parsed, `true` and `false` are represented internally as 1 and 0. – Barmar Jul 25 '13 at 14:03
0

None of these matters affect performance at all.

You are looking dramatically wrong way for the improving performance.

Community
  • 1
  • 1
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • Thanks, I actually have astoundingly great performance already. I am just striving for 100% perfection on this one function. Could not find the subject being discussed anywhere else. – Dieter Gribnitz Jul 25 '13 at 06:00