Jump to content
MakeWebGames

Speed differences within PHP


a_bertrand

Recommended Posts

I am not sure how to test this. Which would be faster?

This one.

$t=$db->query("SELECT agility FROM userstats WHERE userid={['$ir['userid']}");
$r=$db->fetch_row($t);

 

Or this one.

$t=($db->fetch_row($db->query("SELECT agility FROM userstats WHERE userid={$ir['userid']}")));

 

It seems to me that the second one would be faster but I'm not sure.

Or is it just a case of keeping the code shorter?

Link to comment
Share on other sites

I am not sure how to test this. Which would be faster?

This one.

$t=$db->query("SELECT agility FROM userstats WHERE userid={['$ir['userid']}");
$r=$db->fetch_row($t);

 

Or this one.

$t=($db->fetch_row($db->query("SELECT agility FROM userstats WHERE userid={$ir['userid']}")));

 

It seems to me that the second one would be faster but I'm not sure.

Or is it just a case of keeping the code shorter?

Theoretically, the second one would be faster, but that's just theory.

Any number of things can make it differ.

The reason I can think of is that the second variable is never assigned, thus using less memory.

But, in terms of how PHP executes those statements, it should be exactly the same.

The bottle-neck there is that ->query() isn't a form of a closure - if it were, it would be faster(such as PDO).

Link to comment
Share on other sites

Can you explain that further, please. I don't quite understand what you mean by closure.

I'm not even going to try explaining closures, it could take an eternity to understand.

Instead, here's a wiki with a lot of different people taking their own approaches at explaining it.

http://stackoverflow.com/questions/111102/how-do-javascript-closures-work

I tried to write a proper example in PHP, but unfortunately I couldn't create a good example, so see the following link for practical applications of closured in PHP: http://www.vancelucas.com/blog/practical-uses-for-php-5-3-closures/

In PHP, a closure makes use of the same semantics as an anonymous functions, but I'm not even going to stand by this point at all.

In JavaScript, I use closures daily - there are many examples: the whole of jQuery is riddled with closures everywhere.

But, they are fairly easy and convenient - if you know your variable scope, and how data flows trough an application.

Edit: This is also a good read: https://wiki.php.net/rfc/closures

Edited by Spudinski
Link to comment
Share on other sites

  • 5 months later...

Note that } elseif() { is somewhat faster than } else if() {

===================================

Test (100,000,000 runs):

<?php
$start = microtime(true);
for($i = 0; $i < 100000000; $i++) {
   if(2 === 0) {} else if(2 === 1) {} else {}
}
$end = microtime(true);
echo "1: ".($end - $start)."\n";
unset($start, $end);

$start = microtime(true);
for($i = 0; $i < 100000000; $i++) {
   if(2 === 0) {} elseif(2 === 1) {} else {}
}
$end = microtime(true);
echo "2: ".($end - $start);
?>

===================================

Result (depending on hardware configuration):

1: 20.026723146439

2: 20.20437502861

:):):):):):):):):):):):):):):)

 

 

I LOL'ed

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...