Home >Backend Development >PHP Tutorial >PHP most efficiently expands a positive integer a thousand times

PHP most efficiently expands a positive integer a thousand times

藏色散人
藏色散人forward
2021-04-19 16:52:032110browse

Plan Development

How to expand a positive integer a thousand times with the highest efficiency?

When this question is thrown to people's minds, those of us who have received excellent nine-year compulsory education can give a naughty yet master-like answer after a little thought: just add three zeros. ~

But when this problem is handed over to programmers to solve with code, the problem must be considered from a programming perspective. As phper, the first solution I thought of was

  • Solution 1: Glue the string "000" at the end of the number

  • Option 2: Simply multiply the original number by 1000

When I threw this problem to my friend, he told me:

Option 1 Definitely not! You should use option 2, but if you are a chicken thief enough, you should use positive integer X 1024 - positive integer When taking 1000, he will perform positive integer X 512 positive integer X 256 positive integer X 128 positive integer X 128 positive integer X 64 positive integer up to 1000 times. The operation

2 raised to the 10th power

is faster than the string of plus signs. [Recommended learning: PHP video tutorial]

Practice brings true knowledge

I was quickly moved by these words of the boss Got me. In order to practice the true knowledge of the big brother and prove that the big brother's explanation is like an enlightenment to me, I quickly wrote a short method, using three different methods to expand a random positive integer by 1000 times. Run 10 million times respectively to check the efficiency of each method (using framework: laravel)
// 图表内容

$headers = ['次数', '方案1:拼接法', '方案2:乘1000', '方案3:乘以 1024'];

$data = [

    [0=>'第一次'],

    [0=>'第二次'],

    [0=>'第三次']

];

// 每个方法执行三次

for ($count = 0; $count < 3; $count ++) {

    // 生成变量名 : plan1start1

    $start = Carbon::now()->getPreciseTimestamp();

    for ($i = 0; $i < 10000000; $i ++)

    {

        $integer = rand(1, 999);

        $result = (int)($integer . &#39;000&#39;);

    }

    $end = Carbon::now()->getPreciseTimestamp();

    $data[$count][] = ($end - $start)/1000000 . &#39;秒&#39;;

}

for ($count = 0; $count < 3; $count ++) {

    $start = Carbon::now()->getPreciseTimestamp();

    for ($i = 0; $i < 10000000; $i ++)

    {

        $integer = rand(1, 999);

        $result = $integer * 1000;

    }

    $end = Carbon::now()->getPreciseTimestamp();

    $data[$count][] = ($end - $start)/1000000 . &#39;秒&#39;;

}

for ($count = 0; $count < 3; $count ++) {

    $start = Carbon::now()->getPreciseTimestamp();

    for ($i = 0; $i < 10000000; $i ++)

    {

        $integer = rand(1, 999);

        $result = $integer * 1024 - $integer * 24;

    }

    $end = Carbon::now()->getPreciseTimestamp();

    $data[$count][] = ($end - $start)/1000000 . &#39;秒&#39;;

}

$this->table($headers, $data);
After running this code multiple times, a relatively stable result will be obtained:

Seeing this result, big questions occupied my little mind again.

Multiplying by 1024 and then subtracting and multiplying by 24PHP most efficiently expands a positive integer a thousand times is slower than

multiplying directly by 1000

. When I took this result to find the boss to solve my doubts, I got this answer from him: I don’t usually work enough, right? Is there still time for such an experiment?

Thoughts on the test results

I secretly despised him, and by the way, downgraded his status in my heart from Big Brother

A common friend of mine who wishes to remain anonymous

. I continued to think about this matter. To concatenate the string ‘000’ after a positive integer, convert this positive integer into a string, then concatenate the string ‘000’, and then convert it back to a positive integer. The complexity of calculation far exceeds the direct calculation of positive integers, and it is obviously inferior to the latter in terms of time. This has been verified and there is no doubt. But one of my Doudou classmates who did not want to be named explained the

binary

calculation model in the same way. I am learning

interpreted language php

, which means that a group of players who have a hardcore understanding of code have made a lot of algorithm optimizations on php where I can't see it. And Doudou is learning the compiled language C . Is it the difference in this interpreted language that leads to the different results of this operation?                                                                                                               

The above is the detailed content of PHP most efficiently expands a positive integer a thousand times. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete