Jump to content

What do you prefer? Readable code or faster code


davidb52

Recommended Posts

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

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

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

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

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

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...