# Need Help .. I made slow Time Calculation Function

## 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

##### 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)

Local \$aTimes = StringSplit(\$s, ":")

Return StringFormat("%2.2i:%2.2i:%2.2i", \$aTimes[1], \$aTimes[2], \$aTimes[3])

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
##### 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
##### 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.

##### 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

## Create an account

Register a new account

• ### Recently Browsing   0 members

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
×
• Create New...