大家可真无聊,简直透顶了,
嗯 ,现在
我做出了一个决定,
准备比大家更无聊:
把原来的再贴过来,认真看,两个代码的循环次数分别为:
15楼的:999-100+1=900次
(1重循环)
19楼的:9*10*10= 900次
(3重循环)
再分析循环体所用语句:
15楼的:一次set 直接赋值(set str=%%i)+3次set/a赋值+
3次变量截取+1次set/a算术运算(立方和)+一个判断输出(if !var! equ !str! echo !str!)
19楼的:一次set 直接赋值(set num1=!a!!b!!c!)+3次set/a赋值+
(合并)+1次set/a算术运算(立方和)+一个判断输出(if !num1! equ !num2! echo !num1!)
15楼的:@echo off
setlocal enabledelayedexpansion
for /l %%i in (100,1,999) do (
set str=%%i
set/a a=!str:~,1!, b=!str:~1,1!, c=!str:~2,1!
set /a var=!a!*!a!*!a!+!b!*!b!*!b!+!c!*!c!*!c!
if !var! equ !str! echo !str!
)
pause
19楼的:@echo off
setlocal enabledelayedexpansion
for /l %%i in (1,1,9) do for /l %%j in (0,1,9) do for /l %%k in (0,1,9) do (
set/a a=%%i,b=%%j,c=%%k
set num1=!a!!b!!c!
set /a num2=!a!*!a!*!a!+!b!*!b!*!b!+!c!*!c!*!c!
if !num1! equ !num2! echo !num1!
)
pause
以下是我的分析,(我不是计算机出身的,批处理只是业余爱好,说错话内行勿笑)
同样的循环次数,不同循环重数,花费的时间不同的,重数多了,指令就多了,花的时间就长了,这么看来就是15楼的循环花的时间短些。
问题在变量的截取上,截取是花时间的 而那句!a!!b!!c!所谓的”合并“只是3个变量放到一起了,而
a=!str:~,1!, b=!str:~1,1!, c=!str:~2,1!
这句对应地有某些指令告诉解释器,到底是str的那些部分被截取赋给变量a,b,c这是花些时间的.
这么看来,15楼的循环体内语句又稍费点时间,
一短一长,能抵消吗?
我没试过,
Everyone is really boring, simply dreadful,
Well, now
I've made a decision,
Ready to be more boring than everyone:
Paste the original again, read carefully, the number of loop iterations for the two codes are respectively:
For the 15th floor: 999-100+1=900 times
(1 loop)
For the 19th floor: 9*10*10= 900 times
(3 loops)
Then analyze the statements in the loop body:
For the 15th floor: one set direct assignment (set str=%%i) + 3 set/a assignments +
3 variable extractions + 1 set/a arithmetic operation (cube sum) + one judgment output (if !var! equ !str! echo !str!)
For the 19th floor: one set direct assignment (set num1=!a!!b!!c!) + 3 set/a assignments +
(merge) + 1 set/a arithmetic operation (cube sum) + one judgment output (if !num1! equ !num2! echo !num1!)
For the 15th floor:@echo off
setlocal enabledelayedexpansion
for /l %%i in (100,1,999) do (
set str=%%i
set/a a=!str:~,1!, b=!str:~1,1!, c=!str:~2,1!
set /a var=!a!*!a!*!a!+!b!*!b!*!b!+!c!*!c!*!c!
if !var! equ !str! echo !str!
)
pause
For the 19th floor:@echo off
setlocal enabledelayedexpansion
for /l %%i in (1,1,9) do for /l %%j in (0,1,9) do for /l %%k in (0,1,9) do (
set/a a=%%i,b=%%j,c=%%k
set num1=!a!!b!!c!
set /a num2=!a!*!a!*!a!+!b!*!b!*!b!+!c!*!c!*!c!
if !num1! equ !num2! echo !num1!
)
pause
The following is my analysis, (I'm not from a computer background, batch processing is just a hobby, don't laugh at me if I say something wrong)
With the same number of loop iterations, different numbers of loop levels, the time taken is different. More loop levels mean more instructions, and more time is spent. So it seems that the loop on the 15th floor takes less time.
The problem is with variable extraction, which takes time. And that so-called "merge" of !a!!b!!c! is just putting three variables together, while
a=!str:~,1!, b=!str:~1,1!, c=!str:~2,1!
This corresponds to some instructions telling the interpreter which parts of str are extracted and assigned to variables a, b, c, which takes some time.
So it seems that the statements in the loop body on the 15th floor also take a little more time,
One short and one long, can they cancel out?
I haven't tried it,