Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1] 2

Author Topic: some possible optimztions ?  (Read 9582 times)

mosh

  • Hero Member
  • *****
  • Karma: 77
  • Posts: 510
  • Awesome day :)
    • cms-zen
some possible optimztions ?
« on: May 04, 2008, 05:17:51 pm »

hey all  :D

(1)
i was looking at the token function
Code: [Select]
// SESSION TOKEN
<?php
function token() {
$a =  md5(substr(session_id(), 27));
$b $_SERVER['HTTP_USER_AGENT'];
$c _SITE;
$token md5($a.$b.$c);
return $token;
}
?>


if move the first var $a md5 function,
and define it outside the token function,
can gain a lil optimiztion
and less function calls by the script:

Code: [Select]
<?php
$toki 
md5(substr(session_id(), 27));
define('_TOKI',$toki);

// SESSION TOKEN
function token() {
//$a =  md5(substr(session_id(), 2, 7));
$a _TOKI;
$b $_SERVER['HTTP_USER_AGENT'];
$c _SITE;
$token md5($a.$b.$c);
return $token;
}
?>



some xdebug data:

//// md5 is defined outside token function
Function count = 328
Memory usage = 2078056
Memory Peak usage = 2223624
Time index = 0.0615880489349

//// defult token function
Function count = 349
Memory usage = 2079608
Memory Peak usage = 2225176
Time index = 0.0626409053802

 ;D

(2)
i am doing some testing with replace few is_null condtion with empty condtion  :P
i only replaced is_null that are not in admin functions

so far it looks that it is a possible speed boost
and less function calls.

would like to know what the code wizzards think about it

so far i did not get any errors  ;D

Joost

  • Guest
Re: some possible optimztions ?
« Reply #1 on: May 04, 2008, 05:38:03 pm »

Code: [Select]
<?php
       $a 
=  md5(substr(session_id(), 27));
$b $_SERVER['HTTP_USER_AGENT'];
$c _SITE;
$token md5($a.$b.$c);

// SESSION TOKEN
function token() { global $token;
return 
$token;
}
?>


How about this?
« Last Edit: May 04, 2008, 05:41:00 pm by Joost »
Logged

mosh

  • Hero Member
  • *****
  • Karma: 77
  • Posts: 510
  • Awesome day :)
    • cms-zen
Re: some possible optimztions ?
« Reply #2 on: May 04, 2008, 05:54:02 pm »

hey joost  :D
thank you,

here are xdebug:
Function count = 325
Memory usage = 2078088
Memory Peak usage = 2226776
Time index = 0.0625269412994

so, it is a yes in function calls, 3 less, but time and memory are a lil up :)

(2)
how do you think about the is_null v empty ?

as i see in db,  id is not null but also empty,
and as far as a can tell using empty give speed boost as well.

Joost

  • Guest
Re: some possible optimztions ?
« Reply #3 on: May 04, 2008, 06:10:57 pm »

I've been doing some reading and testing on this.
is_null is a little bit slower, but not significant. It is because it checks the 'type' of content.
empty just checks whether it is.... empty.
But be careful with isset and empty, don't mix them.

For instance this code

$bottle = null

isset only checks whether the variable is set. Hence its name.. It does not say anything about the value.
empty will check the value.
Logged

mosh

  • Hero Member
  • *****
  • Karma: 77
  • Posts: 510
  • Awesome day :)
    • cms-zen
Re: some possible optimztions ?
« Reply #4 on: May 04, 2008, 06:27:34 pm »

just to demo the diff ( demo made on 16 )
in function l()
for language and home changed the is_null to empty

