Which of these three codes is most recommended in memory and speed?


I would like to know which of these codes would be most recommended for speed, since all three do the same job:


$rand_length = 1;
$rand_rules = range(0, 9);
$rand_rules = array_slice($rand_rules, 1, 9);
$uni_id1 = "";
for ($rand_id = 0; $rand_id < $rand_length; $rand_id++) {
    $uni_id1 .= $rand_rules[$rand_id]; }
echo $uni_id1."\n";


$rand_length = 1;
$rand_rules = "123456789";
$uni_id2 = "";
for ($rand_id = 0; $rand_id < $rand_length; $rand_id++) {
    $uni_id2 .= $rand_rules[mt_rand(0, strlen($rand_rules)-1)]; }
echo $uni_id2."\n";


$rand_rules = range(0, 9);
$rand_rules = array_slice($rand_rules, 1, 1);
$uni_id3 = implode($rand_rules, "");
echo $uni_id3."\n";
asked by anonymous 03.02.2015 / 00:00

2 answers


Your question is very interesting. PHP as any programming language requires optimizations in the development cycle in any project. As for the question put and in my opinion your last option should be the most agile.

Php is a scripting language developed in C. The speed will always depend on many factors, but without doubt that its third option gives C the "dirty" work of cycles that in C will certainly be faster, however the same task is performed using more than one function, which leaves me in doubt.

So the only way you'll know which one will be the fastest is to use a timer ... in the end you'll get a pretty close picture.

03.02.2015 / 10:45

There is little difference and in PHP the concern should not be this. This is called micro-optimization and should be avoided until it proves absolutely necessary. If you really needed to optimize memory and speed the first thing I would tell you is to switch languages. PHP is not suitable for applications that require the best optimization.

In PHP reasonable optimizations are those in which you choose the right data structure and algorithm. Do not improve small code details.

That said, the second seems to be slightly better in performance and perhaps minimally in memory (again, reinforcement that makes no real difference). It seems to me to be more readable too (not everyone would agree). This is a good concern.

The third (which later came in edition) seems to be the slowest (I can not guarantee and will not waste time trying to figure it out, it just is not worth it) but it may not be. It is the shortest and simplest, most abstract. I would probably go into it after I've learned to use the loop well. Maybe by changing the range by the string literal of the second.

If you can not tell the difference between them, it makes no difference. And if you measure and see that one is better than another, just change a little and the result may be different. There are many variables influencing performance making it difficult to make a definitive assessment.

Give names of meaningful variables. You tried to give good names but did not hit the target. Using rand_id as a variable to count the loop causes confusion because it indicates that it should be something random. It seems to me that all these prefixes rand in the names are all exaggerated because none of them holds random things. But it's just an opinion. I do not know what your real intention is.

Also avoid keeping the end key of the loop on the last line. It is easier to identify the end when she is on a line by herself. It's taste but this is something few programmers disagree with.

03.02.2015 / 00:17