Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Program Speeding Up
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
gitoxa




PostPosted: Sun Jun 01, 2008 11:21 pm   Post subject: Program Speeding Up

When the amount of balls is increased, for some reason it speeds up, and I can't quite figure it out.

Just edit this to test it.
code:
const NUMBALLS := <number>


Also, if you have any suggestions on the Get_Velocity procedure, you can say them too. Yay laziness!



Physics_Thingy.t
 Description:
IT HAS PHYSICS IN IT!

Download
 Filename:  Physics_Thingy.t
 Filesize:  5.54 KB
 Downloaded:  84 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
gitoxa




PostPosted: Fri Jun 06, 2008 9:05 pm   Post subject: RE:Program Speeding Up

Any ideas, anyone? Still haven't figured out the speed problem.
DemonWasp




PostPosted: Thu Jun 12, 2008 8:22 pm   Post subject: RE:Program Speeding Up

The problem lies in your main loop (right at the bottom of your program). You have the following:

Turing:

loop
    for Ball : 1 .. NUMBALLS
        Draw_Borders (BORTHICK, BORCOLOUR)
        Draw_Circles (CircleX, CircleY, RADIUS, CIRCLECOLOUR)
        New_Vel_And_Position (CircleX, CircleY, VelUp, VelSide)
        Check_Boundries (CircleX, CircleY, VelUp, VelSide)

        if CircleY (Ball) = BORTHICK + RADIUS and VelSide (Ball) = 0 then
            if Motionless (Ball) > 0 then %Been motionless for more than one iteration in a row
                Motionless (Ball) := 2
            else
                Motionless (Ball) := 1 %This is to set to check to see if next time it's still on the ground
            end if
        else
            Motionless (Ball) := 0 %Means it's not on the ground
        end if
    end for

    if Check_Still (Motionless) then
        Get_Velocity (CircleX, CircleY, VelSide, VelUp)
    end if

    View.Update
    delay (22)
    cls
end loop


But go look closely at each of Draw_Borders, Draw_Circles, New_Vel_and_Position, and Check_Boundaries. Each of those methods does everything in a for Ball: 1 to NUMBALLS loop.

So each time you went through the loop-endloop pictured, you were doing NUMBALLS iterations of the draw, draw, new velocities/positions, and check boundries methods - so the larger number of balls means your program does more between delays (so we see it moving faster).

To solve the problem, just move the method calls outside the for loop, as follows:

Turing:

loop
    Draw_Borders (BORTHICK, BORCOLOUR)
    Draw_Circles (CircleX, CircleY, RADIUS, CIRCLECOLOUR)
    New_Vel_And_Position (CircleX, CircleY, VelUp, VelSide)
    Check_Boundries (CircleX, CircleY, VelUp, VelSide)

    for Ball : 1 .. NUMBALLS
        if CircleY (Ball) = BORTHICK + RADIUS and VelSide (Ball) = 0 then
            if Motionless (Ball) > 0 then %Been motionless for more than one iteration in a row
                Motionless (Ball) := 2
            else
                Motionless (Ball) := 1 %This is to set to check to see if next time it's still on the ground
            end if
        else
            Motionless (Ball) := 0 %Means it's not on the ground
        end if
    end for

    if Check_Still (Motionless) then
        Get_Velocity (CircleX, CircleY, VelSide, VelUp)
    end if

    View.Update
    delay (22)
    cls
end loop


I've checked, and that seems to solve the problem completely. Cool program, by the way.

Nitpick: it's "Boundaries". Yes, I'm a spelling meanie.
riveryu




PostPosted: Thu Jun 12, 2008 8:27 pm   Post subject: Re: Program Speeding Up

If you are worrying anything to be too fast I think you can use Time.DelaySinceLast (time in ms), it makes sure the loop runs at the same speed no matter how fast the computer is. Fast machines will spend little time in the loop and longer waiting to return from Time.DelaySinceLast. Slower machines will take longer to execute the loop and will consequently wait less time before returning from Time.DelaySinceLast. F10 Turing help is actually helpful.
DemonWasp




PostPosted: Thu Jun 12, 2008 9:24 pm   Post subject: RE:Program Speeding Up

That's a cool system call, but it actually wouldn't have solved this problem. As per my explanation above, he was actually running the physics simulation code NUMBALLS times for every delay (or Time.DelaySinceLast() ). It may have made the timing more consistent, but it wouldn't have solved the problem.
gitoxa




PostPosted: Fri Jun 13, 2008 9:14 pm   Post subject: RE:Program Speeding Up

Thanks a lot DemonWasp. +Karma
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: