# calculation becoming slow-delay accumulation

Page 1 of 1

## 14 Replies - 1344 Views - Last Post: 30 April 2012 - 06:28 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=276613&amp;s=52a5c559de34aa084895d0242cb3d3fb&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ajmalm543

Reputation: 0
• Posts: 7
• Joined: 24-April 12

# calculation becoming slow-delay accumulation

Posted 24 April 2012 - 11:29 AM

```Private Function calculate()

cx = (X - x1) * xratio
cy = (Y - y1) * yratio

If cx < 0 Then
xd = 0
cx = (0 - cx)
Else
xd = 1
End If

If cy < 0 Then
yd = 2
cy = (0 - cy)
Else
yd = 3
End If

cx = CInt(cx)
cy = CInt(cy)

If cx = 0 Then
xr = 0
yr = 1
ElseIf cy = 0 Then
yr = 0
xr = 1
Else

crflag = 1

If cx < cy Then
cr = cx / cy
Else
cr = cy / cx
End If

For cri = 1 To range
crtemp = (cr * cri)
crtemp = CInt(crtemp) - crtemp
crtemp = Format(crtemp, "####0.00000")
If crtemp < 0 Then
crtemp = 0 - crtemp
End If
If crflag = 1 Then
crlow = crtemp
crlowi = cri
crflag = 0
Else
If crtemp < crlow Then
crlow = crtemp
crlowi = cri
End If
End If
Next cri

If cx < cy Then
xr = crlowi
yr = CInt(crlowi / cr)
Else
yr = crlowi
xr = CInt(crlowi / cr)
End If

For cri = range To 1 Step -1
If (xr Mod cri) = 0 And (yr Mod cri) = 0 Then
xr = xr / cri
yr = yr / cri
End If
Next cri

tempr = xr
xr = yr
yr = tempr

End If

xr1 = xr Mod 10
xr = (xr - xr1) / 10
xr2 = xr Mod 10
xr = (xr - xr2) / 10
xr3 = xr Mod 10
xr = (xr - xr3) / 10
xr4 = xr Mod 10

yr1 = yr Mod 10
yr = (yr - yr1) / 10
yr2 = yr Mod 10
yr = (yr - yr2) / 10
yr3 = yr Mod 10
yr = (yr - yr3) / 10
yr4 = yr Mod 10

cx1 = cx Mod 10
cx = (cx - cx1) / 10
cx2 = cx Mod 10
cx = (cx - cx2) / 10
cx3 = cx Mod 10

cy1 = cy Mod 10
cy = (cy - cy1) / 10
cy2 = cy Mod 10
cy = (cy - cy2) / 10
cy3 = cy Mod 10
cy = (cy - cy3) / 10
cy4 = cy Mod 10

End Function
```

i am using this function in my program..its being called repeatedly(more than 200-300 times)..initially,it takes no delay for execution..bt as it proceeds, the time taken for executing the function is becoming higher..this slows down my software..how can i solve it???

This post has been edited by BobRodes: 24 April 2012 - 11:41 AM
Reason for edit:: code tags

Is This A Good Question/Topic? 0

## Replies To: calculation becoming slow-delay accumulation

### #2 BobRodes

• Lovable Curmudgeon

Reputation: 600
• Posts: 3,071
• Joined: 19-May 09

## Re: calculation becoming slow-delay accumulation

Posted 24 April 2012 - 11:50 AM

What makes you think that the problem is in the function itself? The function doesn't declare any of the variables that it uses, so it's almost certain that the reason that your calculate function gets slower and slower is because the values in those variables get bigger and bigger. Of course, you could be implicitly declaring all those variables, but we have no way of knowing that because you won't tell us.

Suppose, for an extreme example, that you multiplied your range variable by 2 each time you called your calculate function. The 300th time around, your range value would be 2^300 times what it was when you started. You'd be old and gray before that finished!

This post has been edited by BobRodes: 24 April 2012 - 11:52 AM

### #3 ajmalm543

Reputation: 0
• Posts: 7
• Joined: 24-April 12

## Re: calculation becoming slow-delay accumulation

Posted 25 April 2012 - 09:47 AM

iam sure the problem is with this function itself..i checked the timing for all other sections and that works fine..
all the variables used in this function doesnot store previous values and the values are changed every time the function is called..den wat could be the reason???

