|
dragonamd
中级用户
  
积分 209
发帖 100
注册 2008-8-1
状态 离线
|
『楼 主』:
高手们帮忙优化一下代码
使用 LLM 解释/回答一下
R02 u70.843 i-19.91 5-10.17 I5.641
U71.796 B-18.487 5-8.11 I4.556
G75.13 H-10.666 5-9.16 I-9.209
F75.13 5-7.27 H-10.666 I-9.209
上面的这段中,5-以后的10.17数字最大了,就显示5-10.17
@echo off&setlocal enabledelayedexpansion
for /f "delims=" %%a in (a.txt) do (
for %%i in (%%a) do (
set "var=%%i"
if "!var:~,1!" equ "5" (
set "var= !var!"
set "_!var:~-10!=%%i"
)
)
)
for /f "tokens=2 delims==" %%i in ('set _') do set "max=%%i"
echo %max%
pause
代码是可以用的,。。如果在10MB的文本中,。执行效率慢的要死,,差不多要半个小时了,.VBS也行,总之能快就行
Last edited by dragonamd on 2008-9-12 at 05:10 AM ]
R02 u70.843 i-19.91 5-10.17 I5.641
U71.796 B-18.487 5-8.11 I4.556
G75.13 H-10.666 5-9.16 I-9.209
F75.13 5-7.27 H-10.666 I-9.209
Among the above, the number after 5- is 10.17 which is the largest, so display 5-10.17
@echo off&setlocal enabledelayedexpansion
for /f "delims=" %%a in (a.txt) do (
for %%i in (%%a) do (
set "var=%%i"
if "!var:~,1!" equ "5" (
set "var= !var!"
set "_!var:~-10!=%%i"
)
)
)
for /f "tokens=2 delims==" %%i in ('set _') do set "max=%%i"
echo %max%
pause
The code works. However, if it's executed on a 10MB text, the execution efficiency is extremely slow, taking almost half an hour. VBS is also okay, as long as it's faster.
Last edited by dragonamd on 2008-9-12 at 05:10 AM ]
|
|
2008-9-12 04:51 |
|
|
bat-zw
金牌会员
      永远的学习者
积分 3105
发帖 1276
注册 2008-3-8
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
代码的效率问题是出在set _上,修改如下:
@echo off&setlocal enabledelayedexpansion
for /f "delims=" %%a in (a.txt) do (
for %%i in (%%a) do (
set "var=%%i"
if "!var:~,2!" equ "5-" (
if not defined max set "max=!var:~2!"
if !var:~2! geq !max! set "max=!var:~2!"
)
)
)
echo %max%&pause>nul
The efficiency problem of the code lies in set _. The modification is as follows:
@echo off&setlocal enabledelayedexpansion
for /f "delims=" %%a in (a.txt) do (
for %%i in (%%a) do (
set "var=%%i"
if "!var:~,2!" equ "5-" (
if not defined max set "max=!var:~2!"
if !var:~2! geq !max! set "max=!var:~2!"
)
)
)
echo %max%&pause>nul
|

批处理之家新域名:www.bathome.net |
|
2008-9-12 11:16 |
|
|
bat-zw
金牌会员
      永远的学习者
积分 3105
发帖 1276
注册 2008-3-8
状态 离线
|
『第 3 楼』:
我晕,上面的没考虑浮点比较,修改如下
使用 LLM 解释/回答一下
@echo off&setlocal enabledelayedexpansion
for /f "delims=" %%a in (a.txt) do (
for %%b in (%%a) do (
set "var=%%b"
if "!var:~,2!" equ "5-" (
if not defined max set "max=!var:~2!"
for /f "tokens=1* delims=." %%c in ("!max!") do set "max1=%%c"&set "max2=%%d"
for /f "tokens=1* delims=." %%e in ("!var:~2!") do (
if %%e gtr !max1! (
set "max=!var:~2!"
) else (
if %%e equ !max1! if %%d gtr !max2! set "max=!var:~2!"
)
)
)
)
)
echo %max%&pause>nul
```
@echo off&setlocal enabledelayedexpansion
for /f "delims=" %%a in (a.txt) do (
for %%b in (%%a) do (
set "var=%%b"
if "!var:~,2!" equ "5-" (
if not defined max set "max=!var:~2!"
for /f "tokens=1* delims=." %%c in ("!max!") do set "max1=%%c"&set "max2=%%d"
for /f "tokens=1* delims=." %%e in ("!var:~2!") do (
if %%e gtr !max1! (
set "max=!var:~2!"
) else (
if %%e equ !max1! if %%d gtr !max2! set "max=!var:~2!"
)
)
)
)
)
echo %max%&pause>nul
```
|

批处理之家新域名:www.bathome.net |
|
2008-9-12 11:39 |
|
|
huahua0919
银牌会员
    
