#### Archived

This topic is now archived and is closed to further replies.

This topic is 5152 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Just getting into game/graphics programming, and for one of my first projects I decided to create a "game" that just draws large prime numbers. it works fine at first, spinning very fast, but of course it takes longer and longer to calculate the next one and the frame rate drops and drops. is the correct solution to make my program have two threads, one for drawing and the other for calculating? i''m using directx9 but i can''t imagine that makes any difference. llloyd

##### Share on other sites
How exactly do you calculate the next number.

Say you have a loop or something, you can just pause the loop, draw your scene, resume the loop.

##### Share on other sites
numbers As New System.Collections.ArrayListSub main()    Dim i As Integer = 0    Dim numbers As New System.Collections.ArrayList    numbers.Add(2)    For i = 2 To 10        If (IsPrime(i)) Then            numbers.Add(i)        End If    NextEnd SubFunction IsPrime(ByVal number As Integer) As Boolean    Dim i As Integer    IsPrime = False    For Each i In numbers        If (number Mod i) = 0 Then            Exit Function        End If        If i >= System.Math.Sqrt(number) Then            Exit For        End If    Next    IsPrime = TrueEnd Function

how would you then determine how many loops to do before exiting? presumably 1 calc for 1 frame wouldnt be the most efficient...

llloyd

##### Share on other sites
PRECALCULATE THE SQUARE ROOT OF NUMBER. Every loop you''re taking the sqrt of it, which is an expensive operation indeed. Just calculate it when you run the function, save it to a variable, and check against that.

##### Share on other sites
His question seemed to be more in regards to if it would be proper to use seperate threads, and not whether is system of calculating the prime was proper.

##### Share on other sites
Regardless of his question, he should still avoid that mistake when programming. Thread or not, that should give a big speed boost with larger numbers.

##### Share on other sites
So where are you calling the drawing code from?

do something like this:

sub mainloop()   dim hist as integer   do while not closing      drawscene      hist = figureoutnextprime(hist, 20)   loopend subfunction figureoutnextprime(seed as integer, ms as long) as integer    dim start as long    dim i as integer    start = gettickcount()       for i = seed to collection.count - 1       ''your calculations       if gettickcount - start >= ms then          if i = collection.count - 1 then            figureoutnextprime = 0          else            figureoutnextprime = i+1          end if          exit function       end if    next i    figureoutnextprime = 0end function

something like that

##### Share on other sites
I don''t think there really is a solution to your problem. It''s difficult to calculate large prime numbers. So far as calculating the square root every time through the loop, a smart compiler should figure that out for you since number is a local variable and its value is never changed (i''m speaking for C/C++ here, other languages might not be as optimised). A multithreaded program could possibly speed things up to some extent. You are likely waiting for some system resources at some point in time during your drawing routine and you''d be able to hold the CPU for prime number calculating during that period if that section of your code was in a different thread. But you probably won''t make it much further before your program slows to a halt regardless.

##### Share on other sites
sorry if i wasnt more specific, i''m not actually calculating large prime numbers, but i do have a similiar expensive calculation.

sounds like i really should just try and break it down by some arbritrary number into steps and perform it a number at a timer, per frame. sounds reasonable enough, just have to play around and find out how many steps to perform at once by guesswork.. ?

llloyd

##### Share on other sites
Create threads that will calculate the prime numbers before you actually need them, and that way everything can continue along. Store them all in a table, and just keep precalculating.

The more you dig deeper, the slower the calculations will go. This is just that way things go. And not even having threads precalculating the result will help.

Of course, you could be tricky and have a crazy amount of prime numbers precalculated in a file. I think you might be able to download somewhere a very long list of all the prime numbers calculated up to this date, and just put them into a file.

That way, you won't even have to do any calculating at all. However, you WILL run out of prime numbers...but if you want to keep the program running and not just stop at the end of your list, just have the program go in reverse.

[edited by - Programmer One on April 12, 2004 1:32:03 AM]

• 10
• 18
• 14
• 18
• 15