Ticket #752 (closed enhancement: wontfix)

Opened 7 months ago

Last modified 7 months ago

should not use "substr($str, 0, 1)" but "$str[0]"

Reported by: MugeSo Owned by: david
Priority: normal Milestone: 0.11.2
Component: _OTHER_ Version: 0.11.0
Severity: normal Keywords:
Cc: Patch attached: no

Description

In src/validator/AgaviDependencyManager.class.php, src/controller/AgaviController.class.php and src/routing/AgaviRouting.class.php, to retrive the first charactor of a string, substr function is used. However, in this case, []operator is better.

benchmark result:

[senna@vine senna]$ time php -r '$str="aaaaa"; for($i=0; $i < 1000000; $i++){ substr($str, 0, 1) == "a";}'

real    0m3.345s
user    0m3.320s
sys     0m0.020s
[senna@vine senna]$ time php -r '$str="aaaaa"; for($i=0; $i < 1000000; $i++){ $str[0] == "a";}'

real    0m2.012s
user    0m1.990s
sys     0m0.020s

[senna@vine senna]$ time php -r '$str="aaaaa"; for($i=0; $i < 1000000; $i++){ $str[0];}'

real    0m0.853s
user    0m0.850s
sys     0m0.010s
[senna@vine senna]$ time php -r '$str="aaaaa"; for($i=0; $i < 1000000; $i++){ substr($str, 0, 1);}'

real    0m2.585s
user    0m2.570s
sys     0m0.010s

Attachments

Change History

Changed 7 months ago by david

  • status changed from new to assigned
  • milestone changed from 0.11.1 to 0.11.2

good catch. thanks!

Changed 7 months ago by MugeSo

strpos($action, '/')===0 In FPF should change to $str[0]==='/' too.

Changed 7 months ago by david

  • status changed from assigned to closed
  • resolution set to wontfix

I thought about this... the problem is that we'd get a notice if the string is empty. So we have to add a strlen() check, too. Which means we again have a method call, which probably eliminates the performance advantage, and most importantly: makes the code less readable. Not worth it in the end, I think. I'm closing this as wontfix for now; feel free to re-open the ticket if you believe I'm wrong, or add comments, or discuss it on IRC/dev mailing list :)

Changed 7 months ago by MugeSo

So we have to add a strlen() check, too.

i use isset() and benchmark again :)

[senna@vine senna]$ time php -r '$str="abaed"; $token="a"; for($i=0; $i < 1000000; $i++){ isset($str[0]) && $str[0] === $token;}'

real    0m1.959s
user    0m1.940s
sys     0m0.020s
[senna@vine senna]$ time php -r '$str=5; $token="a"; for($i=0; $i < 1000000; $i++){ isset($str[0]) && $str[0] === $token;}'

real    0m0.969s
user    0m0.950s
sys     0m0.020s

but, this code accepts $str=array($token).

Add/Change #752 (should not use "substr($str, 0, 1)" but "$str[0]")

Author



Action
as closed
Next status will be 'reopened'
 
Note: See TracTickets for help on using tickets.