标题: [数值计算] [已更新]cmd指令执行效率简易测试工具 [打印本页]
作者: plp626 时间: 2011-6-15 21:22 标题: [已更新]cmd指令执行效率简易测试工具
本帖最后由 plp626 于 2011-6-16 19:04 编辑
- @ECHO off
- :: cmd 命令执行效率测试,保存为cff.bat于windows目录下,
- :: 命令行调用格式:cff 1000 "命令名" "命令名" ...
- :: 举例:
- :: cff 300 "chcp >nul"
- :: cff 10000 "set var=123" "set var=^!random^!" "shift"
- :: cff 10000 "for /f %a in ("abcdef")do set str=%a"
- :: Version: 1.01; Code by plp; Thanks to: fastslz, batchar
-
- setlocal EnableDelayedExpansion
- set/a $lp=%1 2>nul||(
- echo Parameter %%1 --- %1
- echo is invalid number.&exit/b2
- )
- if %2.==. echo Paramter %%2 is missing.&exit/b3
- call:getinf&rem :: 获取版本信息和CPU信息
-
- :begin
- set "cmd=%~2"
- Set $time1=%time%
- for /l %%@ in (1 1 %$lp%)do %~2
- Set $time2=%time%
- call:etime $time1 $time2 $t12
- if !$t12!==0 echo Parameter %%1 --- %$lp%&echo is too small.&exit/b4
- set/a $cff=$lp*100/$t12,$r=$lp*100%%$t12*10/$t12
-
- echo === [!$time1! --^> !$time2!=!$t12!] ===
- ECHO "!cmd!" : !$cff!.!$r! (Hz)
- if !$t12! leq 20 (
- echo.&echo ^^! Paramter %%1 --- %$lp%
- echo is smallish and maybe the result "!$cff!.!$r!" is not accurate
- exit/b5
- )
- if %3. neq . shift&goto:begin
- goto:eof
-
- :etime <beginTime_VarName> <endTime_VarName> <retVar>
- Set/a "%3=(!%2:~,2!-!%1:~,2!)*360000+(1!%2:~3,2!-1!%1:~3,2!)*6000+1!%2:~-5,2!!%2:~-2!-1!%1:~-5,2!!%1:~-2!,%3+=-8640000*(%3>>31)"&goto:eof
- :getinf
- ver&wmic cpu get name|find "CPU"
- set NUMBER_OF_PROCESSORS
复制代码
作者: fastslz 时间: 2011-6-15 22:47
本帖最后由 fastslz 于 2011-6-17 11:33 编辑
我也来一个vbs的
cn-dos旧作- 'CPU 基准测试+信息查询,好玩又防奸商,去电脑城选电脑带上一个吧!CPU信息一目了然。
- 'CPUtest.vbs BY: fastslz http://bbs.cn-dos.net http://bbs.bathome.net
- Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
- Set CPUs = objWMIService.InstancesOf("Win32_Processor")
- Set Caches = objWMIService.InstancesOf("Win32_CacheMemory")
- For Each ObjCache In Caches
- Select Case objCache.Level
- case 3
- aUsage = "一级缓存: " & objCache.MaxCacheSize & "KB (一级数据+L1缓存)"
- case 4
- aUsage = aUsage & vbCrLf & "二级缓存: " & objCache.MaxCacheSize & "KB"
- case 5
- aUsage = aUsage & vbCrLf & "三级缓存: " & objCache.MaxCacheSize & "KB"
- End Select
- Next
- For Each ObjCPU In CPUs
- Usage = "CPU 名称: " & ltrim(ObjCPU.Name) & Chr(9) & vbCrLf _
- & "CPU 构架: " & ObjCPU.Description & vbCrLf _
- & "制造厂商: " & ObjCPU.Manufacturer & vbCrLf _
- & "接口规格: " & ObjCPU.SocketDesignation & vbCrLf _
- & "核心数量: " & ObjCPU.NumberOfCores & vbCrLf _
- & "地址位宽: " & ObjCPU.AddressWidth & " Bit" & vbCrLf _
- & "数据位宽: " & ObjCPU.DataWidth & " Bit" & vbCrLf _
- & "CPU 电压: " & ObjCPU.CurrentVoltage / 10 & "V" & vbCrLf _
- & "主频速度: " & ObjCPU.CurrentClockSpeed & "MHZ" & vbCrLf _
- & "总线速度: " & ObjCPU.ExtClock & "MHZ" & vbCrLf & aUsage
- Next
- If (Lcase(Right(Wscript.FullName,11)) = "wscript.exe") Then
- MsgBox Usage , vbInformation ,"CPU 基准测试"
- Else
- WScript.Echo Usage
- End If
- Dim I,T1,T2,Tempvalue,aRunTime,bRunTime
- T1 = Timer()
- For I = 1 To 2000000
- Tempvalue= 2^0.5
- Next
- T2 = Timer()
- aRunTime = FormatNumber((T2-T1)*1000,2)
- NumMsg = "CPU 200万次开方计算所需时间:" &aRunTime&" 毫秒" &Chr(9)
-
- T1 = Timer()
- For I = 1 To 6000000
- Tempvalue= 1 + 1
- Next
- T2 = Timer()
- bRunTime = FormatNumber((T2-T1)*1000,2)
- NumMsg = NumMsg & vbCrLf & "CPU 600万次加法计算所需时间:"&bRunTime&" 毫秒" &Chr(9)
- If (Lcase(Right(Wscript.FullName,11)) = "wscript.exe") Then
- MsgBox NumMsg , vbInformation ,"CPU 基准测试"
- Else
- WScript.Echo NumMsg
- End If
复制代码
作者: plp626 时间: 2011-6-16 12:44
- Microsoft Windows XP [版本 5.1.2600]
- Genuine Intel(R) CPU T1600 @ 1.66GHz
- NUMBER_OF_PROCESSORS=2
-
- "rem" : 366678.1 (Hz)
- "shift" : 139301.5 (Hz)
- "if a==b 命令" : 145857.1 (Hz)
- "@echo off" : 116761.1 (Hz)
- "break" : 107383.2 (Hz)
- "popd" : 108809.0 (Hz)
- "set a=1" : 70921.9 (Hz)
复制代码
结论:- rem >> if a==b 命令 >> shift >> @echo off >> break >> set a=1
复制代码
作者: fastslz 时间: 2011-6-17 10:36
plp626 兄
在Windows7 64bit Q8300(四核CPU)环境下,经过多次测试发现cmd wscript cscript在不调用外部命令的情况下CPU占用始终保持在25%,甚至只使用一个线程(核心),之前你写的圆周率计算的代码更奇怪一个
核心CPU占用95%其它3个核心空闲中,这个情况何解?
http://bbs.bathome.net/viewthread.php?tid=3437
作者: plp626 时间: 2011-6-17 15:35
4# fastslz
也许是因为cmd,cscript,wscript这些脚本解释器都是单线程程序,使用内部命令时,始终是一个线程运行。
如果开多个cmd进程,就相当于多个线程,在多核系统中,系统根据任务调度的基本单位--线程,很可能根据情况分多个cpu。
(仅是猜测,勉强可以解释现象。)
作者: fastslz 时间: 2011-6-17 16:01
5# plp626
是啊、可以猜测一下
cmd+系统自带外部命令 单线程、多个线程
cmd+内部命令 只能是单线程
建议改进下代码,测试每秒能运算圆周率小数点位数
作者: plp626 时间: 2011-6-17 17:25
每秒运算圆周率位数这个不能说明问题,计算圆周率的算法太多,没有个统一参考;
我倒是有个想法,测试cmd的echo off指令执行频率,容易统一比较;
还有一楼的代码太繁琐了,不知不觉都40行了,准备写个精简版的,可以根据是否为外部命令智能给出合适的循环次数,不用人为指定循环次数;
作者: plp626 时间: 2011-6-17 17:42
本帖最后由 plp626 于 2011-6-17 21:36 编辑
这个无需给定命令循环次数,由程序指定。- :: cff "命令名"; "命令名"; ...
- :: 举例:
- :: cff "set var=^!random^!"
- :: cff "call set a=1"; "call set/a a=1"; "set/a a=1"
- :: cff "for /f %a in ("abcdef")do set str=%a"
复制代码
- :: cmd 命令执行效率测试,保存为cff.bat于windows目录下,
- :: 命令行调用格式:
- :: cff "命令名"; "命令名"; ...
- :: Version: 1.1; Code by plp; Thanks to: fastslz, batchar
- @ECHO off&setlocal EnableDelayedExpansion&call:getinf
- echo ------------ CMD指令测试 -------------
- :beg
- set/a $lp=random%%5+1&set "cmd=%~1"
- :begin
- Set $time1=%time%
- for /l %%@ in (1 1 %$lp%)do %cmd%
- Set $time2=%time%
- call:etime $time1 $time2 $t12
- if !$t12! leq 20 set/a $lp=$lp*2+5&goto:begin
- set/a $cff=$lp*100/$t12,$r=$lp*100%%$t12*10/$t12
- call:print
- if %2. neq . shift&goto:beg
- goto:eof
-
- :etime <beginTime_VarName> <endTime_VarName> <retVar>
- Set/a "%3=(!%2:~,2!-!%1:~,2!)*360000+(1!%2:~3,2!-1!%1:~3,2!)*6000+1!%2:~-5,2!!%2:~-2!-1!%1:~-5,2!!%1:~-2!,%3+=-8640000*(%3>>31)"&goto:eof
- :getinf 获取版本信息和CPU信息
- ver&wmic cpu get name|find "CPU"
- set NUMBER_OF_PROCESSORS&goto:eof
- :print 格式化输出
- set "prt="!cmd!" "
- set $cf= !$cff!.!$r!&set $cf=!$cf:~-8! Hz
- if !cmd:~37!. neq . (echo "!cmd!" @ !$cf!)else (
- echo !prt:~0,37! @ !$cf!
- )
复制代码
作者: lllsoslll 时间: 2011-6-17 21:33
1.1版本的:
运行 —> cmd —> 粘贴代码,回车:- cd.>f.t&cd.>g.t
- cff "set a=1" "set a=^!random:~2,1^!"; "set a=^!random:2=#^!"; "set abcd12345678901234567890=$$"; "call set a=1234567890"; "call set/a a=2" "call set f=%%a%%"; popd "setlocal&endlocal"; "if a==0 ?"; "for %a in (1)do set b=%a"; "for /f %a in ("1")do set b=%a"; "ver>nul"; "echo off>a.t" "echo.>b.t 4>a.t" "echo.>b.t" "if exist xxxx.txt ?"; "for %a in (#.txt)do set x=%a"; rem; "copy/b f.t+g.t f.t>nul"; "copy f.t+g.t f.t>nul" shift
复制代码
输出:- Microsoft Windows XP [版本 5.1.2600]
- Genuine Intel(R) CPU T1600 @ 1.66GHz
- NUMBER_OF_PROCESSORS=2
- ------------ CMD指令测试 -------------
- "set a=1" @ 70202.8 Hz
- "set a=!random:~2,1!" @ 40943.3 Hz
- "set a=!random:2=#!" @ 37221.2 Hz
- "set abcd12345678901234567890=$$" @ 55843.1 Hz
- "call set a=1234567890" @ 275.7 Hz
- "call set/a a=2" @ 6124.0 Hz
- "call set f=%%a%%" @ 275.7 Hz
- "popd" @ 102379.1 Hz
- "setlocal&endlocal" @ 2461.2 Hz
- "if a==0 ?" @ 136519.4 Hz
- "for %a in (1)do set b=%a" @ 43867.8 Hz
- "for /f %a in ("1")do set b=%a" @ 43867.8 Hz
- "ver>nul" @ 6124.0 Hz
- "echo off>a.t" @ 2631.0 Hz
- "echo.>b.t 4>a.t" @ 1263.3 Hz
- "echo.>b.t" @ 1804.7 Hz
- "if exist xxxx.txt ?" @ 6959.0 Hz
- "for %a in (#.txt)do set x=%a" @ 39622.5 Hz
- "rem" @ 351067.8 Hz
- "copy/b f.t+g.t f.t>nul" @ 748.0 Hz
- "copy f.t+g.t f.t>nul" @ 692.5 Hz
- "shift" @ 140420.0 Hz
复制代码
作者: plp626 时间: 2011-6-17 23:37
本帖最后由 plp626 于 2011-6-17 23:55 编辑
8楼的代码(version 1.1版本)win7下报错,具体原因下次公布答案,现在还在思考;
这次升级到1.11版,win7兼容版:- :: cmd 命令执行效率测试,保存为cff.bat于windows目录下,
- :: 命令行调用格式:
- :: cff "命令名"; "命令名"; ...
- :: Version: 1.11; Code by plp; Thanks to: fastslz, batchar
- @ECHO off&setlocal EnableDelayedExpansion&call:getinf
- echo ------------ CMD指令测试 -------------
- :beg
- if "%~1"=="" goto:eof
- :: 比较纳闷的这句,在win7下改为 if %1.==. goto:eof 会出错!!
- set/a $lp=random%%5+1&set "cmd=%~1"
- :begin
- Set $time1=%time%
- for /l %%@ in (1 1 %$lp%)do %cmd%
- Set $time2=%time%
- call:etime $time1 $time2 $t12
- if !$t12! leq 20 set/a $lp=$lp*2+5&goto:begin
- set/a $cff=$lp*100/$t12,$r=$lp*100%%$t12*10/$t12
- call:print & shift& goto:beg
-
- :etime <beginTime_VarName> <endTime_VarName> <retVar>
- Set/a "%3=(!%2:~,2!-!%1:~,2!)*360000+(1!%2:~3,2!-1!%1:~3,2!)*6000+1!%2:~-5,2!!%2:~-2!-1!%1:~-5,2!!%1:~-2!,%3+=-8640000*(%3>>31)"&goto:eof
- :getinf 获取版本信息和CPU信息
- ver&wmic cpu get name|find "CPU"
- set NUMBER_OF_PROCESSORS&goto:eof
- :print 格式化输出
- set "prt="!cmd!" "
- set $cf= !$cff!.!$r!&set $cf=!$cf:~-8! Hz
- if !cmd:~37!. neq . (echo "!cmd!" @ !$cf!)else (
- echo !prt:~0,37! @ !$cf!
- )
复制代码
作者: fastslz 时间: 2011-6-19 12:00
不知道怎么回事,今天怎么测试8楼的也不报错了,见鬼了- cff.bat "rem";"break";"popd";"set var=^!random^!";"call set a=1";"for /f %a in ("abcdef")do set str=%a";"setlocal&endlocal";"find /?>nul";"findstr /?>nul"
复制代码
Microsoft Windows [版本 6.1.7601]
Intel(R) Core(TM)2 Quad CPU Q8300 @ 2.50GHz
NUMBER_OF_PROCESSORS=4
------------ CMD指令测试 -------------
"rem" @ 531359.4 Hz
"break" @ 25579.1 Hz
"popd" @ 25579.1 Hz
"set var=!random!" @ 17052.7 Hz
"call set a=1" @ 947.5 Hz
"for /f %a in ("abcdef")do set str=%a @ 17540.0 Hz
"setlocal&endlocal" @ 5670.3 Hz
"find /?>nul" @ 138.7 Hz
"findstr /?>nul" @ 172.0 Hz
Microsoft Windows XP [版本 5.1.2600]
Intel(R) Core(TM)2 Quad CPU Q8300 @ 2.50GHz
NUMBER_OF_PROCESSORS=4
------------ CMD指令测试 -------------
"rem" @ 531359.4 Hz
"break" @ 158538.7 Hz
"popd" @ 158538.7 Hz
"set var=!random!" @ 55831.8 Hz
"call set a=1" @ 1804.7 Hz
"for /f %a in ("abcdef")do set str=%a @ 63002.5 Hz
"setlocal&endlocal" @ 18055.8 Hz
"find /?>nul" @ 63.3 Hz
"findstr /?>nul" @ 63.3 Hz
可见WinXP和Win7 cmd执行效率差异和大
作者: plp626 时间: 2011-6-19 18:35
从你的测试数据看,win7想对于xp:
仅外部命令启动所花费的时间变少了,是xp时间的一半。
cmd的内部命令执行效率反而低了!而且低了1倍以上!
像set这样实用非常频繁的指令,xp下执行的效率是win7的近4倍!
很意外,这到底是什么原因呢?
作者: fastslz 时间: 2011-6-19 19:13
本帖最后由 fastslz 于 2011-6-19 19:47 编辑
Microsoft Windows [版本 6.1.7601]
Intel(R) Core(TM) i3 CPU M 370 @ 2.40GHz
NUMBER_OF_PROCESSORS=4
------------ CMD指令测试 -------------
"rem" @ 531359.4 Hz
"break" @ 58490.4 Hz
"popd" @ 68252.7 Hz
"set var=!random!" @ 22737.0 Hz
"call set a=1" @ 692.5 Hz
"for /f %a in ("abcdef")do set str=%a @ 26691.3 Hz
"setlocal&endlocal" @ 7290.4 Hz
"find /?>nul" @ 122.8 Hz
"findstr /?>nul" @ 153.5 Hz
Microsoft Windows XP [版本 5.1.2600]
Intel(R) Core(TM) i3 CPU M 370 @ 2.40GHz
NUMBER_OF_PROCESSORS=4
------------ CMD指令测试 -------------
"rem" @ 634203.2 Hz
"break" @ 153584.3 Hz
"popd" @ 182025.9 Hz
"set var=!random!" @ 66408.1 Hz
"call set a=1" @ 3179.1 Hz
"for /f %a in ("abcdef")do set str=%a @ 74457.5 Hz
"setlocal&endlocal" @ 10223.3 Hz
"find /?>nul" @ 29.6 Hz
"findstr /?>nul" @ 30.4 Hz
还得改进
i3 M370是双核心4线程
NUMBER_OF_PROCESSORS其实是线程数
wmic cpu get NumberOfCores这个是核心数
再增加64bit 或32bit系统
wmic cpu get AddressWidth 或set PROCESSOR_ARCHITECTURE
从上面的数据可以看出原生4核和4线程还是有区别的,xp下外部命令效率提示很明显- @echo off
- for /f "delims=" %%i in ('wmic cpu get name^,AddressWidth^,NumberOfCores^,NumberOfLogicalProcessors /Value') do set "%%i">nul
- echo %Name%
- echo 核芯数 %NumberOfCores%
- echo 线程数 %NumberOfLogicalProcessors%
- echo %AddressWidth% Bit
- ver
复制代码
作者: qzwqzw 时间: 2011-6-20 11:12
之前在想一个可以测试代码性能的工具
测试结果仅于代码有关
而与测试环境无关
可惜试了很多思路
没有找到足够完整的测试基准
可以抵消测试环境对测试结果的影响
作者: cjiabing 时间: 2011-6-20 22:39
不明白,求真相
作者: fly2sky 时间: 2011-6-29 23:42
本帖最后由 fly2sky 于 2011-6-29 23:45 编辑
原来没有认真看说明,还以为2008R2不行呢
作者: zhj 时间: 2011-6-30 19:08
之前在想一个可以测试代码性能的工具
测试结果仅于代码有关
而与测试环境无关
可惜试了很多思路
没有找到足够完整的测试基准
可以抵消测试环境对测试结果的影响
qzwqzw 发表于 2011-6-20 11:12
用DOSBOX之类限制CPU频率即可,或者用虚拟机限制
作者: mstsc 时间: 2011-7-24 20:28
好深奥 一点都不懂 现在只能复制粘贴 用用
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |