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

Asked

Viewed 121 times

2

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

1

$rand_length = 1;
$rand_rules = range(0, 9);
shuffle($rand_rules);
$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";

2

$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";

3

$rand_rules = range(0, 9);
shuffle($rand_rules);
$rand_rules = array_slice($rand_rules, 1, 1);
$uni_id3 = implode($rand_rules, "");
echo $uni_id3."\n";

2 answers

12

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 change language. 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 being 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 yes is a good concern.

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

If you can’t 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 can be another. There are many variables influencing performance making it difficult to make a definitive assessment.


Give names of significant variables. You tried to give good names but did not hit the target. Use rand_id as variable to count the loop generates 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 keeps random things. But it’s just an opinion. I don’t know what your real intention is.

Also avoid keeping the final loop key on the last line. It is easier to identify the end when it is on a line alone. It’s nice but this is something that few programmers disagree.

  • I really liked your explanation @bigown on the name of the variables, these are not definitive, I created at random, so I worried on which of the three to use, to be able to improve the one that would be the most recommendable, in the case, for a slightly large application with many requests to the database and generation of many random numbers by many users at the same time, you would use the second option?

  • 4

    I think I would use the third one. Even more if it involves a bunch of data. Database is so much slower than processing this code that it will make even less difference. PHP is slow, there’s no point in optimizing your code. Not even Facebook doing micro-optimization in their PHP code (they did something much more radical :) )

0


Your question is very interesting. Php like any programming language requires optimization in the development cycle in any project. As far as the question raised and in my opinion your last option should be the most agile.

php is a scripting language developed in C. Speed will always depend on many factors, but no doubt your 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 doubt.

Thus, the only way you know which one will be the fastest is to use a timer... at the end will have an idea very close to reality.

  • could you show me an example of timer? I found a very complex but it doesn’t seem to work because it shows exactly the same thing

  • before: $time_start = microtime(true); after the method: $time_end = microtime(true); and finally: $execution_time = $time_end - $time_start.... will certainly give an idea

Browser other questions tagged

You are not signed in. Login or sign up in order to post.