SitePoint Sponsor

User Tag List

Results 1 to 18 of 18
  1. #1
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Trivia: incrementing $i

    Here's a little piece of PHP trivia;

    In PHP there are basically three main ways to increment a variable by one. Using addition your options are;

    PHP Code:
    $i=$i+1
    PHP Code:
    $i++; 
    PHP Code:
    ++$i
    Now running a little performance check;

    PHP Code:
    <?php
    function getmicrotime(){ 
        list(
    $usec$sec) = explode(" ",microtime()); 
        return ((float)
    $usec + (float)$sec); 
    }


    $start getmicrotime();
    $i=0;
    while (
    $i 1000000) {
        
    $i=$i+1;
    }
    $end getmicrotime() - $start;
    echo ( 
    "<p>\$i=\$i+1 Took: ".$end );

    $start getmicrotime();
    $i=0;
    while (
    $i 1000000) {
        
    $i++;
    }
    $end getmicrotime() - $start;
    echo ( 
    "<p>\$i++ Took: ".$end );

    $start getmicrotime();
    $i=0;
    while (
    $i 1000000) {
        ++
    $i;
    }
    $end getmicrotime() - $start;
    echo ( 
    "<p>++\$i Took: ".$end );

    ?>
    The results are;

    Code:
    $i=$i+1 Took: 4.1503800153732
    
    $i++ Took: 2.8252899646759
    
    ++$i Took: 2.7342989444733
    ++$i is consistently slightly faster than $i++. In most cases (not all), ++$i can be used interchangeably with $i++.

    Head here for more detail: http://www.php.net/manual/en/languag....increment.php

    OK - trivia over.

    Finally a brain teazer (don't give the game away by answering here);

    PHP Code:
    $i=1;
    $i += ++$i $i++;
    echo (
    $i); 
    What will PHP echo at the end (without running the code first)?

  2. #2
    SitePoint Guru dragonhawk's Avatar
    Join Date
    Apr 2002
    Location
    Melbourne
    Posts
    707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Pick me!! Pick me!!!

    Very interesting about the $i++ and the ++$i

    I always use $i++ without thinking anything of it...

  3. #3
    SitePoint Wizard silver trophy Jeremy W.'s Avatar
    Join Date
    Jun 2001
    Location
    Toronto, Canada
    Posts
    9,123
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    ++i is always faster in every programming language
    SVP Marketing, SoCast SRM
    Personal blog: Strategerize
    Twitter: @jeremywright

  4. #4
    FreeBSD The Power to Serve silver trophy pippo's Avatar
    Join Date
    Jul 2001
    Location
    Italy
    Posts
    4,514
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Harry,
    you forget this case

    PHP Code:
    $i += 1
    using your script I have

    Code:
    $i=$i+1 Took: 4.01947200298
    
    $i++ Took: 2.70495903492
    
    ++$i Took: 2.622205019
    
    $i += 1 Took: 2.75653505325
    It could be interesting to do an average of the times,
    scoates did something similar if I'm right


    pippo
    Mr Andrea
    Former Hosting Team Advisor
    Former Advisor of '03

  5. #5
    Forum Mathematics Geek Agent Dwarf's Avatar
    Join Date
    Aug 2002
    Location
    Commonwealth of Pennsylvania
    Posts
    232
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Did you know that the ++ operator works on numbers AND strings? a will turn to b, z will turn to aa. Pretty useful.

  6. #6
    Your Lord and Master, Foamy gold trophy Hierophant's Avatar
    Join Date
    Aug 1999
    Location
    Lancaster, Ca. USA
    Posts
    12,305
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hmmm... Well no one answered the brain teaser.

    I mostly use $i++, I would think most people do. It actually looks the most logical. However once in a while it is necessary to increment a variable after using it somewhere so ++$i comes in handy as well.
    Wayne Luke
    ------------


  7. #7
    Forum Mathematics Geek Agent Dwarf's Avatar
    Join Date
    Aug 2002
    Location
    Commonwealth of Pennsylvania
    Posts
    232
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'll make an educated guess and say 10.

    Edit:

  8. #8
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,631
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    My guess was wrong too

    Back to elementary math for me I guess.
    Ian Anderson
    www.siteguru.co.uk

  9. #9
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's a tricky one. And apparently if you try it with different programming languages, you'll get different answers!

  10. #10
    FreeBSD The Power to Serve silver trophy pippo's Avatar
    Join Date
    Jul 2001
    Location
    Italy
    Posts
    4,514
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    >>> What will PHP echo at the end
    6

    *edited*
    I launched it...but I have to admit that I was wrong...

    *edited 2*
    In C the result is the same as in php:
    Code:
    i = 1;
    i += ++i + i++;
    Last edited by pippo; Oct 2, 2002 at 06:06.
    Mr Andrea
    Former Hosting Team Advisor
    Former Advisor of '03

  11. #11
    SitePoint Wizard Mincer's Avatar
    Join Date
    Mar 2001
    Location
    London | UK
    Posts
    1,140
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by HarryF
    It's a tricky one. And apparently if you try it with different programming languages, you'll get different answers!
    It's because some languages have different operator precedence.

    In case people don't understand the difference between $i++ and ++$i:

    ++$i is the pre-increment operator, and $i++ is the post-increment operator. To illustrate the difference, I'll use a simple example:

    PHP Code:
    $i ;
    print 
    $i++ . ' - ' $i 
    This will print:

    Code:
    1 - 2
    Whereas this:

    PHP Code:
    $i ;
    print ++
    $i ' - ' $i 
    Will print:

    Code:
    2 - 2
    As you can see, the pre-increment operator increments the variable before passing to the current pending function (in this case 'print'), the post-increment operator passes the variable, and then increments.

    I hope that's reasonably clear.

    Matt.

  12. #12
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I found it very interesting that:
    PHP Code:
    $i += 1
    Was faster than
    PHP Code:
    $i $i +1
    This suggests to me that:
    PHP Code:
    $i .= $k
    might be faster than
    PHP Code:
    $i $i $k
    and so on.

    Does this basic theory hold up across most operations? (The fewer times you reference a variable, the faster it'll be.)
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  13. #13
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does this basic theory hold up across most operations? (The fewer times you reference a variable, the faster it'll be.)
    Pretty much. Think the more instances of a variable PHP has to hold in memory, the slower it gets.

    I could be wrong in saying this but perhaps it's worth thinking about the = operator as meaning "creates new instance of". So $i = $i + 1; creates another instance of $i.

    As a matter of intest, that teazer in Java;

    PHP Code:
    // MyI.java
    import java.util.*;

    public class 
    MyI {
        
        private 
    int i;

        
    void display () {
                
    this.i=1;
                
    this.+= ++this.this.i++;
                
    System.out.println("MyI: "+this.i);
        }

        public static 
    void main String[] args ) {
            
    MyI m = new MyI();
            
    m.display();
        }

    Java gives 5 as the answer.

  14. #14
    SitePoint Evangelist
    Join Date
    Nov 2001
    Location
    UK
    Posts
    466
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To go even faster (programming theory ergo untested) you could evaluate toward zero rather than toward 1000000.
    Code:
    $i = 1000000;
    while($i>0) {
    --$i;
    }
    No math is really involved to determine how far $i is from zero as it is always $i so it should beat determining if $i is less than a million. It certainly would be useful when used in a syntax where the million was variable
    Code:
    $i = $variable;
    while(--$i>0) {
    //stuff
    }
    as $variable doesn't need to be deduced each time.

    Subnote: I guessed 6 too
    Last edited by pootergeist; Oct 31, 2002 at 05:54.
    teckis - that's news to me.

  15. #15
    morphine for a wooden leg randem's Avatar
    Join Date
    Jun 2002
    Location
    .chicago.il.us
    Posts
    957
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by pootergeist
    Subnote: I guessed 6 too
    Since I haven't seen anyone point it out, I think someone should explain why it's not 6.

    ++$i is a preincrement, which means increment the value in $i before evaluating it.

    $i++ is a postincrement, which means increment the value in $i after evaluating it.

    So, for instance
    Code:
    $i = 1;
    print $i++;
    should print 1, and then leave a value of 2 in $i
    ----Adopt-a-Sig----
    Your message here!

  16. #16
    SitePoint Wizard Mincer's Avatar
    Join Date
    Mar 2001
    Location
    London | UK
    Posts
    1,140
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by randem
    Since I haven't seen anyone point it out, I think someone should explain why it's not 6.
    *cough* see my previous post *cough*


  17. #17
    morphine for a wooden leg randem's Avatar
    Join Date
    Jun 2002
    Location
    .chicago.il.us
    Posts
    957
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Mincer
    *cough* see my previous post *cough*
    Oops. Hahaha.
    ----Adopt-a-Sig----
    Your message here!

  18. #18
    will code HTML for food Michel V's Avatar
    Join Date
    Sep 2000
    Location
    Corsica
    Posts
    552
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Now, that's interesting:
    Code:
    $ perl
    
    $i = 1;
    $i += ++$i + $i++;
    print $i;
    
    8
    So PHP says '7', Java says '5', Perl says '8', and I had guessed '6'. Phew!
    [blogger: zengun] [blogware contributor: wordpress]


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •