Code tooooo slow

Just starting out? Need help? Post your questions and find answers here.
Megaborsti
User
User
Posts: 35
Joined: Sat Aug 16, 2003 4:52 pm
Location: Germany

Code tooooo slow

Post by Megaborsti »

Hi,
If tried to write the following code (don´t ask why):
I have an array of say 10 numbers.
I tried to multiply the numbers like this:

Code: Select all

erg(0) = array(0) * array(0)
erg(1) = array(0) * array(1)
erg(2) = array(0) * array(2)
....
erg(?) = array(1) * array(0)
erg(?) = array(1) * array(1)
Clear? Each element should get multiplied with each element...
Later, I have to do that with the same array much more. Each element of an array should be multiplied with each element of this array 10 times, hard to explain, should look like this:

Code: Select all

erg(0) = array(0) * array(0) * array(0) * ... * array(0)
erg(1) = array(0) * array(0) * array(0) * ... * array(1)
...
...
erg(?) = array(9) * array(9) * ... * array(8)
erg(?) = array(9) * array(9) * ... * array(9)
I thought it would do, if I used 10 for-loops like this:

Code: Select all

for a=0 to last
  for b=0 to last
    for c=0 to last 
...
But if I run the program, it does not work. If I try it with only 5 loops, it takes long, if I use 6 loops, it takes very much time, and with 10 loops, it does not work.

Does someone know any way to solve this problem?
I become better!!! :)
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1285
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Post by Paul »

Just curious what values you might be storing in Array() since you could quite easily cause an overflow doing such multiplications, giving you wrong resutls ;)

Wouldn't the highest value you could store in arg() be 2^32 ?
Image Image
Froggerprogger
Enthusiast
Enthusiast
Posts: 423
Joined: Fri Apr 25, 2003 5:22 pm
Contact:

Post by Froggerprogger »

Okay, perhaps you want to use the overflow for a kind of crypting ?

However, you have to calculate 10^10 = 10.000.000.000 multiplikations with 10 operands each, so 100.000.000.000 multiplikations total.

My computer needs for 1.000.000.000 multiplikations around 90 seconds, tested with the following code:

Code: Select all

start = GetTickCount_()
For i=0 To 1000000000
    a * b
Next
Debug GetTickCount_() - start
So it will last ~9000 seconds to calculate your code.
%1>>1+1*1/1-1!1|1&1<<$1=1
Megaborsti
User
User
Posts: 35
Joined: Sat Aug 16, 2003 4:52 pm
Location: Germany

Post by Megaborsti »

I dont think they get so high...
I use floats, which usually are between 0 and 2 or so.
If I multiply them only 10 times, they will hardly become 2^10, but never 2^32.
That was not my problem, i´m sure everything works fine, but these 10 for-loops with some if questions and multiplications need so much time that the program seems to stop, I have to STRG+ALT+ENTF it.
I wanted to know if there is a faster way, because it has to be possible to multiply some arrays, hasn´t it?
Thanks anyway.
I wait for other answers...
I become better!!! :)
jack
Addict
Addict
Posts: 1358
Joined: Fri Apr 25, 2003 11:10 pm

Post by jack »

do you by any chance run the program in debug-mode?
if so, turning debug off might speed things up. :)
Megaborsti
User
User
Posts: 35
Joined: Sat Aug 16, 2003 4:52 pm
Location: Germany

Post by Megaborsti »

Without Debug-Mode it really is faster, but it is still MUCH too slow.
It seems, that there is really no other way to solve my problem, I will try to do it another way.
If someone has a good idea, he nevertheless can post it.
I become better!!! :)
jack
Addict
Addict
Posts: 1358
Joined: Fri Apr 25, 2003 11:10 pm

Post by jack »

if you would post the code, i am confident that someone
would be able to help you solve the problem. 8)
Megaborsti
User
User
Posts: 35
Joined: Sat Aug 16, 2003 4:52 pm
Location: Germany

Post by Megaborsti »

Its all right now. :D
I multiply only the important array (they can be chosen)
so it takes only about 10-20s
IT WORKS :D :D :D :D
I become better!!! :)
Karbon
PureBasic Expert
PureBasic Expert
Posts: 2010
Joined: Mon Jun 02, 2003 1:42 am
Location: Ashland, KY
Contact:

Post by Karbon »

