Jump to content

Need Help .. I made slow Time Calculation Function


Guest
 Share

Recommended Posts

Hello,

I created a function that knows how to add time xx: xx: xx seconds ..

For example - I want to add another 8282 seconds for:

00:01:17

Then the function know the result - 02:19:19 (hopefully correct)

Is a function I wrote:

Func TimeCalculation()
$Ts = $Ts+$Sfix
        If $Ts >= 60 Then
        $Tm = $Tm+1    
            $m = 0
            While 1
            $m = $m+1
                $Ts = $Ts-60
                
                If $Ts >= 60 Then
                    $Tm = $Tm+1
                Else
                    ExitLoop
                EndIf
            WEnd
        EndIf
        If $Tm >= 60 Then
            $Th = $Th+1
            For $h = 1 To 60
                $Tm = $Tm-60
                If $Tm >= 60 Then
                    $Th = $Th+1
                Else
                    ExitLoop
                EndIf
            Next
        EndIf    
If $Ts < 10 And StringLen($Ts) < 2 Then $Ts = "0"&$Ts
If $Tm < 10 And StringLen($Tm) < 2 Then $Tm = "0"&$Tm
If $Th < 10 And StringLen($Th) < 2 Then $Th = "0"&$Th

Return $Th
Return $Tm
Return $Ts
EndFunc

Before using an important function set $Sfix ( = number of seconds), $Ts ( = hh:mm:ss) , $Tm ( = hh:mm:ss) and $Th ( = hh:mm:ss)

If I run the function on large file (big loop), then it seems that it takes some time .. I do not know why.

What did I do wrong that makes it work slower?

Edited by Guest
Link to comment
Share on other sites

What's wrong with _DateAdd?

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

Seems that someone wasn't paying attention in their maths class?

I'm feeling generous, so here is a very simple function, without any error checking, that uses a far simpler approach:

ConsoleWrite(_TimeAdd("00:01:17", 8282) & @LF)

Func _TimeAdd($s, $add)
    Local $aTimes = StringSplit($s, ":")

    $aTimes[1] += Floor($add / 3600)
    $add = Mod($add, 3600)

    $aTimes[2] += Floor($add / 60)
    $add = Mod($add, 60)

    $aTimes[3] += $add

    Return StringFormat("%2.2i:%2.2i:%2.2i", $aTimes[1], $aTimes[2], $aTimes[3])
EndFunc   ;==>_TimeAdd

Edit: Just read your function. There are more flaws than just logical ones... Can I recommend reading up on function parameters and also use of the return statement? Those two things would really help your function in the first post.

Edited by Mat
Link to comment
Share on other sites

Ok thanks ..

I did not know there was something ready in the help file.

So what do you mean?

There is one logic flaw?

Edited by Guest
Link to comment
Share on other sites

Couple of quick pointers. Firstly, take a look at this code:

ConsoleWrite(MyFunction() & @LF)

Func MyFunction()
    Return 1
    Return 2
    Return 3
EndFunc

Return immediately exits the function, and returns that value, so the above will always print "1", and the second and third return statements will never be executed. You probably wanted to do:

return $Th & ":" & $Tm & ":" & $Ts

This line:

If $Ts < 10 And StringLen($Ts) < 2 Then

Since we are only working with positive integers, you do not need both the "Ts < 10" and "StringLen(Ts) < 2" as they are both saying the same thing. Since you are using AND they will both be executed when they are both true.

Last point is purely convention rather than an actual flaw. You are using global variables to pass the data around. This makes your function very difficult to copy and use straight away. It also makes it difficult to use for anything other than its original purpose. If I change it to the following then it works exactly as expected:

Func TimeCalculation($Th, $Tm, $Ts, $Sfix)
    $Ts = $Ts + $Sfix

    If $Ts >= 60 Then
        $Tm = $Tm + 1
        $m = 0
        While 1
            $m = $m + 1
            $Ts = $Ts - 60

            If $Ts >= 60 Then
                $Tm = $Tm + 1
            Else
                ExitLoop
            EndIf
        WEnd
    EndIf

    If $Tm >= 60 Then
        $Th = $Th + 1
        For $h = 1 To 60
            $Tm = $Tm - 60
            If $Tm >= 60 Then
                $Th = $Th + 1
            Else
                ExitLoop
            EndIf
        Next
    EndIf

    If $Ts < 10 And StringLen($Ts) < 2 Then $Ts = "0" & $Ts
    If $Tm < 10 And StringLen($Tm) < 2 Then $Tm = "0" & $Tm
    If $Th < 10 And StringLen($Th) < 2 Then $Th = "0" & $Th

    Return $Th & ":" & $Tm & ":" & $Ts
EndFunc   ;==>TimeCalculation

Apart from those points, everything worked as it should and it got the right answer. I guess since you were reading the globals rather than the return value from the function you didn't notice the most serious problem.

Link to comment
Share on other sites

Ok it relaxes because I thought my logic fails with the calculations ..

So if it works correctly then the logic is correct.

Last point is purely convention rather than an actual flaw. You are using global variables to pass the data around. This makes your function very difficult to copy and use straight away. It also makes it difficult to use for anything other than its original purpose. If I change it to the following then it works exactly as expected:

I thought about it but I wrote it for something specific ..

Return immediately exits the function, and returns that value, so the above will always print "1", and the second and third return statements will never be executed. You probably wanted to do:

I thought to write just like your example. But I wanted to return each variable separately to save process (stringsplit() ) in another process (not in the function)

But I did not notice that it exits the function ..

I know this case .. But the in the test I did, i did not see a problem.

Edited by Guest
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...