Why does this code go so slowly?

Blitz3D Forums/Blitz3D Programming/Why does this code go so slowly?

Rottbott(Posted 2004) [#1]
This is really puzzling me. My timing code is left in. Mostly it outputs 0 or 1 for the time elapsed, but every now and then I get an 11 or 12, which seems WAY too long for this simple bit of code. Any suggestions?

MS = MilliSecs()

    ; Increase delay counters for all removed clients
    For i = RN_DelayLoop To RN_DelayLoop + 500
      If H\Clients[i] < 0 Then H\Clients[i] = H\Clients[i] + 1
    Next
    RN_DelayLoop = RN_DelayLoop + 501
    If RN_DelayLoop >= RN_MaxClientID
      RN_DelayLoop = 0
    ElseIf RN_DelayLoop > RN_MaxClientID - 500
      RN_DelayLoop = RN_MaxClientID - 500
    EndIf

MS = MilliSecs() - MS
If MS > 2 Then DebugLog MS


EDIT: Something else that's weird. As you can see, the code is some network code (from a new version of RottNet as a matter of fact). When a run a client and join the server, this same bit of code starts taking 200 milliseconds! That's really ridiculous. I can see why running another program at the same time will slow my server program down, but 200 milliseconds for that simple bit of code is far too long.


Damien Sturdy(Posted 2004) [#2]
can't see anything slow there- its something running outside of blitz, probably.....


Rottbott(Posted 2004) [#3]
I just edited with a new observation.

Also, when my client is running, the overall RottNet update on the server starts taking up to 4000 milliseconds! Something is badly wrong here. The client itself runs at 250 FPS no problem.

EDIT: I think it must be something to do with BlitzPlus being too nice about CPU time. (The server is B+, client B3D.) I noticed the client uses 98% CPU, and the server uses 0-2%.
Also it runs a lot faster with debug off - perhaps it was doing bounds checking on that Blitz array when debug was on? That might explain some of the slowness.


Damien Sturdy(Posted 2004) [#4]
For i = RN_DelayLoop To RN_DelayLoop + 500
      If H\Clients[i] < 0 Then H\Clients[i] = H\Clients[i] + 1
    Next


What sorta values does "rn_delayloop" reach?


Rottbott(Posted 2004) [#5]
9,500

Whatever it is though, the loop only iterates 500 times.


TomToad(Posted 2004) [#6]
Actually, it loops 501 times. it loops with i = RN_DelayLoop for the first loop, then i = RN_DelayLoop+1 for the second loop, i = RN_DelayLoop+2 for the third, etc... all the way to i = RN_DelayLoop+500 for the 501st loop.
Although I cannot see that as being the cause of the slowdown, unless you're accidentally writing to an extra byte that's out of range which I would think would be caught by the debugger.
If you want it to iterate exactly 500 times, the command should read For i = RN_DelayLoop to RN_DelayLoop + 499.