【问题标题】:Boost up the Batch Processing speed提高批处理速度
【发布时间】:2014-01-30 04:50:56
【问题描述】:

我有一个日志文件Source.txt,其内容结构如下:

Handle v3.51
Copyright (C) 1997-2013 Mark Russinovich
Sysinternals - www.sysinternals.com

------------------------------------------------------------------------------
DataSource.exe pid: 5860 USER\DEVELOPMENT
  5EC: File  (R--)   G:\apps\OracleClient\product\11.1.0\client_2\RDBMS\mesg\ocius.msb
  600: File  (R--)   G:\apps\OracleClient\product\11.1.0\client_2\RDBMS\mesg\ocius.msb
  614: File  (R--)   G:\apps\OracleClient\product\11.1.0\client_2\RDBMS\mesg\ocius.msb
  628: File  (R--)   G:\apps\OracleClient\product\11.1.0\client_2\RDBMS\mesg\ocius.msb
  834: File  (RW-)   G:\apps\Oracle\Middleware\user_projects\epmsystem8\products\FinancialManagement\Server Working Folder\APP1_RulesLogFiles\1-28-2014_Timer.log
  838: File  (RW-)   G:\apps\Oracle\Middleware\user_projects\epmsystem8\products\FinancialManagement\Server Working Folder\APP1_RulesLogFiles\1-28-2014_Debug.log
  854: File  (RW-)   G:\apps\Oracle\Middleware\user_projects\epmsystem8\products\FinancialManagement\Server Working Folder\APP1_RulesLogFiles\1-28-2014_Timer.log
  858: File  (RW-)   G:\apps\Oracle\Middleware\user_projects\epmsystem8\products\FinancialManagement\Server Working Folder\APP1_RulesLogFiles\1-28-2014_Debug.log
  874: File  (RW-)   G:\apps\Oracle\Middleware\user_projects\epmsystem8\products\FinancialManagement\Server Working Folder\APP1_RulesLogFiles\1-28-2014_Timer.log
  878: File  (RW-)   G:\apps\Oracle\Middleware\user_projects\epmsystem8\products\FinancialManagement\Server Working Folder\APP1_RulesLogFiles\1-28-2014_Debug.log
 1058: File  (RW-)   C:\Windows\winsxs\amd64_microsoft.vc80.atl_1fc8b3b9a1e18e3b_8.0.50727.4053_none_8a1a02152edb659b

------------------------------------------------------------------------------
DataSource.exe pid: 10568 USER\DEVELOPMENT
 1074: File  (RW-)   C:\Windows\winsxs\amd64_microsoft.vc80.crt_1fc8b3b9a1e18e3b_8.0.50727.4053_none_88e046c92fae6f57
 1078: File  (RW-)   C:\Windows\winsxs\amd64_microsoft.vc80.atl_1fc8b3b9a1e18e3b_8.0.50727.4053_none_8a1a02152edb659b
  8F4: File  (RW-)   C:\Windows\winsxs\amd64_microsoft.vc80.crt_1fc8b3b9a1e18e3b_8.0.50727.4053_none_88e046c92fae6f57
  908: File  (RW-)   G:\apps\Oracle\Middleware\user_projects\epmsystem8\products\FinancialManagement\Server Working Folder\APP2_RulesLogFiles\1-28-2014_Timer.log
  90C: File  (RW-)   G:\apps\Oracle\Middleware\user_projects\epmsystem8\products\FinancialManagement\Server Working Folder\APP2_RulesLogFiles\1-28-2014_Debug.log
  928: File  (RW-)   G:\apps\Oracle\Middleware\user_projects\epmsystem8\products\FinancialManagement\Server Working Folder\APP2_RulesLogFiles\1-28-2014_Timer.log
  92C: File  (RW-)   G:\apps\Oracle\Middleware\user_projects\epmsystem8\products\FinancialManagement\Server Working Folder\APP2_RulesLogFiles\1-28-2014_Debug.log
  948: File  (RW-)   G:\apps\Oracle\Middleware\user_projects\epmsystem8\products\FinancialManagement\Server Working Folder\APP2_RulesLogFiles\1-28-2014_Timer.log
1064: File  (RW-)   C:\Windows\winsxs\amd64_microsoft.vc80.crt_1fc8b3b9a1e18e3b_8.0.50727.4053_none_88e046c92fae6f57
 1068: File  (RW-)   C:\Windows\winsxs\amd64_microsoft.vc80.atl_1fc8b3b9a1e18e3b_8.0.50727.4053_none_8a1a02152edb659b
  | |
  | |
  | |
and so on......