积分 1608
发帖 780
注册 2007-10-7
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
借用S11S1代码写个vbs
set fso=createobject("scripting.filesystemobject")
set f=fso.opentextfile("a.txt",1)
while f.atendofstream <> true
r=r&split(split(f.readline,"5-")(1))(0)&" " '提取所有5-后的数字
wend
r=split(r) '转换为数组
n=ubound(r) '确定数组最大下标
for i=0 to n-1 '转换数值类型
r(i)=cdbl(r(i))
next
quicksort r,0,n
msgbox r(n-1)
'快速排序:
'划分:
function partition(r,l,h)
dim i,j,t
i=l
j=h
t=r(i)'初始化,t为基准
do
while r(j)>=t and i<j
j=j-1'从右向左扫描,查找第1个小于t的数
wend
if i<j then
r(i)=r(j)'交换r(i)和r(j)
i=i+1
end if
while r(i)<=t and i<j
i=i+1'从左向右扫描,查找第1个大于t的数
wend
if i<j then
r(j)=r(i)'交换r(i)和r(j)
j=j-1
end if
loop while i<>j
r(i)=t'基准t已被最后定位
partition=i
end function
'排序:
sub quicksort(r,s1,t1)
dim i
if s1<t1 then'只有一个数或无数时无须排序
i=partition(r,s1,t1)'对r(s1)到r(t1)做划分
quicksort r,s1,i-1'递归处理左区间
quicksort r,i+1,t1'递归处理右区间
end if
end sub
Last edited by huahua0919 on 2008-9-12 at 01:06 PM ]
Borrow the S11S1 code to write a VBS
set fso=createobject("scripting.filesystemobject")
set f=fso.opentextfile("a.txt",1)
while f.atendofstream <> true
r=r&split(split(f.readline,"5-")(1))(0)&" " 'Extract all numbers after 5-
wend
r=split(r) 'Convert to array
n=ubound(r) 'Determine the maximum subscript of the array
for i=0 to n-1 'Convert to numeric type
r(i)=cdbl(r(i))
next
quicksort r,0,n
msgbox r(n-1)
'Quick sort:
'Division:
function partition(r,l,h)
dim i,j,t
i=l
j=h
t=r(i)'Initialize, t is the reference
do
while r(j)>=t and i<j
j=j-1'Scan from right to left, find the first number less than t
wend
if i<j then
r(i)=r(j)'Swap r(i) and r(j)
i=i+1
end if
while r(i)<=t and i<j
i=i+1'Scan from left to right, find the first number greater than t
wend
if i<j then
r(j)=r(i)'Swap r(i) and r(j)
j=j-1
end if
loop while i<>j
r(i)=t'Reference t has been finally positioned
partition=i
end function
'Sort:
sub quicksort(r,s1,t1)
dim i
if s1<t1 then'No need to sort when there is one number or no number
i=partition(r,s1,t1)'Divide r(s1) to r(t1)
quicksort r,s1,i-1'Recursively process the left interval
quicksort r,i+1,t1'Recursively process the right interval
end if
end sub
Last edited by huahua0919 on 2008-9-12 at 01:06 PM ]
|
|
2008-9-12 12:54 |
|
|
dragonamd
中级用户
  
积分 209
发帖 100
注册 2008-8-1
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
代码都测试了,。不会加快 10MB的文本还是一样慢
The code has all been tested. Still, it doesn't speed up. The 10MB text is still as slow.
|
|
2008-9-12 15:27 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
|
2008-9-12 15:58 |
|
|
dragonamd
中级用户
  
积分 209
发帖 100
注册 2008-8-1
状态 离线
|
|
2008-9-13 02:37 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
Originally posted by dragonamd at 2008-9-13 02:37:
不会写,
不会写可以自己学习一下嘛。
gawk "{for (i=1;i<=NF;i++) { if($i~/^5-/){ split($i,a,/-/); if(a>max) max=a; }}} END{print max}" your_file
Last edited by lxmxn on 2008-9-13 at 12:09 PM ]
Originally posted by dragonamd at 2008-9-13 02:37:
Can't write,
You can learn by yourself if you can't write.
gawk "{for (i=1;i<=NF;i++) { if($i~/^5-/){ split($i,a,/-/); if(a>max) max=a; }}} END{print max}" your_file
Last edited by lxmxn on 2008-9-13 at 12:09 PM ]
|
|
2008-9-13 12:07 |
|
|
dragonamd
中级用户
  
积分 209
发帖 100
注册 2008-8-1
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
怎么运行了是没用的,实在不懂
It doesn't work after running. I really don't understand.
|
|
2008-9-13 12:17 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
gawk 是一个工具,需要下载,网上搜搜吧。
我这里运行正常。
ps 建议看看置顶的批处理新手FAQ。
gawk is a tool that needs to be downloaded. Just search online.
It runs normally here.
ps It is suggested to take a look at the top-sticked batch processing novice FAQ.
|
|
2008-9-13 12:26 |
|
|
dragonamd
中级用户
  
积分 209
发帖 100
注册 2008-8-1
状态 离线
|
|
2008-9-14 00:07 |
|
|
HAT
版主
       
积分 9023
发帖 5017
注册 2007-5-31
状态 离线
|
『第 12 楼』:
使用 LLM 解释/回答一下
可以批处理,但是根据你的要求,批处理效率不如gawk高。
It can be batch processed, but according to your requirements, the efficiency of batch processing is not as high as that of gawk.
|

 |
|
2008-9-14 02:18 |
|
|
dragonamd
中级用户
  
积分 209
发帖 100
注册 2008-8-1
状态 离线
|
『第 13 楼』:
使用 LLM 解释/回答一下
不知道那个工具怎么安装,使用
I don't know how to install and use that tool
|
|
2008-9-14 19:48 |
|
|
HAT
版主
       
积分 9023
发帖 5017
注册 2007-5-31
状态 离线
|
『第 14 楼』:
使用 LLM 解释/回答一下
不用安装,直接下在gawk.exe即可直接在批处理中调用,不会使用可以看帮助。
No need to install, just download gawk.exe and you can call it directly in batch processing. If you don't know how to use it, you can view the help.
|

 |
|
2008-9-14 19:50 |
|
|
dragonamd
中级用户
  
积分 209
发帖 100
注册 2008-8-1
状态 离线
|
『第 15 楼』:
使用 LLM 解释/回答一下
在网上找了很久下载不到,。能提供下载一下吗?
I've been searching online for a long time and can't download it. Can you provide a download?
|
|
2008-9-14 21:23 |
|