Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Time 如何在DOS中为脚本计时?_Time_Dos - Fatal编程技术网

Time 如何在DOS中为脚本计时?

Time 如何在DOS中为脚本计时?,time,dos,Time,Dos,如何在DOS提示符下计时脚本 *尼克斯等效值为: $ time myscript real 0m0.886s user 0m0.846s sys 0m0.031s $ 有与此等效的DOS吗?谢谢您可以将其放在批次的开始和结束处: echo %time% 这是一个DOS批处理脚本,它将为您计算时差,而不仅仅是执行echo%time% 只要“定时用户部分”的运行时间不超过24小时,以下代码就可以正常工作。请原谅我,或者给我一些建议,这是我花了大约一个小时才完成的一项正在进行的工作 将“暂停”

如何在DOS提示符下计时脚本

*尼克斯等效值为:

$ time myscript

real 0m0.886s
user 0m0.846s
sys 0m0.031s

$

有与此等效的DOS吗?谢谢

您可以将其放在批次的开始和结束处:

echo %time%

这是一个DOS批处理脚本,它将为您计算时差,而不仅仅是执行
echo%time%

只要“定时用户部分”的运行时间不超过24小时,以下代码就可以正常工作。请原谅我,或者给我一些建议,这是我花了大约一个小时才完成的一项正在进行的工作

将“暂停”替换为调用脚本来计时自己的代码

DOS BAT文件如下:

注意:已编辑以修复前导零为八进制而非十进制的问题

注意:已编辑以处理“午夜”小时的换行,并在输出中放置前导零

@echo off
@rem --------------------------------------------
setlocal ENABLEEXTENSIONS

set start_time=%time%
echo Beginning at: %start_time%
echo Running Timed Batch File
echo.

@rem echo %*  <-- Extension for all args -- easier than following line
@rem echo %0 %1 %2 %3 %4 %5 %6 %7 %8 %9 ... ...

@rem DO YOUR WORK HERE -- see "call" / PAUSE is here to "fake" work
@rem call userscript.bat %*
PAUSE

set stop_time=%time%
echo.
echo Timed Batch File Completed
echo Start time: %start_time%
echo Stop time : %stop_time%


set TEMPRESULT=%start_time:~0,2%
call:FN_REMOVELEADINGZEROS
set start_hour=%TEMPRESULT%
@rem
set TEMPRESULT=%start_time:~3,2%
call:FN_REMOVELEADINGZEROS
set start_min=%TEMPRESULT%
@rem
set TEMPRESULT=%start_time:~6,2%
call:FN_REMOVELEADINGZEROS
set start_sec=%TEMPRESULT%
@rem
set TEMPRESULT=%start_time:~9,2%
call:FN_REMOVELEADINGZEROS
set start_hundredths=%TEMPRESULT%

set TEMPRESULT=%stop_time:~0,2%
call:FN_REMOVELEADINGZEROS
set stop_hour=%TEMPRESULT%
@rem
set TEMPRESULT=%stop_time:~3,2%
call:FN_REMOVELEADINGZEROS
set stop_min=%TEMPRESULT%
@rem
set TEMPRESULT=%stop_time:~6,2%
call:FN_REMOVELEADINGZEROS
set stop_sec=%TEMPRESULT%
@rem
set TEMPRESULT=%stop_time:~9,2%
call:FN_REMOVELEADINGZEROS
set stop_hundredths=%TEMPRESULT%

set /A start_total=(((((%start_hour%*60)+%start_min%)*60)+%start_sec%)*100)+%start_hundredths%
set /A stop_total=(((((%stop_hour%*60)+%stop_min%)*60)+%stop_sec%)*100)+%stop_hundredths%

set /A total_time=%stop_total% - %start_total%

set /A total_hundredths=%total_time% %% 100
set total_hundredths=00%total_hundredths%
set total_hundredths=%total_hundredths:~-2%
set /A total_time=%total_time% / 100

set /A total_sec="%total_time% %% 60"
set total_sec=00%total_sec%
set total_sec=%total_sec:~-2%
set /A total_time=%total_time% / 60

set /A total_min="%total_time% %% 60"
set total_min=00%total_min%
set total_min=%total_min:~-2%
set /A total_time=%total_time% / 60

set /A total_hour="%total_time% %% 60"
@rem Handle if it wrapped around over midnight
if "%total_hour:~0,1%"=="-" set /A total_hour=%total_hour% + 24

echo Total time: %total_hour%:%total_min%:%total_sec%.%total_hundredths%

@rem --------------------------------------------
@rem Exit the BAT Program
endlocal
goto END

@rem --------------------------------------------
@rem FN_REMOVELEADINGZEROS function
@rem  Used to remove leading zeros from Decimal
@rem  numbers so they are not treated as Octal.
:FN_REMOVELEADINGZEROS
if "%TEMPRESULT%"=="0" goto END
if "%TEMPRESULT:~0,1%" NEQ "0" goto END
set TEMPRESULT=%TEMPRESULT:~1%
goto FN_REMOVELEADINGZEROS

@rem --------------------------------------------
@rem BAT PROGRAM / FUNCTION FILE EXIT
:END
@echo关闭
@雷姆--------------------------------------------
setLocalEnableExtensions
设置开始时间=%time%
回音开始时间:%start\u时间%
echo运行定时批处理文件
回声。

@rem echo%*您可以从中使用timeit.exe。您还可以获得大量其他有用的工具和Unix命令端口

C:\Documents and Settings\Rob>timeit sleep 3

Version Number:   Windows NT 5.1 (Build 2600)
Exit Time:        2:40 pm, Thursday, December 10 2009
Elapsed Time:     0:00:03.296
Process Time:     0:00:00.015
System Calls:     7576
Context Switches: 1974
Page Faults:      1072
Bytes Read:       6172
Bytes Written:    11086
Bytes Other:      144432

如果您不介意使用第三方EXE提供帮助,那么from非常有用

setlocal ENABLEEXTENSIONS
FOR /F %%G IN ('timer') DO SET start_time=%%G

@rem Do your work Here
call userscript.bat %*

timer %start_time% "Total time"
endlocal

你真的是在谈论DOS,还是windows的命令外壳?这种方法的可能重复对我来说是最简单的,但有一点麻烦。我正在为位于c:\windows\system32中的dos批处理文件计时。当我尝试计时时,我必须输入.bat的完全限定路径,即使它在我的系统路径中。警告:不要直接在命令上使用它(
echo%time%&myprog&echo%time%
),因为这两个命令的扩展都是在命令执行之前完成的。