我想实现以下目标:

  1. 搜索第一次出现的“DataSource.exe”,然后搜索 对于(APP1 APP2 APP3 APP4 APP5 APP6 APP7 APP8 APP9) 中任何一个单词的第一次出现,我们假设找到了 APP1 单词 将以下输出发送到文件“Output.txt”:

    DataSource.exe pid: 5860 USER\DEVELOPMENT  APP1
    
  2. 然后搜索“DataSource.exe”的第二次出现,然后 再次搜索(APP1 APP2 APP3 APP4 APP5 APP6 APP7 APP8 APP9)中任何一个单词的第一次出现[我们可以丢弃已经找到的单词],让我们假设 APP2 单词 找到然后将以下输出附加到同一文件“Output.txt”:

    DataSource.exe pid: 10568 USER\DEVELOPMENT  APP2
    

    等等..

我正在使用以下工作脚本:

@ECHO OFF
setlocal enabledelayedexpansion
SET "keystring1="
(
 FOR /f "delims=" %%a IN (
  Source.txt
  ) DO (
  ECHO %%a|FIND "DataSource.exe" >NUL
  IF NOT ERRORLEVEL 1 SET keystring1=%%a
  FOR %%b IN (APP1 APP2 APP3 APP4 APP5 APP6 APP7 APP8 APP9 ) DO (
   ECHO %%a|FIND "%%b" >NUL
   IF NOT ERRORLEVEL 1 IF DEFINED keystring1 CALL ECHO(%%keystring1%% %%b&SET "keystring1="

  )))>Output.txt

GOTO :EOF

问题:

我在这里遇到的问题是 Source.txt file of size 312 KB ,即使只有这 9 个应用程序,这个脚本也会占用 4 minutes to produce Output.txt ,这似乎是相当长的时间。

无论如何我想减少这个时间,而不使用任何额外的批处理文件。请问有什么解决方法吗?

*具体应用名称与APP1、APP2...等不同。

【问题讨论】:

  • 不是搜索进程然后确定它打开了哪些文件,而是枚举文件 (dir) 并为每个文件确定锁定它的进程 (handle file)

标签: windows string performance batch-file for-loop


【解决方案1】:

您的二次搜索可能需要一些时间 - 正在检查 %%aAPP1.. APP2.. etc. 您可以创建第二个批处理文件以使用不同的参数调用此文件,并行运行它们。

如果您的脚本名为 script.bat,请创建第二个执行以下操作的脚本 -

START script.bat APP1 APP2 APP3
START script.bat APP4 APP5 APP6
START script.bat APP7 APP8 APP9

然后更改您的脚本以包含输入参数。

@ECHO OFF
setlocal enabledelayedexpansion
SET "searchvals=%*"
SET "keystring1="
(
    FOR /f "delims=" %%a IN (Source.txt) DO (
        ECHO %%a|FIND "DataSource.exe" >NUL
        IF NOT ERRORLEVEL 1 SET keystring1=%%a
        FOR %%b IN (%searchvals%) DO (
            ECHO %%a|FIND "%%b" >NUL
            IF NOT ERRORLEVEL 1 IF DEFINED keystring1 CALL ECHO(%%keystring1%% %%b&SET "keystring1="
)))>Output.txt

EXIT

这样,脚本的每个实例仅将 Source.txt 的每一行与三个字符串进行比较,而不是 9 个。

希望这确实有帮助。请注意,Output.txt 的内容可能不会按顺序排列。

【讨论】:

  • @unclement..这是一个非常好的解决方案,但不幸的是,我被限制创建任何其他批处理文件。我只需要在一个批处理脚本中完成这些事情。
【解决方案2】:

这使用了一个名为 findrepl.bat 的辅助批处理文件 - 从以下位置下载:https://www.dropbox.com/s/rfdldmcb6vwi9xc/findrepl.bat

findrepl.bat 放在与批处理文件相同的文件夹中或路径上。

@echo off
for %%a in (APP1 APP2 APP3 APP4 APP5 APP6 APP7 APP8 APP9 ) do (
type "source.txt" |findrepl "DataSource.exe" /e:"^-----" /b:"%%a" |findrepl /o:1:1  2>nul
)
pause

【讨论】:

  • @foxi..完美的帮助文件就是..真的..!但我不允许使用任何其他批处理文件。我只需要在一个批处理文件中完成这些事情......有什么帮助吗?
  • 有兴趣的是,Sunny,您从这个解决方案中获得了哪些速度提升?
【解决方案3】:
@echo off
setlocal EnableDelayedExpansion

(for /F "tokens=1-5* delims=\ " %%a in ('findstr "DataSource.exe APP" Source.txt') do (
   if "%%a" equ "DataSource.exe" (
      set pid=%%a %%b %%c %%d\%%e
      set "APP="
   ) else if not defined APP (
      set "APP=%%f"
      set "APP=!APP:*APP=!"
      echo !pid!  APP!APP:~0,1!
   )
)) > Output.txt

goto :EOF

【讨论】:

  • @Aacini..你也会同意我的观点,我们必须更加概括,因为我对提高速度的主要关注与将来可以轻松添加更多不同名称的应用程序有关。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-10-18
  • 2017-07-25
  • 2020-10-19
  • 2022-01-20
  • 2013-10-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多