10 Replies Latest reply on May 9, 2009 12:19 AM by kglad

    huge calculations

    nullbyte

      I have a Flash program that does a set of arithmetic calculations 100,000 times. It takes 14 hours to complete all of the calculations. I wrote an identical program in C. It finishes the calculations in 4 minutes, which is over 200 times faster than the ActionScript version. However, the C program has some incorrect results caused by differences between ActionScript and C's decimal precision. I need to run the calculations at a speed close to C but I need all of the results to be identical to the calculations of a Flash program.

       

      Can you compile ActionScript code in a way that optimizes it for running pure arithmetic calculations? Or is there a way to convert ActionScript code to a faster language like C without getting differences in precision?

       

      Thanks.

        • 1. Re: huge calculations
          kglad Adobe Community Professional & MVP

          100,000 arithmetic calculations in as2 should take less than 1 second.  show your code.

          • 2. Re: huge calculations
            nullbyte Level 1

            100,000 SETS of calculations. Each set has about 100,000 calculations itself.

            • 3. Re: huge calculations
              kglad Adobe Community Professional & MVP

              then as2 is not a good option.

               

              you can figure it will take on the order of 1 second for every 10**5 to 10**7 operations in a for-loop or about 10**3 to 10**5 seconds for 10**10 operations.

               

              with as3, you can expect that time will decrease by a factor of 1000 and, of course, that is feasible.

              • 4. Re: huge calculations
                Rothrock Level 5

                Generally Flash is known for slow calculations. So it would not be a good choice for anything intensive. As long as you are using Flash CS4 and publishing to Flash 10 you are probably getting the most speed out of flash that your algorithm can handle.

                 

                Also Flash isn't really all that precise. There are lots of posts here about "errant" results due to Flash's 15 (or is it 16?) digit precision. The real problem (I think, and I'm sure kglad will correct me if I'm wrong) is that given a finite set of binary bits to represent any given real number there will be "errors" in the conversion.

                 

                I don't know much about C (are you using straight out C or some variant?) but I would imagine that it has at least the same accuracy as Flash and most likely there are libraries for doing even higher precision calculations. Generally the precision is controlled by the processor type not the language. And there are a whole set of tools for fixing the calculations as they go.

                 

                All that being said there are many tricks in Flash that can speed up intensive operations, but not generally from 14 hours to 4 minutes!

                 

                Things like

                 

                square=x*x

                 

                instead of

                 

                square = Math.pow(x,2);

                 

                And so on. But without know exactly what you are trying to do it is hard to make suggestions.

                1 person found this helpful
                • 5. Re: huge calculations
                  nullbyte Level 1

                  Thanks for your replies!

                   

                  with as3, you can expect that time will decrease by a factor of 1000 and, of course, that is feasible.

                  Are you saying that as3 is 1000 times faster than as2?

                   

                  Generally the precision is controlled by the processor type not the language.

                  Does that mean that an online Flash puzzle game that depends on decimal precision could have different solutions on different computers?

                  • 6. Re: huge calculations
                    kglad Adobe Community Professional & MVP

                    1.  as3 is about 1000 times faster for arithmetic operations.

                     

                    2.  the processor's arithmetic capacity/floating point instructions and whatever else are a limiting factor for any software's accuracy.

                     

                    but flash is more limiting than anyone's processor.  (well, there's probably someone still using an 8-bit processor but i don't think it would be reasonable to account code for that.)

                    • 7. Re: huge calculations
                      nullbyte Level 1

                      Is there any way to simulate exactly how Flash limits precision in a faster language? Could I make a C program where all decimal values are exactly the same as in Flash?

                      • 8. Re: huge calculations
                        kglad Adobe Community Professional & MVP

                        flash isn't limiting percision.  it has all the accuracy of every other application that uses floating point arithmetic and 16 decimal place accuracy on your computer.  you can get better accuracy with some applications (if they support double floating point arithmetic).

                         

                        but each application has it's own algorithms for arithmetic operations and i don't see how it would be worthwhile to try and emulate the way one app handles arithmetic, using another app.

                         

                        in addition, all apps will (or should) give the same answer -  within their decimal accuracy.

                         

                        specifically, 1.01-1 in flash is likely to be different than in c.  they'll both be .01 to within 15 or 16 decimal places, but they might not look similar.  for example, both the following are reasonable returns, that you might see from flash or c or any other app, from that arithmetic operation:

                         

                        .009999999999999

                         

                        .010000000000001

                         

                        bottomline:   there's no way to gurantee you can get exactly the same results in flash as in c.  in fact, you can be sure the results will not be the same.

                        • 9. Re: huge calculations
                          nullbyte Level 1

                          I'm using Math.floor, and .999999999999999 floors to 0. I think I've fixed it by adding .000001 to all numbers.

                          • 10. Re: huge calculations
                            kglad Adobe Community Professional & MVP

                            if you're problem is solved, then you're set.

                             

                            but it's unusual to add a fudge factor (like .00001) to obtain a result.  i've never encountered a situation where that was needed.

                             

                            using Math.floor, Math.ceil and/or Math.round plus some standard techniques to obtain accuracy to some number of decimal places has always been satisfactory.

                             

                            so, for example, if you wanted an answer rounded to the nearest 1/1000th, you would use:

                             

                            answer = Math.round(intermediateAnswer*1000)/1000;

                             

                            and more generally,

                             

                            answer = Math.round(intermediateAnswer*Math.pow(10,n))/Math.pow(10,n);

                             

                            would give an answer rounded to the nearest 1/Math.pow(10,n)

                            1 person found this helpful