### #4 maj3091

• D.I.C Lover

Reputation: 330
• Posts: 1,997
• Joined: 26-March 09

## Re: calculation becoming slow-delay accumulation

Posted 25 April 2012 - 12:56 PM

Explain how the range variable is calculated before calling the function.

As Bob has already pointed out, your function could be relatively simple, but if you've got it looping an almost endless number of times, it will get slow!

### #5 BobRodes

• Lovable Curmudgeon

Reputation: 600
• Posts: 3,071
• Joined: 19-May 09

## Re: calculation becoming slow-delay accumulation

Posted 25 April 2012 - 02:22 PM

ajmalm543, on 25 April 2012 - 05:47 PM, said:

iam sure the problem is with this function itself..i checked the timing for all other sections and that works fine..
all the variables used in this function doesnot store previous values and the values are changed every time the function is called..den wat could be the reason???

Well, so long as you're sure that I'm wrong, your problem is solved. Oh, it isn't? My sympathies.

If you should become unsure at some future point, then post back for more help.

This post has been edited by BobRodes: 25 April 2012 - 02:27 PM

### #6 ajmalm543

Reputation: 0
• Posts: 7
• Joined: 24-April 12

## Re: calculation becoming slow-delay accumulation

Posted 25 April 2012 - 03:11 PM

Thats not the problem..the range has a fixed value 10...i dont think the problem is with calculations..will explain the reason..forget what we discussed till now..i hav changed the program..nw it looks like..

```i = 1
cdraw = 0

while cdraw <= clines

cdraw = cdraw + 1

MSComm1.Output = "s"

pd = Val(Mid(newline, i, 1))
i = i + 1
MSComm1.Output = Chr\$(pd)

xd = Val(Mid(newline, i, 1))
i = i + 1
MSComm1.Output = Chr\$(xd)

yd = Val(Mid(newline, i, 1))
i = i + 1
MSComm1.Output = Chr\$(yd)

xr1 = Val(Mid(newline, i, 1))
i = i + 1
MSComm1.Output = Chr\$(xr1)

xr2 = Val(Mid(newline, i, 1))
i = i + 1
MSComm1.Output = Chr\$(xr2)

xr3 = Val(Mid(newline, i, 1))
i = i + 1
MSComm1.Output = Chr\$(xr3)

xr4 = Val(Mid(newline, i, 1))
i = i + 1
MSComm1.Output = Chr\$(xr4)

yr1 = Val(Mid(newline, i, 1))
i = i + 1
MSComm1.Output = Chr\$(yr1)

yr2 = Val(Mid(newline, i, 1))
i = i + 1
MSComm1.Output = Chr\$(yr2)

yr3 = Val(Mid(newline, i, 1))
i = i + 1
MSComm1.Output = Chr\$(yr3)

yr4 = Val(Mid(newline, i, 1))
i = i + 1
MSComm1.Output = Chr\$(yr4)

cx1 = Val(Mid(newline, i, 1))
i = i + 1
MSComm1.Output = Chr\$(cx1)

cx2 = Val(Mid(newline, i, 1))
i = i + 1
MSComm1.Output = Chr\$(cx2)

cx3 = Val(Mid(newline, i, 1))
i = i + 1
MSComm1.Output = Chr\$(cx3)

cy1 = Val(Mid(newline, i, 1))
i = i + 1
MSComm1.Output = Chr\$(cy1)

cy2 = Val(Mid(newline, i, 1))
i = i + 1
MSComm1.Output = Chr\$(cy2)

cy3 = Val(Mid(newline, i, 1))
i = i + 1
MSComm1.Output = Chr\$(cy3)

cy4 = Val(Mid(newline, i, 1))
i = i + 1
MSComm1.Output = Chr\$(cy4)

'waits till process is complete
Do Until recv = "c"
DoEvents
DoEvents
Loop

Wend

```

ill explain the whole thing..the program sends a packet of data(each packet contains 19 bytes namely s,pd,xd,yd,xr1,xr2....yr4) to serial port and waits for an input "c" from the serial port..if c is recieved.it sends the next packet of data.the 'recv' variable stores the character recieved in the mscomm_oncomm event.'clines' is the total number of packets..the string 'newline' contains the data for all the packets sequentially..there can be upto 300-400 packets and hence the string newline may contain 300*18 characters(numbers).
now executing this program..i faced the same delay problem.at first it took no time sending each packet but it slows down as the packet number increases.(the delay becomes higher as packet nmbr reaches 200 and more.).is the problem continous execution of a same loop..?

