Board logo

标题: [讨论]批处理对抗 [打印本页]

作者: willsort     时间: 2006-6-28 21:44    标题: [讨论]批处理对抗

To All:

      日来天气闷热无所获,偶得此批处理对抗调度程序[1],同时随意写了一个最简单的对抗代码[2]。虽然作了一些公平性处理,但仍然无法完全达到公平对抗的目标。在数次的测试中,均是优先调度的失败。看看大家有什么办法。

[1] Scheduler.cmd - V1 - 批处理代码对抗调度程序
:: Scheduler.cmd - V1 - 批处理代码对抗调度程序
:: Will Sort - 2006-06-28 - CMD@WinXP
@echo off & setlocal
if not exist battle\ md battle
copy test1.cmd battle>nul
copy test2.cmd battle>nul
cd battle
:Wait1
if 1%time:~6,2% GEQ 157 goto Wait1
set /a sec=1%time:~6,2%+3
set wartime=%time:~0,6%%sec:~1,2%.00
echo Wartime:%wartime%
start test2.cmd %wartime% test1.cmd 2>nul
start test1.cmd %wartime% test2.cmd 2>nul

:Wait2
if exist test1.cmd if exist test2.cmd goto wait2

:Finish
if not exist test1.cmd echo test1 fail!
if not exist test2.cmd echo test2 fail!
cd..
pause
[2] test1.cmd & test2.cmd
@echo off

:Wait
if not "%time%"=="%1" goto wait
echo %time%.
echo Start %0 at %time% ...

:Start
del %2
if exist %2 goto start

作者: 3742668     时间: 2006-6-28 22:41
无语。。。
不过就代码上来看,个人认为问题应该出在时间上,批处理似乎并不能很精确地处理时间,而在本例代码中时间精确到了微秒,估计是这方面出了问题。更改代码如下:
:: Scheduler.cmd - V1 - 批处理代码对抗调度程序
:: Will Sort - 2006-06-28 - CMD@WinXP
@echo off & setlocal
if not exist battle\ md battle
copy test1.cmd battle>nul
copy test2.cmd battle>nul
cd battle
:Wait1
if 1%time:~6,2% GEQ 157 goto Wait1
set /a sec=1%time:~6,2%+3
set wartime=%time:~0,6%%sec:~1,2%
echo Wartime:%wartime%
start test2.cmd %wartime% test1.cmd 2>nul
start test1.cmd %wartime% test2.cmd 2>nul

:Wait2
if exist test1.cmd if exist test2.cmd goto wait2

:Finish
if not exist test1.cmd echo test1 fail!
if not exist test2.cmd echo test2 fail!
cd..
pause
test1 & test2:
@echo off

:Wait
if not "%time:~0,8%"=="%1" goto wait
echo %time%.
echo Start %0 at %time% ...

:Start
del %2
if exist %2 goto start
测试了4次,其中test1获胜一次,test2获胜二次,平一次(意料之外……)。
之所以对平一次感到意外,原因如下:
test1:
@echo off
del test2.cmd
test2:
@echo off
del test1.cmd
用鼠标同时选中二文件,然后按下回车键,两文件同时运行(看起来如此),结果每次test2都被test1给挂了。当然,就算把两个文件的排列顺序颠倒,然后再选取执行结果也是一样,看来文件名占了很大的优势啊。
进而想到在前面的测试中出现平手的情况,只能感慨CMD用在处理循环上的时间实在是太长了。
另:关于循环延时,想起了以前的一个贴子:   从一个GIF文件引起的麻烦

后记:大家无聊的时候都做甚么?有无若willsort兄这般拿CMD来打发时光?老手与新手,高手与菜鸟的区别就是从这里开始的。
鄙视自己一个先,居然无聊的时候打 傲剑狂刀。
作者: doscc     时间: 2006-6-29 14:08
我也ps 一下自己! 有空在手机论坛潜水!
以下这版主的代码:

if 1%time:~6,2% GEQ 157 goto Wait1   rem 为什么用 157 这是自定的吗?
set /a sec=1%time:~6,2%+3   rem 这里我觉得不用加 1 (1%tiem:~6,8%) 吧 ,因为在下面一句  (%sec:~1,2%.00) 把 左边第一个字符去了.
set wartime=%time:~0,6%%sec:~1,2%.00

改后代码:
if 1%time:~6,2% GEQ 157 goto Wait1
set /a sec=%time:~6,2%+3
set wartime=%time:~0,6%%sec%.00
3次运行版主的原代码.都是 test1.cmd 赢! 佩服版主的思路

[ Last edited by doscc on 2006-6-29 at 14:10 ]
作者: xiongwei2624     时间: 2006-6-29 15:40
比较深奥!还要慢慢研究!
作者: 3742668     时间: 2006-7-1 10:04


  Quote:
if 1%time:~6,2% GEQ 157 goto Wait1   rem 为什么用 157 这是自定的吗?

因为57+3=60

  Quote:
set /a sec=1%time:~6,2%+3   rem 这里我觉得不用加 1 (1%tiem:~6,8%) 吧 ,因为在下面一句  (%sec:~1,2%.00) 把 左边第一个字符去了.

如果时间为08或09呢?
c:\>set /a var=08+1
无效数字。数字常数只能是十进制(17),十六位进制(0x11)或八进制(021)。

作者: doscc     时间: 2006-7-1 11:40
嗯`
谢谢 3742668版主