Fast add in php



  • Here's a post from one of PHP developers' forums. (translated into English):

    Do you know that on the assember level, this code:

    $pages += 2;

    is run as:

    add eax, 2
    (eax is an example, it can be another register)


    and this code:

    $zmienna++;

    $zmienna++;

    is run on the server as:

    inc eax;

    inc eax;

    code from second example will run faster ;) but in the current era, when microseconds don't have any meaning, it doesn't matter at all ;)

     It's scary that some people can be so clueless...



  • Even if it were executed like that, it'd probably be wrong as the 2nd 'inc eax' would stall because it's using the same register. 

    Clueless on many counts.



  • @viraptor said:

     It's scary that some people can be so clueless...

    Clueless !? Think of the possibilities man !!! 

    $total += 10000;

    would be 10000 times faster when you rewrite it ... just jam it !!!



  • @mxsscott said:

    2nd 'inc eax' would stall because it's using the same register. 
     

    It wouldn't stall, but it couldn't be auto-parallelized by the cpu's dispatcher. Stalls occur when you need to wait for data from the cache or main memory. "inc eax" is a purely in-cpu expression. 

    He's wrong about the double inc being faster though, at least on the 386 (which is the last one I've got the Intel programmer's manual available for). add and inc both cost 2 cycles for register operations, so the double inc takes 4 cycles compared to 2 for the add. add's 1 cycle worse (7) than inc (6) when working on memory. But then, add also updates the carry flag, inc doesn't. 

    If I remember right, starting with the 486, all the basic instructions such as add and inc were all reduced to 1 cycle. 



  • Of course responses are great too -> from ridiculous:

    - I see that you don't know much about programming, if you write things like that  ... I'll give you a hint - do you know the difference between ++$i and $i++ .. (+ attached code to measure a time difference between double ++$i, $i++ and $i+=2)

    to normal ones which are of course dissed by other people:

    - Do you know that PHP is an interpreted language?
    - - It doesn't change a fact that computers still run machine code :)



  • On recent CPUs that's a little bit more complicated, but on a 80486 ADD reg, immed is 1 cycle and inc reg32 is 1 cycle as well.

    $pages+=2 would probably be compiled to add [pages], 2 anyway instead of

    mov eax, [pages]
    add eax, 2
    mov [pages], eax

    since they require the same number of cpu cycles (3), but the former preserves the value of eax.

    If the compiler was dumb enough to behave as this genius said, the latter would be compiled to

    mov eax, [pages]
    inc eax
    inc eax
    mov [pages], eax

    which is 4 cpu cycles. Therefore, $pages += 2 is better since it uses less CPU cycles and conserves the value of eax (if we accept the dubious fact the php compiles the script into assembly and is too dumb to optimize properly).

    As already mentioned stalling is not an issue here because registers are (obviously) not cached.  I would like to point out that most assembler syntaxes out there do not require the semicolon after theinstructions.

    I think this guy is some smartass CS graduate who obviously lacks real life development experience but wants to show off his 1337 sk1llz he got from the university.



  • TRWTF is that you people are talking about assembly when PHP is compiled into Zend bytecode...



  • @DrJokepu said:

    ...(if we accept the dubious fact the php compiles the script into assembly and is too dumb to optimize properly).

    Obviously PHP is not compiled into assembly, it is a weak and slow language for people who do not understand programming.  ColdFusion does not compile to assembly, either, but instead runs on Java which not only makes it faster than assembly, but also invulnerable to any kind of security problems.



  • @TehFreek said:

    TRWTF is that you people are talking about assembly when PHP is compiled into Zend bytecode...

     

    I suspected it's doing something like that but since I am far from being a PHP expert I accepted the guy's hypothesys about PHP compiled into machine code to show how it's wrong on many levels.



  •  

     microseconds don't have any meaning

    is also wrong in more than one way. 



  • @MarcB said:

    @mxsscott said:

    2nd 'inc eax' would stall because it's using the same register. 
     

    It wouldn't stall, but it couldn't be auto-parallelized by the cpu's dispatcher. Stalls occur when you need to wait for data from the cache or main memory. "inc eax" is a purely in-cpu expression. 

    [url]http://en.wikipedia.org/wiki/Hazard_(computer_architecture)#Data_hazards [/url]

    Some other instructions would probably be run while waiting for the first increment to finish, so you likely wouldn't get a stall here anyway.  Nevertheless, saying that stalls can only occur when you're waiting on memory is misleading.



  • @TehFreek said:

    TRWTF is that you people are talking about assembly when PHP is compiled into Zend bytecode...

    And as we all know, modern processors have a Zend bytecode interpreter built right in... those Zend bytecodes will never be converted to machine code.



  • @stolen_username said:

    @MarcB said:

    @mxsscott said:

    2nd 'inc eax' would stall because it's using the same register. 
     

    It wouldn't stall, but it couldn't be auto-parallelized by the cpu's dispatcher. Stalls occur when you need to wait for data from the cache or main memory. "inc eax" is a purely in-cpu expression. 

    http://en.wikipedia.org/wiki/Hazard_(computer_architecture)#Data_hazards

    Some other instructions would probably be run while waiting for the first increment to finish, so you likely wouldn't get a stall here anyway.  Nevertheless, saying that stalls can only occur when you're waiting on memory is misleading.

     

    Aargh, I wish I had never said 'stall'. Suffice to say PHP is executed as bytecodes by the Zend engine and I've never heard of it being compiled into CPU instructions. There's also probably more appropriate places for optimization than this single addition, e.g. pick a better algorithm at a higher level!



  • The real WTF is developers focusing on optimizing away a CPU cycle or two when the real performance killer is the 200 queries they're running just to display a blank page

     There is never a point at which you should ever be dealing with this level of optimization in a web application.



  • @morbiuswilters said:

    @DrJokepu said:

    ...(if we accept the dubious fact the php compiles the script into assembly and is too dumb to optimize properly).

    Obviously PHP is not compiled into assembly, it is a weak and slow language for people who do not understand programming.  ColdFusion does not compile to assembly, either, but instead runs on Java which not only makes it faster than assembly, but also invulnerable to any kind of security problems.

     

     

     

    Please, please be sarcasm. 



  • @shark615 said:

    Please, please be sarcasm. 
     

    Excellent, another newcomer with no humor detection.



  •  @MasterPlanSoftware said:

    @shark615 said:

    Please, please be sarcasm. 
     

    Excellent, another newcomer with no humor detection.

     

    Excellent, a forum 'regular' with such a dull life they need to bolster their faltering ego by being an asshole on an anonymous message board. Good job you can now exit your moms basement and tell her what a hero you are.



  • @shark615 said:

    Excellent, a forum 'regular' with such a dull life they need to bolster their faltering ego by being an asshole on an anonymous message board. Good job you can now exit your moms basement and tell her what a hero you are.
     

    I agree. My wording was awfully harsh. Let me come down to your level here:

    morbiuswilters was making this thing called a joke. When you see these you should either laugh or ignore it. If you don't understand it, you could always browse through the forums to get a better idea of some of the ongoing/inside jokes. This browsing without posting is called 'lurking'. It is much better to lurk, than to start cluelessly posting and grumbling about jokes some of the regulars have made. It makes you appear a little dense.



  • @shark615 said:

    Good job you can now exit your moms basement and tell her what a hero you are.
    What a clever and original line! Surely, we should not be questioning the wit of such a groundbreaking humorist!



  • I never thought such a petty, half serious response could get some regulars
    riled up. Yes gentleman I know what sarcasm is, I also know what a joke is. What
    I said was about as serious as your posts are relevant to anything other then
    an obvious need to feel superior due to spending a pathetic amount of time on
    these forums. Would you like me to make some spelling or grammar mistakes so
    you can pick those apart too?<o:p></o:p>

    @bstorer said:

    @shark615 said:
    Good job you can now exit your moms basement and tell her what a hero you are.
    What a clever and original line! Surely, we should not be questioning the wit of such a groundbreaking humorist!
    <o:p></o:p>

    I thought it was pretty witty and very descriptive of your type.<o:p></o:p>

    I am sure MasterPlan could fend for himself in his never-ending quest to deride everyone in his attempt to feel superior. He seems more then qualified on being a prick without any help. Do you feel the need to defend a regular or are you just a lacky; looking for some forum respect? Perhaps you smell some blood in the water and can't wait to jump in and rail on a 'newbie'? <o:p></o:p>



  • @shark615 said:

    I never thought such a petty, half serious response could get some regulars
    riled up.
    Spectate? Is that you?
    @shark615 said:
    Yes gentleman I know what sarcasm is, I also know what a joke is. What
    I said was about as serious as your posts are relevant to anything other then
    an obvious need to feel superior due to spending a pathetic amount of time on
    these forums.
    That's great. My point is this: what you said was neither funny nor creative. The rest of us are putting some brainpower into it. Is it too much to ask for you to divert some of your meager mental power away from sustained breathing and put it into your response?
    @shark615 said:
    Would you like me to make some spelling or grammar mistakes so
    you can pick those apart too?
    i hav no idea wut u iz taklin abowt
    @shark615 said:
    I thought it was pretty witty and very descriptive of your type.
    Dude, you're posting on an IT forum. What the fuck do you expect if not pedantry? Also, again, your post: not witty.
    @shark615 said:
    I am sure MasterPlan could fend for himself in his never-ending quest to
    deride everyone in his attempt to feel superior.
    If you knew MPS at all, you'd know he'll have feelings of superiority regardless of what takes place on these boards.
    @shark615 said:
    He seems more then qualified
    on being a prick without any help.
    It's "than", asshole. Thanks for throwing that in there, by the way.
    @shark615 said:
    Do you feel the need to defend a regular or
    are you just a lacky; looking for some forum respect? Perhaps you smell some
    blood in the water and can't wait to jump in and rail on a 'newbie'?
    No, I just rankle at such a trite -- and incredibly wrong -- description. Further, it's a joke so beaten into the ground that hack sportswriters use it to describe bloggers. Can't you think of something funnier than the "jokes" tossed out by 90-year-old beat writers for the Podunk Gazette? If not, please just leave the humor to us professionals.



  • @shark615 said:

    Perhaps you smell some
    blood in the water and can't wait to jump in and rail on a 'newbie'?
     

    You see, this is your problem of being defensive here. No one was really picking on you. You will know when someone is picking on you. See: SpectateSwamp, TunnelRat, Russ0519, JimBastard.

    But here you are, gone off the deep end.



  • @shark615 said:

    I am sure MasterPlan could fend for himself in his never-ending quest to
    deride everyone in his attempt to feel superior.

    Surely it will end some day... right?  Surely?

     

    @shark615 said:

    Do you feel the need to defend a regular or
    are you just a lacky; looking for some forum respect?

    Uh... the second.  pstorer is mostly a lackey, although he's supposed to be my lackey..

     

    @shark615 said:

    'newbie'

    n00b.  It's pronounced n00b.



  • @bstorer said:

    @shark615 said:
    Good job you can now exit your moms basement and tell her what a hero you are.
    What a clever and original line! Surely, we should not be questioning the wit of such a groundbreaking humorist!

    Wait, you guys actually think the Coldfusion jokes are funny? I thought it was one of your "meta jokes" that only gets posted so you can post "Get a sense of humor, n00b!" later on.

    EDIT: WHY THE FUCK CAN'T I USE STRIKETHROUGH TAGS? I HATE YOU COMMUNITY SERVER!



  • Ok, I'll try this one more time: Maybe you should better organize your trolling comedy routines in your treehouse IRC channel.



  • @Cap'n Steve said:

    Wait, you guys actually think the Coldfusion jokes are funny? I thought it was one of your "meta jokes" that only gets posted so you can post "Get a sense of humor, n00b!" later on
    See, the loser in the mom's basement is just tired; everyone's used it for years. The ColdFusion joke is an inside joke. Those are funny not because of the humor but because of the exclusion of others. Also, because it results in posts from nitwits like you who have nothing better to do than to follow MPS around and feebly attack him.



  • @bstorer said:

    Also, because it results in posts from nitwits like you who have nothing better to do than to follow MPS around and feebly attack him.

    Wouldn't Cap'n Steve have to learn to use the editor to actually launch a successful attack?  See, the little button that shows ABC with a line through the middle is confusing.  It makes more sense to post a rant about how the forum software won't let you do strike-throughs. 



  • Who said anything to MPS? Are you really joined at the hip now? I was talking to you because you're the only member of the MS Fanboy Club that I can stand. Please don't go to the darkside, bstorer.



  • @Cap'n Steve said:

    Who said anything to MPS? Are you really joined at the hip now? I was talking to you because you're the only member of the MS Fanboy Club that I can stand. Please don't go to the darkside, bstorer.
     

    There is this thing called the Quote button...

    Oh nevermind.



  • @Cap'n Steve said:

    Who said anything to MPS? Are you really joined at the hip now?
    Actually, it's at the spleen.
    @Cap'n Steve said:
    I was talking to you because you're the only member of the MS Fanboy Club that I can stand.
    MS = Microsoft? Because I'm really not a Microsoft fan boy. I just don't see why they get so much derision for the same things other companies do all the time.
    @Cap'n Steve said:
    Please don't go to the darkside, bstorer.
    But... but... they have tacos and a swimming pool and pony rides!



  • @bstorer said:

    @Cap'n Steve said:
    Please don't go to the darkside, bstorer.
    But... but... they have tacos and a swimming pool and pony rides!
     

    And a large, windowless cargo van with 'Free candy' painted on the sides and a mattress in the back.



  • @bstorer said:

    @shark615 said:
    I never thought such a petty, half serious response could get some regulars
    riled up.
    Spectate? Is that you?
    @shark615 said:
    Yes gentleman I know what sarcasm is, I also know what a joke is. What
    I said was about as serious as your posts are relevant to anything other then
    an obvious need to feel superior due to spending a pathetic amount of time on
    these forums.
    That's great. My point is this: what you said was neither funny nor creative. The rest of us are putting some brainpower into it. Is it too much to ask for you to divert some of your meager mental power away from sustained breathing and put it into your response?
    @shark615 said:
    Would you like me to make some spelling or grammar mistakes so
    you can pick those apart too?
    i hav no idea wut u iz taklin abowt
    @shark615 said:
    I thought it was pretty witty and very descriptive of your type.
    Dude, you're posting on an IT forum. What the fuck do you expect if not pedantry? Also, again, your post: not witty.
    @shark615 said:
    I am sure MasterPlan could fend for himself in his never-ending quest to
    deride everyone in his attempt to feel superior.
    If you knew MPS at all, you'd know he'll have feelings of superiority regardless of what takes place on these boards.
    @shark615 said:
    He seems more then qualified
    on being a prick without any help.
    It's "than", asshole. Thanks for throwing that in there, by the way.
    @shark615 said:
    Do you feel the need to defend a regular or
    are you just a lacky; looking for some forum respect? Perhaps you smell some
    blood in the water and can't wait to jump in and rail on a 'newbie'?
    No, I just rankle at such a trite -- and incredibly wrong -- description. Further, it's a joke so beaten into the ground that hack sportswriters use it to describe bloggers. Can't you think of something funnier than the "jokes" tossed out by 90-year-old beat writers for the Podunk Gazette? If not, please just leave the humor to us professionals.
     

     

    Wow just wow. Your response to my little grammar question was lame considering it was correct. You then go on to correct a pedantic grammar mistake! Well done grasshopper. I am not sure why I am responding to you due to how absolutly retarded you are.

     

    After reading your little diatribe you are just a MSP want to be aren't you? He draws first blood with his classic IT superiority complex and you ride his coat tails in? Its ok some day you will find your own way as an anoymous asshole on a silly forum just keep trying.



  • @shark615 said:

    Wow just wow. Your response to my little grammar question was lame considering it was correct. You then go on to correct a pedantic grammar mistake! Well done grasshopper. I am not sure why I am responding to you due to how absolutly retarded you are.
    Sweet zombie Jesus, man! You really don't have any sense of humor whatsoever. Do you lack any other senses? Maybe you can't do abstract math? I ask because I'm wondering what other abilities are located in the part of the brain that gets jokes, an area you clearly lack.
    @shark615 said:
    After reading your little diatribe you are just a MSP want to be aren't you?
    Hardly. Have you seen the things he'll do for money? See, that's another joke.
    @shark615 said:
    He draws first blood with his classic IT superiority complex and you ride his coat tails in? Its ok some day you will find your own way as an anoymous asshole on a silly forum just keep trying.
    Well, we can't all find our way to asshole as easily as you have, I suppose. But while I'm typing this, let's get one thing perfectly clear: you mean absolutely nothing to anyone here. The persecution you think you're receiving is all in your head. You're not special, and the only reason I deign to respond to you is because it beats working. I'm not out for you, I'm not trying to become some big, bad forum troll king, I'm just killing time. If you were to stop saying stupid things and get a sense of humor, I'd probably never notice you again.


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.