本帖最后由 CrLf 于 2015-2-6 19:54 编辑
回复 11# terse
用顶楼数据生成了 468M 大小的文件做了一组测试,结果让我大跌眼镜,gawk 和 sed 竟然这么慢
gawk 耗时58.6s,内存占用可忽略不计- @echo off
- set t=%time%
- gawk "/^Date\/Time:/{$a=$0}END{print $a}" tmp.txt
- echo %t% %time%
- pause
复制代码 sed 耗时未知(太久了,等不起),内存占用可忽略不计- @echo off
- set t=%time%
- sed -n "/^Date\/Time:/h;${g;p}" tmp.txt
- echo %t% %time%
- pause
复制代码 find /c + skip 耗时79.7s,内存占用可忽略不计- @echo off
- set t=%time%
- find "Date/Time:"<tmp.txt>$
- for /f %%a in ('find /c /v ""^<$') do set /a n=%%a-1
- for /f "skip=%n% delims=" %%a in ($) do echo %%a
- echo %t% %time%
- pause
复制代码 sort + set /p 耗时24.7s,内存占用中等- @echo off
- set t=%time%
- findstr "^Date\/Time:" tmp.txt | sort /+65535 /o tmp2.txt
- set /p DateTime=<tmp2.txt
- echo 取得的结果为 %DateTime%
- echo %t% %time%
- pause
复制代码 bat + js 耗时29.5s,内存占用高(约是文件体积的五倍)- @if(0)==(0) echo off
- set t=%time%
- cscript -nologo -e:jscript %0 <tmp.txt
- echo %t% %time%
- pause & exit/b
- @end
- var text = WScript.StdIn.ReadAll();
- var f = text.match(/Date\/Time:.*/ig);
- if (f) {WSH.Echo(f[f.length - 1])}
复制代码 纯 js 耗时65.26s,内存占用高(约是文件体积的两倍)- var start = new Date()
-
- var fso = new ActiveXObject('Scripting.FileSystemObject')
- var ts = fso.OpenTextFile('tmp.txt',1)
-
- var text = ts.ReadAll()
- var f = text.match(/Date\/Time:.*/ig);
- if (f) {WSH.Echo(f[f.length - 1])}
-
- var end = new Date()
- WSH.Echo((end - start)/1000)
复制代码 纯 vbs 耗时49.354s,内存占用高(约是文件体积的两倍,和 js 相当)- start = timer
-
- Set fso = CreateObject("Scripting.FileSystemObject")
- Set ts = fso.OpenTextFile("tmp.txt",1)
-
- text = ts.ReadAll
- i = instrRev(text,vbLf & "Date/Time:")
- text = mid(text,i)
- text = split(text,vbCrLf)(0)
-
- end2 = timer
-
- WSH.Echo(text & vbcrlf & (end2 - start))
复制代码 powershell io.file::ReadAllLines 耗时29.6s,内存占用高(约是文件体积的五倍)- $start = get-date
- $file = "C:\Users\Administrator\Desktop\tmp.txt"
- ([io.file]::ReadAllLines($file) -match "Date/Time:")[-1]
-
- $end = get-date
- $end - $start
复制代码 powershell select-string 耗时140.8s,内存占用高(约是文件体积的五倍)- $start = get-date
- $file = "C:\Users\Administrator\Desktop\tmp.txt"
- $a = select-string $file -pattern '^Date/Time:'
- $a[-1]
- $end = get-date
- $end - $start
复制代码 js while+Read 耗时22.675s,内存占用低,但本质是对最后 100000~200000 个字节进行搜索,所以...- var start = new Date()
-
- var fso = new ActiveXObject('Scripting.FileSystemObject')
- var ts = fso.OpenTextFile('tmp.txt',1)
- var arr = ['','']
- var i=0
-
- while(!ts.AtEndOfStream){
- arr[i=!i]=ts.Read(100000)
- }
-
- var f = (arr[!i]+arr[i]).match(/Date\/Time:.*/ig);
- var end = new Date()
- if (f) {WSH.Echo(f[f.length - 1])}
-
- WSH.Echo((end - start)/1000)
复制代码 改用 skip 实现只取最后的 10000 字节,耗时21.96s,内存占用低,缺点和前一个方法一样,为了效率,并非全文搜索- var start = new Date()
-
- var fso = new ActiveXObject('Scripting.FileSystemObject')
- var file = fso.GetFile('tmp.txt')
- var ts = file.OpenAsTextStream(1)
-
- var skip = file.Size-10000
- if(skip>0)ts.Skip(skip)
-
- var text = ts.ReadAll()
-
- var f = text.match(/Date\/Time:.*/ig);
- var end = new Date()
- if (f) {WSH.Echo(f[f.length - 1])}
-
- WSH.Echo((end - start)/1000)
复制代码
|