### #7 BobRodes

• Lovable Curmudgeon

Reputation: 600
• Posts: 3,071
• Joined: 19-May 09

## Re: calculation becoming slow-delay accumulation

Posted 25 April 2012 - 08:29 PM

Could be, don't have quite enough information to tell. Have you read this doc? You should be able to send newline as a string rather than stripping off one character at a time and sending it that way. While I don't have a ready answer for you, I would first check the buffer size. You might not be clearing the buffer between calls. There are some properties in the control that you can use to check this.

### #8 ajmalm543

Reputation: 0
• Posts: 7
• Joined: 24-April 12

## Re: calculation becoming slow-delay accumulation

Posted 26 April 2012 - 12:10 AM

i further debugged the code and found out one thing.the accumulating delay is created by the section which waits for 'c'..ie even after c is recieved it takes some more time to execute the next working of loop..can i substitute doevents with something else??my aim is just to wait till 'c' is recieved serially.

### #9 maj3091

• D.I.C Lover

Reputation: 330
• Posts: 1,997
• Joined: 26-March 09

## Re: calculation becoming slow-delay accumulation

Posted 26 April 2012 - 04:36 AM

This might be a silly question/point, but as I don't see it in your code, I thought I'd ask....

You are removing the packets from NewLine and decrementing the Cline counter once you have sent a packet aren't you?

I'm reading your description in that NewLine should work like a FIFO buffer, with Cline holding the current number of packets in there?

If you not removing packets, then you'll be sending the same data multiple times, which would slow things down as the buffer size would only increase.

### #10 BobRodes

• Lovable Curmudgeon

Reputation: 600
• Posts: 3,071
• Joined: 19-May 09

## Re: calculation becoming slow-delay accumulation

Posted 26 April 2012 - 08:13 AM

ajmalm543, on 26 April 2012 - 08:10 AM, said:

i further debugged the code and found out one thing.the accumulating delay is created by the section which waits for 'c'..ie even after c is recieved it takes some more time to execute the next working of loop..can i substitute doevents with something else??my aim is just to wait till 'c' is recieved serially.

Again, you need to check the buffers. Again, reading the doc will give you ideas about how to do that. Please stop explaining your reasons for doing what you're doing, and asking irrelevant questions (no you can't substitute doevents with something else), and follow through on the suggestions given before asking for more help. Alternatively, do it your way if that's working for you.

This post has been edited by BobRodes: 26 April 2012 - 08:16 AM

### #11 ajmalm543

Reputation: 0
• Posts: 7
• Joined: 24-April 12

## Re: calculation becoming slow-delay accumulation

Posted 26 April 2012 - 10:42 AM

k..thanx for ur suggestions..bt i solved my problem in another way..i changed my algorithm so that i could do my work without using doevents and that worked fine without making delays..

### #12 maj3091

• D.I.C Lover

Reputation: 330
• Posts: 1,997
• Joined: 26-March 09

## Re: calculation becoming slow-delay accumulation

Posted 26 April 2012 - 10:53 AM

Perhaps you'd like to explain in a little more detail or post your code showing what you did to fix it for the benefit of others in the future.

I for one am mildly curious how changing your algorithm fixed this problem.

This post has been edited by maj3091: 26 April 2012 - 10:53 AM

### #13 BobRodes

• Lovable Curmudgeon

Reputation: 600
• Posts: 3,071
• Joined: 19-May 09

## Re: calculation becoming slow-delay accumulation

Posted 26 April 2012 - 01:41 PM

me too.

### #14 ajmalm543

Reputation: 0
• Posts: 7
• Joined: 24-April 12

## Re: calculation becoming slow-delay accumulation

Posted 29 April 2012 - 07:22 AM

here is wat i did.

http://www.vbforums....416#post4164416

### #15 BobRodes

• Lovable Curmudgeon

Reputation: 600
• Posts: 3,071
• Joined: 19-May 09

## Re: calculation becoming slow-delay accumulation

Posted 30 April 2012 - 06:28 AM