gem
Status: Interested
Joined: 05 Jan 2010
Posts: 10
Back to top
Posted: Jan 5, 10, 12:35    browser_detection_php_ar.txt and OS detection
I profiled the detection script and by far the most time is spent in
the OS detection which I do not need. A flag to not do that part of the
detection woul,d be nice.
techAdmin
Status: Site Admin
Joined: 26 Sep 2003
Posts: 2541
Location: East Coast, West Coast? I know it's one of them.
Back to top
Posted: Jan 5, 10, 12:45    
ah, something like a full-no-os flag. I've never actually tested this code for bottlenecks or performance, so I'll take your word on this.
gem
Status: Interested
Joined: 05 Jan 2010
Posts: 10
Back to top
Posted: Jan 5, 10, 13:12    
Xdebug makes profiling effortless.

This is my test program:
<?php

require_once 'browser_detection_php_ar.inc';
$robot = browser_detection( 'type' );
echo <<<EOT
<html>
<body>robot="{$robot}"
</body>
</html>
EOT;

?>

This is the profile:

:: Code ::
Function  Invocation Count     Tot Self Cost     Total Inc Cost
get_os_data       1    33.37    35.27
browser_detection   1    22.08    93.21
check_is_mobile   1    20.67    22.63
get_item_version   3    7.28    7.65
{main}                   1    6.46    99.98
php::stristr      153    4.49    4.49
php::preg_match   1    2.50    2.50
php::strtolower   1    0.33    0.33
php::substr      10    0.13    0.13
php::count      6    0.13    0.13
require_once::browser_detection_php_ar.inc   1    0.10    0.10
php::is_array      44    0.10    0.10
php::is_numeric   3    0.10    0.10
php::strcspn      3    0.05    0.05
php::strpos      6    0.05    0.05
php::strlen      3    0.05    0.05


Sorry the forum eats the nice columns.

As you can see get_os_data is 1/3 the total cost of the function!
techAdmin
Status: Site Admin
Joined: 26 Sep 2003
Posts: 2541
Location: East Coast, West Coast? I know it's one of them.
Back to top
Posted: Jan 5, 10, 14:13    
very nice, thanks.

Version 5.1.0 now features an optional switch parameter, second parameter: browser_detection( 'full', '1' )....

The second parameter currently has 3 values possible: 1 to turn off os tests, 2 to turn off mobile tests, and 3 to turn both off.

Easy to add more, but there isn't much else you can turn off and have it work.

Note that if you have a page then want something that was turned off previously, it won't work since it was already turned off and the output stored in static variables. Obviously that would not affect most users, but just saying it here to be clear.

to preserve whitespace use [ code] ... [ /code] (minus the extra space after [)
gem
Status: Interested
Joined: 05 Jan 2010
Posts: 10
Back to top
Posted: Jan 5, 10, 14:43    
Excellent. Thanks for that 2nd parm.

Now instead of 6mS the function just
takes 2mS when I specify the 2nd parm to be 3.

:: Code ::

Function          Count     Total Self Cost     Total Inclusive Cost
browser_detection 1    54.11    85.73
get_item_version   3    20.05    20.89
{main}                   1    13.34    100.00
php::preg_match   1    6.58    6.58
php::stristr             19    2.08    2.08
php::strtolower      1    1.10    1.10
php::is_numeric      3    0.35    0.35
php::count              2    0.31    0.31
require_once::/home/sgteam/inc/browser_detection_php_ar.inc
                               1    0.31    0.31
php::substr            10    0.18    0.18
php::strcspn           3    0.13    0.13
php::strpos            6    0.09    0.09
php::strlen             3    0.09    0.09
techAdmin
Status: Site Admin
Joined: 26 Sep 2003
Posts: 2541
Location: East Coast, West Coast? I know it's one of them.
Back to top
Posted: Jan 5, 10, 14:54    
I do forget that ideally, now and then, this little script is actually used by real sites that have to think about server loads etc, so thanks for the reminder on script execution loads etc.
gem
Status: Interested
Joined: 05 Jan 2010
Posts: 10
Back to top
Posted: Jan 5, 10, 15:00    
And thanks for the quick updates.

I use the script to replace the PHP built-in get_browser() which is
slow as molasses in January. We were shocked to find it was consuming 1/3 of the CPU on pretty heavy pages.

Much easier to fix that than to buy more hardware as the site grows.
techAdmin
Status: Site Admin
Joined: 26 Sep 2003
Posts: 2541
Location: East Coast, West Coast? I know it's one of them.
Back to top
Posted: Jan 5, 10, 15:07    
Yes, get_browser was very poorly done, I agree, though I'd never speed tested it. Sometimes it's best to not try to be perfect, just good enough for most uses.

I'd also read that preg_match costs some overhead too, which I see it does, in theory you can code around that one as well, but I was lazy there, and it's not that bad re overhead.
gem
Status: Interested
Joined: 05 Jan 2010
Posts: 10
Back to top
Posted: Jan 5, 10, 15:23    
Agreed, need to get it right first, then see if you need to find speed later.

Even then I only care about speed when the page is too slow or I'm about to need another hardware upgrade. In anticipation of those events I try to keep an eye out for easy ways to write faster code from the start if the site might be busy.

2mS is great for me, but for the sake of argument,
if you look at the last profile I would say the best gains would likely
be in get_item_version().

In general when you can use in intrinsic like preg_match(), however slow it is relative to other intrinsics, you are still better than lots of PHP.

No matter the rules of thumb Xdebug profiles always surprise me and the customers like fast pages.
Display posts from previous:   

All times are GMT - 8 Hours