Code: [Select]
<?php
function l($variable) {
static $language$home  NULL;
if(empty($language)) { $language s('language'); }
 
if(empty($home)) { $home s('home_sef'); }
static $l = array();
if (isset($l[$variable])) { return $l[$variable];
} else {
?>


results:
is_null:
Function count = 481
Memory usage = 1451576
Memory Peak usage = 1473312
Time index = 0.0608470439911

empty:
Function count = 409
Memory usage = 1450808
Memory Peak usage = 1472608
Time index = 0.0603201389313

hmm  :P

Joost

  • Guest
Re: some possible optimztions ?
« Reply #5 on: May 04, 2008, 06:44:01 pm »

Sorry mosh, but I still don't understand how you can measure performance, while using and different function calls and different times.
Logged

mosh

  • Hero Member
  • *****
  • Karma: 77
  • Posts: 510
  • Awesome day :)
    • cms-zen
Re: some possible optimztions ?
« Reply #6 on: May 04, 2008, 07:10:46 pm »

very simple test i do.

added xdebug functions to count how meny function(any) was called by a script (snews + index)
while it load, and the time it take to my index.php file.

same with memory use.

wincachegrind output show in detail what is going on.

i just show here the summery from the index.

Joost

  • Guest
Re: some possible optimztions ?
« Reply #7 on: May 04, 2008, 07:29:13 pm »

Yes, that I can see. But I could not conclude anything from that. You should have at least one parameter fixed. I use a fixed iteration, for different code.

These are the results for token:

Old token

Script timer: 0.187807 seconds. Iteration: 1000
memory_get_peak_usage(true) 2097152 bytes
memory_get_peak_usage(false) 1645712 bytes
memory_get_usage(false) 1447112 bytes


New token
Script timer: 0.036539 seconds. Iteration: 1000

memory_get_peak_usage(true) 2097152 bytes
memory_get_peak_usage(false) 1643096 bytes
memory_get_usage(false) 1445280 bytes
Logged

mosh

  • Hero Member
  • *****
  • Karma: 77
  • Posts: 510
  • Awesome day :)
    • cms-zen
Re: some possible optimztions ?
« Reply #8 on: May 04, 2008, 09:19:54 pm »

hey joost  :D
nice results, that turn out to nice speed gain hehe  ;D

thank you

now i play with the is_null vs empty  :P

update:
running 1000 Iterations with
Code: [Select]
<?php
if(empty($language)) { $language s('language'); }
if(
is_null($language)) { $language s('language'); }
?>


give timing:
empty timer: 0.000692 seconds.
is_null timer: 0.021059 seconds.

looks like big speed gain :)

« Last Edit: May 04, 2008, 10:46:23 pm by mosh »
Logged

codetwist

  • Hero Member
  • *****
  • Karma: 50
  • Posts: 940
Re: some possible optimztions ?
« Reply #9 on: May 05, 2008, 10:08:10 am »

@ token() optimizers: Before You were looking at benefits related to token() as they are for real conditions - namely, snews page building. If You are using just abstract function grinding - like 1000 iterations then You should be sure of few things:
(1) Benefits gained matter for real task. Matter here usually means that component optimized contributes significantly to outcome (performance). And I don't see this in real numbers. Do You?
(2) Benefits gained are achievable for real task. Like that if You call token() just a couple of times as in real task then gained benefits don't shrink suddenly because initialization time for _TOKEN value is comparable/same as time needed for old token() function.
(3) And unless there are significant benefits in relation to real performance new optimized version shouldn't degrade overall quality of code as such. For example, doesn't add more code to the mess already available at global scope.

If You ignore (1) then You are ending with micro-optimizations at bare coding technique level which usually appears to be of category 'false optimization'. Of course it's fun while learning language and coding techniques but it damages product for sure if strong case for (1) isn't present.

Have fun and take care ;D
« Last Edit: May 05, 2008, 10:17:54 am by codetwist »
Logged

mosh

  • Hero Member
  • *****
  • Karma: 77
  • Posts: 510
  • Awesome day :)
    • cms-zen
Re: some possible optimztions ?
« Reply #10 on: May 05, 2008, 11:25:59 am »

hey codetwist  :D
thank you for those points, are good point.

