davidb52 2 Posted February 12, 2012 Report Share Posted February 12, 2012 Hello,I'm trying to optimize my PHP code for more speed.I normally used variables twice but I found out that that is something that slow downs your speed.Although now when I'm avoiding that I think my code is less readable so I wanted to ask you guys:What do you prefer? Readable code or faster code? I'm talking about this short of things: // before:$count = mysql_result($query, 0);if($count==1){ $place = $_POST['place'];if($place>=0 and $place<=$maxsize and is_numeric($place)){ // after:if(mysql_result($query, 0)==1){ if($_POST['place']>=0 and $_POST['place']<=$maxsize and is_numeric($_POST['place'])){ Quote Link to post Share on other sites
boen_robot 107 Posted February 12, 2012 Report Share Posted February 12, 2012 I use the second form, but use some extra whitespace and order things from generic to specific, and place error checks early. Code becomes very readable that way, without becoming any less inefficient (especially if you have something like APC available). I also prefer to use strict checks whenever possible, as they are slightly faster. if (mysql_result($query, 0) === 1) { if (is_numeric($_POST['place']) and $_POST['place'] >= 0 and $_POST['place'] <= $maxsize) { (I also use "&&" instead of "and", and would use a variable for the MySQL instance, but that's mostly a matter of preference) Quote Link to post Share on other sites
justsomeguy 1,135 Posted February 12, 2012 Report Share Posted February 12, 2012 I think the second code is more readable, I don't see a point to storing a value in a variable that you only use once. If you want the code to be descriptive then add a comment to say that the result you're getting is the count. Quote Link to post Share on other sites
davidb52 2 Posted February 12, 2012 Author Report Share Posted February 12, 2012 I use the second form, but use some extra whitespace and order things from generic to specific, and place error checks early. Code becomes very readable that way, without becoming any less inefficient (especially if you have something like 1APC available). I also prefer to use strict checks whenever possible, as they are 2slightly faster.if (mysql_result($query, 0) === 1) { if (is_numeric($_POST['place']) and $_POST['place'] >= 0 and $_POST['place'] <= $maxsize) { (I also use 3"&&" instead of "and", and would 4use a variable for the MySQL instance, but that's mostly a matter of preference) Thanks both, 1. What's that?2. Oke, thanks for the tip3. I did that too a while back ago, but I think and is easier. But is your way faster?4. Yes it's a little bit more readable by than but it's also ironic because I want to use less variables. Quote Link to post Share on other sites
boen_robot 107 Posted February 12, 2012 Report Share Posted February 12, 2012 1. An extension. Besides the functions you can see, when you have it activated, it maintains a near-machine code (called "opcode" in PHP terms) that is then executed by PHP. Without APC, PHP rebuilds such opcodes at every request, and with APC, if PHP has already generated opcodes for a file, it will reuse it.3. It's not faster, but unlike "and", the "&&" operator is available in all C-style languages (JavaScript, C, C++, C#, PHP, JAVA, etc.) whereas "and" is only available in some. Because of that, I use it, so that I don't end up pausing on every "and" thinking "did this language have 'and'?".4. In the case of MySQL in particular, you might want to use MySQLi with its object form anyway. It's faster than the plain MySQL extension. Quote Link to post Share on other sites
davidb52 2 Posted February 13, 2012 Author Report Share Posted February 13, 2012 Thanks for the reply,the only problem: I don't know nothing about mysqli and pdo etc.Can you give me a good tutorials site? Quote Link to post Share on other sites
boen_robot 107 Posted February 13, 2012 Report Share Posted February 13, 2012 Most examples can be converted into MySQLi by simply using "mysqli_" instead of "mysql_" in function names. That's for procedural style... for OOP style, you can simply convert every "mysql_*(**, $link)" into "$link->*(**)". The manual on MySQLi has enough examples. Quote Link to post Share on other sites
davidb52 2 Posted February 13, 2012 Author Report Share Posted February 13, 2012 Which means I still have to change everyting....And if I have to change everything why not go for the best by than? So do you think PDO is good for mysql? Quote Link to post Share on other sites
justsomeguy 1,135 Posted February 13, 2012 Report Share Posted February 13, 2012 If you want maximum flexibility, I would recommend the Pear MDB2 database abstraction package. You can change the database provider details without rewriting the code that deals with queries. Quote Link to post Share on other sites
davidb52 2 Posted February 13, 2012 Author Report Share Posted February 13, 2012 Nah I'm only want to increase the speed not the functionallity.So PDO exists more for the functionallity and Pear MDB2 too. So mysqli for speed? What do you guys think? Quote Link to post Share on other sites
justsomeguy 1,135 Posted February 13, 2012 Report Share Posted February 13, 2012 If you want to test it then feel free to make a benchmark testing various methods. I'll guess that most of the differences in timing will be with the actual database getting the data instead of the bridge with PHP. Quote Link to post Share on other sites
regicidedelferoz 0 Posted February 14, 2012 Report Share Posted February 14, 2012 (edited) for me the socond one Edited February 14, 2012 by Regicide Del Feroz Quote Link to post Share on other sites
davidb52 2 Posted February 14, 2012 Author Report Share Posted February 14, 2012 for me the socond oneAnd the second one was? ps. signature +1 Quote Link to post Share on other sites
boen_robot 107 Posted February 14, 2012 Report Share Posted February 14, 2012 MySQLi is slightly faster than PDO, since all PDO calls go over back and forth between the PDO abstraction layer. PEAR MDB2 is even slower for the same reason (extra abstrations). However, as noted, if you need to switch to a different DB, you'll have to rewrite the DB related functions you're using.Speed or flexibility... choose... you can't have both. Quote Link to post Share on other sites
justsomeguy 1,135 Posted February 14, 2012 Report Share Posted February 14, 2012 There's a comparison about the various options native to PHP here: http://www.php.net/manual/en/mysqli.overview.php Quote Link to post Share on other sites
ShadowMage 94 Posted February 14, 2012 Report Share Posted February 14, 2012 Most examples can be converted into MySQLi by simply using "mysqli_" instead of "mysql_" in function names.Just a note, but it isn't quite that simple. Most if not all of the functions have the parameters in reverse order. I learned this the hard way. I had a relatively large database connection class that I converted to MySQLi and I did a mass find and replace to change the function names. When I started using the converted class, I got lots of errors pointing to every MySQLi function. So I had to go through my code and manually switch the parameters on every function, anyway... Quote Link to post Share on other sites
regicidedelferoz 0 Posted February 21, 2012 Report Share Posted February 21, 2012 And the second one was? ps. signature +1 if(mysql_result($query, 0)==1){ if($_POST['place']>=0 and $_POST['place']<=$maxsize and is_numeric($_POST['place'])){ you dont really need to put $_POST variables into a new one if you're going to use it only once Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.