How many operations are we talking about here? What's the size of the array? 10-20 seconds for some multiplications seems *super* excessive - but without knowing exactly what you're doing and how you're doing it no one can say for sure...

If you can post the code maybe some can offer some faster alternatives to your current approach...

Good luck!!
-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
Megaborsti
User
User
Posts: 35
Joined: Sat Aug 16, 2003 4:52 pm
Location: Germany

Post by Megaborsti »

I cannot post the whole code here, because it is too long, but I will post the following lines. They should be enough to understand, what I wanted to do and how I made it.

The program is about the following:
I have an exchange rate table with currencies (sorry,its a google translation), and want to check all possible exchanges and give out the one, where the profit is the highest (bad english :? ).
That´s is what I have done, it is not commented yet and with this forum´s preview, it looks really difficult, but it should be possible to understand, I hope:
Say if you have a much better idea or if you want to have explained something... :wink:

Code: Select all

Dim highscore.f(10)
    Dim hsway.s(10)
    Dim newcomer.f(10)
    Dim count.b(10)
    
    For a=0 To 9
      highscore(a) = 0
      hsway(a) = ""
      newcomer(a) = 1
      count(a) = 0
    Next
    count(0) = 0
    
    Repeat
      
      If db(count(0))\links = startwert
        If db(count(0))\rechts = startwert
          newcomer(0) = db(count(0))\kurs
          If newcomer(0) > highscore(0)
            highscore(0) = newcomer(0)
            hsway(0) = db(count(0))\rechts
          EndIf
        EndIf
        
        If db(count(0))\rechts = db(count(1))\links
          If db(count(1))\rechts = startwert And maximal >= 1
            newcomer(1) = db(count(0))\kurs * db(count(1))\kurs
            If newcomer(1) > highscore(1)
              highscore(1) = newcomer(1)
              hsway(1) = db(count(0))\rechts+"|"+db(count(1))\rechts
            EndIf
          EndIf
          
          If db(count(1))\rechts = db(count(2))\links
            If db(count(2))\rechts = startwert And maximal >= 2
              newcomer(2) = db(count(0))\kurs * db(count(1))\kurs * db(count(2))\kurs
              If newcomer(2) > highscore(2)
                highscore(2) = newcomer(2)
                hsway(2) = db(count(0))\rechts+"|"+db(count(1))\rechts+"|"+db(count(2))\rechts
              EndIf
            EndIf
            
            If db(count(2))\rechts = db(count(3))\links
              If db(count(3))\rechts = startwert And maximal >= 3
                newcomer(3) = db(count(0))\kurs * db(count(1))\kurs * db(count(2))\kurs * db(count(3))\kurs
                If newcomer(3) > highscore(3)
                  highscore(3) = newcomer(3)
                  hsway(3) = db(count(0))\rechts+"|"+db(count(1))\rechts+"|"+db(count(2))\rechts+"|"+db(count(3))\rechts
                EndIf
              EndIf
              
              If db(count(3))\rechts = db(count(4))\links
                If db(count(4))\rechts = startwert And maximal >= 4
                  newcomer(4) = db(count(0))\kurs * db(count(1))\kurs * db(count(2))\kurs * db(count(3))\kurs * db(count(4))\kurs
                  If newcomer(4) > highscore(4)
                    highscore(4) = newcomer(4)
                    hsway(4) = db(count(0))\rechts+"|"+db(count(1))\rechts+"|"+db(count(2))\rechts+"|"+db(count(3))\rechts+"|"+db(count(4))\rechts
                  EndIf
                EndIf
                
                If db(count(4))\rechts = db(count(5))\links
                  If db(count(5))\rechts = startwert And maximal >= 5
                    newcomer(5) = db(count(0))\kurs * db(count(1))\kurs * db(count(2))\kurs * db(count(3))\kurs * db(count(4))\kurs * db(count(5))\kurs
                    If newcomer(5) > highscore(5)
                      highscore(5) = newcomer(5)
                      hsway(5) = db(count(0))\rechts+"|"+db(count(1))\rechts+"|"+db(count(2))\rechts+"|"+db(count(3))\rechts+"|"+db(count(4))\rechts+"|"+db(count(5))\rechts
                    EndIf
                  EndIf
                  
                  If db(count(5))\rechts = db(count(6))\links
                    If db(count(6))\rechts = startwert And maximal >= 6
                      newcomer(6) = db(count(0))\kurs * db(count(1))\kurs * db(count(2))\kurs * db(count(3))\kurs * db(count(4))\kurs * db(count(5))\kurs * db(count(6))\kurs
                      If newcomer(6) > highscore(6)
                        highscore(6) = newcomer(6)
                        hsway(6) = db(count(0))\rechts+"|"+db(count(1))\rechts+"|"+db(count(2))\rechts+"|"+db(count(3))\rechts+"|"+db(count(4))\rechts+"|"+db(count(5))\rechts+"|"+db(count(6))\rechts
                      EndIf
                    EndIf
                    
                    If db(count(6))\rechts = db(count(7))\links
                      If db(count(7))\rechts = startwert And maximal >= 7
                        newcomer(7) = db(count(0))\kurs * db(count(1))\kurs * db(count(2))\kurs * db(count(3))\kurs * db(count(4))\kurs * db(count(5))\kurs * db(count(6))\kurs * db(count(7))\kurs
                        If newcomer(7) > highscore(7)
                          highscore(7) = newcomer(7)
                          hsway(7) = db(count(0))\rechts+"|"+db(count(1))\rechts+"|"+db(count(2))\rechts+"|"+db(count(3))\rechts+"|"+db(count(4))\rechts+"|"+db(count(5))\rechts+"|"+db(count(6))\rechts+"|"+db(count(7))\rechts
                        EndIf
                      EndIf
                      
                      If db(count(7))\rechts = db(count(8))\links
                        If db(count(8))\rechts = startwert And maximal >= 8
                          newcomer(8) = db(count(0))\kurs * db(count(1))\kurs * db(count(2))\kurs * db(count(3))\kurs * db(count(4))\kurs * db(count(5))\kurs * db(count(6))\kurs * db(count(7))\kurs * db(count(8))\kurs
                          If newcomer(8) > highscore(8)
                            highscore(8) = newcomer(8)
                            hsway(8) = db(count(0))\rechts+"|"+db(count(1))\rechts+"|"+db(count(2))\rechts+"|"+db(count(3))\rechts+"|"+db(count(4))\rechts+"|"+db(count(5))\rechts+"|"+db(count(6))\rechts+"|"+db(count(7))\rechts+"|"+db(count(8))\rechts
                          EndIf
                        EndIf
                        
                        If db(count(8))\rechts = db(count(9))\links
                          If db(count(9))\rechts = startwert And maximal >= 9
                            newcomer(9) = db(count(0))\kurs * db(count(1))\kurs * db(count(2))\kurs * db(count(3))\kurs * db(count(4))\kurs * db(count(5))\kurs * db(count(6))\kurs * db(count(7))\kurs * db(count(8))\kurs * db(count(9))\kurs
                            If newcomer(9) > highscore(9)
                              highscore(9) = newcomer(9)
                              hsway(9) = db(count(0))\rechts+"|"+db(count(1))\rechts+"|"+db(count(2))\rechts+"|"+db(count(3))\rechts+"|"+db(count(4))\rechts+"|"+db(count(5))\rechts+"|"+db(count(6))\rechts+"|"+db(count(7))\rechts+"|"+db(count(8))\rechts+"|"+db(count(9))\rechts
                            EndIf
                          EndIf
                          
                        EndIf
                      EndIf
                    EndIf
                  EndIf
                EndIf
              EndIf
            EndIf
          EndIf
        EndIf  
      EndIf
       
      count(0)+1
      If count(0)=count : count(0)=0 : count(1)+1 : EndIf
      If count(1)=count : count(1)=0 : count(2)+1 : EndIf
      If count(2)=count : count(2)=0 : count(3)+1 : EndIf
      If count(3)=count : count(3)=0 : count(4)+1 : EndIf
      If count(4)=count : count(4)=0 : count(5)+1 : EndIf
      If count(5)=count : count(5)=0 : count(6)+1 : EndIf
      If count(6)=count : count(6)=0 : count(7)+1 : EndIf
      If count(7)=count : count(7)=0 : count(8)+1 : EndIf 
      If count(8)=count : count(8)=0 : count(9)+1 : EndIf
      
      If maximal > 0
        SetGadgetState(#pbar,count(maximal)*count+count(maximal-1))
      EndIf
       
    Until (count(maximal)=0 And count(maximal+1)=1) Or count(9)=count
I become better!!! :)
Post Reply