Thread: Weird Situation

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    44
    Rep Power
    5

    Weird Situation


    I am developing a system using the MVC design pattern.
    for debugging & optimizing the script i am using
    PHP Code:
    memory_get_usage() 
    at the Start and the End of the script.

    i have a file footer.html.php
    if the file looks like this
    PHP Code:
    <?= $bodyContent ?>
    by using extract with
    PHP Code:
    $bodyContent General_View::pageFooter(); 
    the script consume 76kb of memory.

    but if i change footer.html.php to
    PHP Code:
    <?= \Views\General_View::pageFooter() ?>
    the script consume 108kb of memory.

    pageFooter method is:
    PHP Code:
        static public function pageFooter()
        {
            
    $str '<br /><br />'.Converter::memorySizeConverter($b memory_get_usage()).
            
    ' ('.Converter::memorySizeConverter($b-SCRIPT_START_MEMORY).')';
            
    $end microtime(true);
            
    $str .= '<br />Processing time: 'sprintf('%.4f', ($end-SCRIPT_START_TIME)).' seconds';
            return 
    $str;
        } 
  2. #2
  3. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,301
    Rep Power
    7170
    I'm not quite sure I understand the issue. You're using different PHP features so it makes sense that they wouldn't use exactly the same amount of memory.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    44
    Rep Power
    5
    Both 2 methods do the same
    Logically the 2nd method should use less memory
    because it skips the extract part of code
    and do the pageFooter method directly without assigning it into a variable
  6. #4
  7. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,301
    Rep Power
    7170
    OK, I see now. I didn't get that you were using extract with one and not with the other, I thought you were referring to the use of namespaces in one example.

    When PHP copies a variable it does not actually allocate any new memory until it detects a changing write to one of the variables. That is why you can run the following without getting a memory error:
    PHP Code:
    <?php
        
        
    for($i 0$i 100000$i++) {
            
    $longdata .= sha1('abc');
        }
        
        
    $shortdata ' ';
        
        
    $array array_fill(01024$shortdata);
        
        foreach(
    $array as $key => $value) {
            
    $array[$key] = $longdata;
        }
    But cannot run this:
    PHP Code:
    <?php
        
        
    for($i 0$i 100000$i++) {
            
    $longdata .= sha1('abc');
        }
        
        
    $shortdata ' ';
        
        
    $array array_fill(01024$shortdata);
        
        foreach(
    $array as $key => $value) {
            
    $array[$key] = (string)$longdata;
        }
    In the first case, all 1024 elements of the array are pointing at the same physical memory. In the second example, all 1024 elements are pointing at separate pieces of physical memory.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    44
    Rep Power
    5
    sorry but i don't get it,
    how these 2 examples settle the problem i asked about
  10. #6
  11. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,301
    Rep Power
    7170
    extract should not logically use more memory because no new memory needs to be allocated. The variables created by extract are pointing to the same physical memory as the elements in the array.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around

IMN logo majestic logo threadwatch logo seochat tools logo