Home > Article > Backend Development > PHP most efficiently expands a positive integer a thousand times
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
is faster than the string of plus signs. [Recommended learning:
PHP video tutorial
]
Practice brings true knowledge
// 图表内容 $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 . '000'); } $end = Carbon::now()->getPreciseTimestamp(); $data[$count][] = ($end - $start)/1000000 . '秒'; } 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 . '秒'; } 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 . '秒'; } $this->table($headers, $data);After running this code multiple times, a relatively stable result will be obtained:
Multiplying by 1024 and then subtracting and multiplying by 24 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 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
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!