Jump to content
Sign in to follow this  
2tim3_16

Script hogging CPU

Recommended Posts

2tim3_16

Hey guys,

I've got a script I wrote to run backups on our computers, but it uses 80-90% CPU on single-core processors. Needless to say, people complain about that. (Guess that's the problem with scripting on a dual-core and running the scripts on a single-core. :) ) Anyway, I've searched the forums to see if I could find anyone with similar scripts having the same problem, and all I'm seeing is forgotten Sleeps in loops. I didn't forget any Sleeps, but I'd prefer not having to add any more time. $delay is currently 20 milliseconds. Anyone know of any other way to speed this up without adding a longer delay? If I make the delay too long, it runs for hours on some people's computers.

Do I just have too much going on? (BTW, thanks to GEOSoft for the Access ADO functions!)

Func _CheckTimeStamp($vPath)
    GUICtrlSetData($GUI_Label, "Checking files in folder '" & $vPath & "'")
    $Debug_Text = "Preparing file list..." & @CRLF & $Debug_Text
    TraySetToolTip("LAM Backup Script" & @CRLF & "Preparing file list...")
    GUICtrlSetData($GUI_Debug_Text, $Debug_Text)
    Local $File_List
    Local $x
    Local $y
    Local $z
    If FileExists($vPath) Then

        $Omit_List = _ArrayCreate("")
        
        If FileExists($Custom_Exclude) Then
            $Custom_Omit_List = ""
            If _FileReadToArray($Custom_Exclude, $Custom_Omit_List) = 1 Then
                For $i = 1 To UBound($Custom_Omit_List) - 1
                    _ArrayAdd($Omit_List, $Custom_Omit_List[$i])
                Next
            EndIf
        EndIf
        
        If FileExists($Default_Exclude) Then
            $Default_Omit_List = ""
            If _FileReadToArray($Default_Exclude, $Default_Omit_List) = 1 Then
                For $i = 1 To UBound($Default_Omit_List) - 1
                    _ArrayAdd($Omit_List, $Default_Omit_List[$i])
                Next
            EndIf
        EndIf
        
        FileDelete(@TempDir & "\tmpOUTPUT.txt")
        
        _FileWriteFromArray(@ScriptDir & "\omit_list.txt", $Omit_List)
        $find = '| findstr /l /i /v /g:"' & @ScriptDir & "\omit_list.txt" & '" '

        RunWait(@ComSpec & ' /c dir /b /s /a-d /o:e "' & $vPath & '" ' & $find & '> "' & @TempDir & '\tmpOUTPUT.txt"', @WorkingDir, @SW_HIDE)
        _FileReadToArray(@TempDir & "\tmpOUTPUT.txt", $File_List)
        FileDelete(@TempDir & "\tmpOUTPUT.txt")
        FileDelete(@ScriptDir & "\omit_list.txt")

        _ArrayDelete($File_List, UBound($File_List) - 1)

        $x = 0
        
        For $i = 1 To UBound($File_List) - 1
            $strSplitPath = _PathSplit($File_List[$i], $szDrive, $szDir, $szFName, $szExt)
            GUICtrlSetData($GUI_Label, "Checking files in folder '" & $szDrive & $szDir & "'")

            $Debug_Text = "Checking file '" & $szFName & $szExt & "'" & @CRLF & $Debug_Text
            GUICtrlSetData($GUI_Debug_Text, $Debug_Text)
            $strFileName = StringReplace($File_List[$i], "'", Chr(31))
            $strFileName = StringReplace($strFileName, "[", Chr(254))
            $strFileName = StringReplace($strFileName, "]", Chr(222))
            $rData = _adoQueryLike ($tbl_Files, 'File_Name', $strFileName, 1)
            If @error = 3 Then
                $rData = _ArrayCreate('1', $strFileName, FileGetTime($File_List[$i], 0, 1), _Now())
                _adoAddRecord ($tbl_Files, $rData, 0)
                $rData = _ArrayCreate('1', $strFileName)
                _adoAddRecord ($tbl_Backup, $rData, 0)
            Else
                If IsArray($rData) Then
                    $rData = StringSplit($rData[1], Chr(28))
                    If @error Then
                        _FileWriteLog($Error_Log, _Now() & " - StringSplit error occured while processing '" & $File_List[$i] & "'.")
                    Else
                        If $rData[2] = FileGetTime($File_List[$i], 0, 1) Then
                            _adoUpdateRecord ($tbl_Files, 'File_Name', $strFileName, 'Last_Checked', _Now())
                        Else
                            $rData = _ArrayCreate('1', $strFileName)
                            _adoUpdateRecord ($tbl_Files, 'File_Name', $strFileName, 'Time_Stamp', FileGetTime($File_List[$i], 0, 1))
                            _adoUpdateRecord ($tbl_Files, 'File_Name', $strFileName, 'Last_Checked', _Now())
                            _adoAddRecord ($tbl_Backup, $rData, 0)
                        EndIf
                    EndIf
                EndIf
            EndIf

            $rData = ""

            $x = $x + 1
            $y = UBound($File_List) - 1
            $z = Round($x / $y * 100, 0)
            TraySetToolTip("LAM Backup Script" & @CRLF & _
                    "Searching '" & $vPath & "'" & @CRLF & _
                    "Total files checked: " & $x & @CRLF & _
                    "Percent complete: " & $z & "%")
            GUICtrlSetData($GUI_Progress, $z)
            Sleep($delay)
        Next
    EndIf
    
    TraySetToolTip("LAM Backup Script")
    GUICtrlSetData($GUI_Progress, 0)
EndFunc ;==>_CheckTimeStamp
Edited by 2tim3_16

Share this post


Link to post
Share on other sites
2tim3_16

Use a sleep()

Already am. That's why I asked if there is anything else.

Share this post


Link to post
Share on other sites
narayanjr

I don't know if it will work, ive never done it or had to do it but I would imagine if you set the priority of the program lower then normal it wouldn't take as much of the cpu, but then again now that i think about it that would probably also make it take longer but I really don't know was just a thought.

Share this post


Link to post
Share on other sites
2tim3_16

I don't know if it will work, ive never done it or had to do it but I would imagine if you set the priority of the program lower then normal it wouldn't take as much of the cpu, but then again now that i think about it that would probably also make it take longer but I really don't know was just a thought.

Just gave that a try, but there doesn't seem to be any noticeable difference, even going from High to Low priority. But even so, I think the same effect could probably be accomplished by increasing the length of the Sleep(), which I'm hoping not to do. We've thought about doing a re-write in C# or C++, but I don't know enough to do so. I've only had a basic intro to C++.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×