davidb52 Posted February 12, 2012 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'])){ Link to comment Share on other sites More sharing options...
boen_robot Posted February 12, 2012 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) Link to comment Share on other sites More sharing options...
justsomeguy Posted February 12, 2012 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. Link to comment Share on other sites More sharing options...
davidb52 Posted February 12, 2012 Author 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. Link to comment Share on other sites More sharing options...
boen_robot Posted February 12, 2012 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. Link to comment Share on other sites More sharing options...
davidb52 Posted February 13, 2012 Author 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? Link to comment Share on other sites More sharing options...
boen_robot Posted February 13, 2012 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. Link to comment Share on other sites More sharing options...
davidb52 Posted February 13, 2012 Author 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? Link to comment Share on other sites More sharing options...
justsomeguy Posted February 13, 2012 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. Link to comment Share on other sites More sharing options...
davidb52 Posted February 13, 2012 Author 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? Link to comment Share on other sites More sharing options...
justsomeguy Posted February 13, 2012 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. Link to comment Share on other sites More sharing options...
regicidedelferoz Posted February 14, 2012 Share Posted February 14, 2012 for me the socond one Link to comment Share on other sites More sharing options...
davidb52 Posted February 14, 2012 Author Share Posted February 14, 2012 for me the socond oneAnd the second one was? ps. signature +1 Link to comment Share on other sites More sharing options...
boen_robot Posted February 14, 2012 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. Link to comment Share on other sites More sharing options...
justsomeguy Posted February 14, 2012 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 Link to comment Share on other sites More sharing options...
ShadowMage Posted February 14, 2012 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... Link to comment Share on other sites More sharing options...
regicidedelferoz Posted February 21, 2012 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 Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.