in my first post you can see a reduction in Number of functions ( in this case any )
while script ( snews ) being executed.

it may be like in Sven case, where Number of mysql calls needed to be cut down.
could be in larg db / traffic websits that Number of functions exe would go very high ?
token case:
output by wincachegrind show,
normal token:
md5 function being called 14 times.

new token (joost):
md5 function was called 2 times only.

as i understand it, the md5 use the sess id for calculation,
so for every user view has sess id, this show only one user difference,
how much gain we get with more users needs to be tested online :)

may be worth the saving hehe

i may got it wrong  hehe   :P


skian

  • Full Member
  • ***
  • Karma: 14
  • Posts: 120
Re: some possible optimztions ?
« Reply #11 on: May 05, 2008, 12:24:07 pm »

Hi guys,

Please, don't try to optimze token() - the problem with this function is not what it does but how it is used. I've proposed in another thread (http://snewscms.com/forum/index.php?topic=7217.0) that a new function "isadmin()" is introduced and I hope it will make its way into 1.7. This new function must test whether $_SESSION[_SITE.'Logged_In'] is set or not before calling token().

As of is_null vs is_empty, there is a major difference between them : is_null does not produce an undefined variable reference. This is why I would suggest to keep is_null wherever possible (and introduce even some more when needed) to help developper create mods. Regarding performance, there are many areas of improvement elsewhere.

Logged

Joost

  • Guest
Re: some possible optimztions ?
« Reply #12 on: May 05, 2008, 12:53:34 pm »

@mosh

Optimization is important, but like Codetwist points out, you can overdo it.
The example code I showed, is indeed faster. It is done in the global scope of the script and the result is pushed inside the several functions needing it.
However, doing that file wide (dismantling all functions), makes maintaining and developing sNews very complex. Each piece of code in the global scope influences another piece of code, while functions can operate isolated. So we have to look for a balance between maintainability and optimization.

Hi guys,

Please, don't try to optimze token() - the problem with this function is not what it does but how it is used. I've proposed in another thread (http://snewscms.com/forum/index.php?topic=7217.0) that a new function "isadmin()" is introduced and I hope it will make its way into 1.7. This new function must test whether $_SESSION[_SITE.'Logged_In'] is set or not before calling token().

As of is_null vs is_empty, there is a major difference between them : is_null does not produce an undefined variable reference. This is why I would suggest to keep is_null wherever possible (and introduce even some more when needed) to help developper create mods. Regarding performance, there are many areas of improvement elsewhere.

Yes, there are many areas of improvement elsewhere. I am afraid they won't make it all in the next release. ;)
Logged

codetwist

  • Hero Member
  • *****
  • Karma: 50
  • Posts: 940
Re: some possible optimztions ?
« Reply #13 on: May 05, 2008, 01:39:23 pm »

@ mosh : Reduction of number of function calls as such doesn't matter without looking into relevant measures like time spent and memory used. If those two doesn't change significantly (according to numbers posted above regarding page builds) then reduction in numbers of function calls might be just that - useless as external reader doesn't care how many times cms engine calls functions to render page. External reader care about total speed (build time) and if page doesn't die off instead of rendering (out of memory). Online test ... if You can't get significant change while building page for single user then why do You expect that to change for several paralel requests? And example with db calls is different one - there You are talking about call of function that will in fact invoke call to another execution engine, mysql, outside of php. There benefits will be worth it even if You just get rid of single additional call because each new sql call is taxy in terms of speed and server resources consumption outside of php :P
« Last Edit: May 05, 2008, 02:01:22 pm by codetwist »
Logged

codetwist

  • Hero Member
  • *****
  • Karma: 50
  • Posts: 940
Re: some possible optimztions ?
« Reply #14 on: May 05, 2008, 04:09:27 pm »

@Joost: What code was used to run 1000 iterations for old/new token()?
Logged
Pages: [1] 2