Go Back   SitePoint Forums > Forum Index > Program Your Site > PHP
Newsletter FAQ Members List Calendar Mark Forums Read

New to SitePoint Forums? Register here for free!

SitePoint Sponsor
 
Reply
 
Thread Tools Display Modes
Old Sep 27, 2002, 07:16   #1
HarryF
SitePoint Wizard
gold trophysilver trophy
 
Join Date: Nov 2000
Location: Switzerland
Posts: 2,898
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)?
HarryF is offline   Reply With Quote
Old Sep 27, 2002, 07:29   #2
dragonhawk
SitePoint Guru
 
dragonhawk's Avatar
 
Join Date: Apr 2002
Location: Melbourne
Posts: 711
Pick me!! Pick me!!!

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

I always use $i++ without thinking anything of it...
dragonhawk is offline   Reply With Quote
Old Sep 27, 2002, 08:16   #3
Jeremy W.
32,817
silver trophy
 
Jeremy W.'s Avatar
 
Join Date: Jun 2001
Location: Toronto, Canada
Posts: 9,292
++i is always faster in every programming language
__________________
Digital Hitman, netmobs
Personal blog: Ensight
Twitter: @jeremywright
Jeremy W. is offline   Reply With Quote
Old Sep 27, 2002, 09:02   #4
pippo
FreeBSD The Power to Serve
silver trophy
 
pippo's Avatar
 
Join Date: Jul 2001
Location: Italy
Posts: 4,568
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
pippo is offline   Reply With Quote
Old Oct 1, 2002, 18:38   #5
Agent Dwarf
Forum Mathematics Geek
 
Agent Dwarf's Avatar
 
Join Date: Aug 2002
Location: Commonwealth of Pennsylvania
Posts: 232
Did you know that the ++ operator works on numbers AND strings? a will turn to b, z will turn to aa. Pretty useful.
Agent Dwarf is offline   Reply With Quote
Old Oct 1, 2002, 19:10   #6
Hierophant
Your Lord and Master, Foamy
gold trophy
 
Hierophant's Avatar
 
Join Date: Aug 1999
Location: Lancaster, Ca. USA
Posts: 12,363
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
------------

Hierophant is offline   Reply With Quote
Old Oct 1, 2002, 19:38   #7
Agent Dwarf
Forum Mathematics Geek
 
Agent Dwarf's Avatar
 
Join Date: Aug 2002
Location: Commonwealth of Pennsylvania
Posts: 232
I'll make an educated guess and say 10.

Edit:
Agent Dwarf is offline   Reply With Quote
Old Oct 2, 2002, 05:52   #8
siteguru
SitePoint Wizard
 
siteguru's Avatar
 
Join Date: Oct 2002
Location: Scotland
Posts: 3,107
My guess was wrong too

Back to elementary math for me I guess.
__________________
Ian Anderson
www.siteguru.co.uk
siteguru is offline   Reply With Quote
Old Oct 2, 2002, 05:54   #9
HarryF
SitePoint Wizard
gold trophysilver trophy
 
Join Date: Nov 2000
Location: Switzerland
Posts: 2,898
It's a tricky one. And apparently if you try it with different programming languages, you'll get different answers!
HarryF is offline   Reply With Quote
Old Oct 2, 2002, 06:01   #10
pippo
FreeBSD The Power to Serve
silver trophy
 
pippo's Avatar
 
Join Date: Jul 2001
Location: Italy
Posts: 4,568
>>> 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++;
__________________
Mr Andrea
Former Hosting Team Advisor
Former Advisor of '03

Last edited by pippo; Oct 2, 2002 at 07:06..
pippo is offline   Reply With Quote
Old Oct 2, 2002, 07:09   #11
Mincer
SitePoint Wizard
 
Mincer's Avatar
 
Join Date: Mar 2001
Location: London | UK
Posts: 1,140
Quote:
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 = 1 ;

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

Code:
1 - 2
Whereas this:

PHP Code:

$i = 1 ;

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.
Mincer is offline   Reply With Quote
Old Oct 10, 2002, 09:35   #12
samsm
SitePoint Wizard
 
samsm's Avatar
 
Join Date: Nov 2001
Location: Atlanta, GA, USA
Posts: 5,024
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?
samsm is offline   Reply With Quote
Old Oct 18, 2002, 09:16   #13
HarryF
SitePoint Wizard
gold trophysilver trophy
 
Join Date: Nov 2000
Location: Switzerland
Posts: 2,898
Quote:
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.i += ++this.i + 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.
HarryF is offline   Reply With Quote
Old Oct 31, 2002, 04:52   #14
pootergeist
SitePoint Evangelist
 
Join Date: Nov 2001
Location: UK
Posts: 466
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
__________________
teckis - that's news to me.

Last edited by pootergeist; Oct 31, 2002 at 04:54..
pootergeist is offline   Reply With Quote
Old Oct 31, 2002, 08:19   #15
randem
morphine for a wooden leg
 
randem's Avatar
 
Join Date: Jun 2002
Location: .chicago.il.us
Posts: 957
Quote:
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!
randem is offline   Reply With Quote
Old Oct 31, 2002, 08:57   #16
Mincer
SitePoint Wizard
 
Mincer's Avatar
 
Join Date: Mar 2001
Location: London | UK
Posts: 1,140
Quote:
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*

Mincer is offline   Reply With Quote
Old Oct 31, 2002, 10:02   #17
randem
morphine for a wooden leg
 
randem's Avatar
 
Join Date: Jun 2002
Location: .chicago.il.us
Posts: 957
Quote:
Originally posted by Mincer
*cough* see my previous post *cough*
Oops. Hahaha.
__________________
----Adopt-a-Sig----
Your message here!
randem is offline   Reply With Quote
Old Nov 7, 2002, 08:02   #18
Michel V
will code HTML for food
 
Michel V's Avatar
 
Join Date: Sep 2000
Location: Corsica
Posts: 552
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]
Michel V is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread | Next Thread »

Thread Tools
Display Modes

 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

 
Forum Jump


All times are GMT -7. The time now is 21:09.


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright 1998-2009, SitePoint Pty Ltd. All Rights Reserved