What's faster, the amount of code or how it executes?

  • elibol
  • Novice
  • Novice
  • elibol
  • Posts: 29

Post 3+ Months Ago

got a few questions before i get to the point->

does anyone know what kind of calculation goes into a logical operation when its executed?

is there a way to see the amount of calculation thats needed to execute a line of code?

or the amount of calculation thats needed to execute several lines of code?

is there a measuring method to give a line of code a value based on the amount of calculation it requires to complete its action? so i can compare smile.gif


now for what sparked my inquiries, i have two chunks of code and i think i know which one will executed faster but im still not sure.

this is the one that i think will execute quicker, but for this to happen i have to write about two times the amount of code. since i don't really understand how actionscript is compiled im unsure if the weight of the two loops will effect how quickly the code will be executed.

in this one there is logical operation to determine which loop to execute, the problem here is there is an extra logical operation and an expression and any other amount of calculation required to initiate a for loop:

Code: [ Select ]
var startVal=((amount/layers)*(j-1)==0)? 1:int((amount/layers)*(j-1))+1, end=(amount/layers)*j;
if(onoff){
for(;startVal<=end;startVal++){
currs[startVal]=((end*layerScale))*growthRatio;
targets[startVal]=currs[startVal]/growthRatio;
}
}else{
for(;startVal<=end;startVal++){
currs[startVal]=((end*layerScale));
targets[startVal]=currs[startVal]*growthRatio;
}
}
  1. var startVal=((amount/layers)*(j-1)==0)? 1:int((amount/layers)*(j-1))+1, end=(amount/layers)*j;
  2. if(onoff){
  3. for(;startVal<=end;startVal++){
  4. currs[startVal]=((end*layerScale))*growthRatio;
  5. targets[startVal]=currs[startVal]/growthRatio;
  6. }
  7. }else{
  8. for(;startVal<=end;startVal++){
  9. currs[startVal]=((end*layerScale));
  10. targets[startVal]=currs[startVal]*growthRatio;
  11. }
  12. }


this one i think will execute slower because in each iteration it has to execute an extra logical operation, but it gains from having one less loop in the code.

Code: [ Select ]
for(var startVal=((amount/layers)*(j-1)==0)? 1:int((amount/layers)*(j-1))+1, end=(amount/layers)*j;startVal<=end;startVal++){
if(onoff){
currs[startVal]=((end*layerScale))*growthRatio;
targets[startVal]=currs[startVal]/growthRatio;
}else{
currs[startVal]=((end*layerScale));
targets[startVal]=currs[startVal]*growthRatio;
}}
  1. for(var startVal=((amount/layers)*(j-1)==0)? 1:int((amount/layers)*(j-1))+1, end=(amount/layers)*j;startVal<=end;startVal++){
  2. if(onoff){
  3. currs[startVal]=((end*layerScale))*growthRatio;
  4. targets[startVal]=currs[startVal]/growthRatio;
  5. }else{
  6. currs[startVal]=((end*layerScale));
  7. targets[startVal]=currs[startVal]*growthRatio;
  8. }}



the first one ask's a question then loops, the second loops and ask's a question on each iteration but with the latter the total amount of written code is less.

thanks in advance for everyones help :)
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

I agree that the first will run faster however, would this not be faster ? (I might be missing somthing)
Code: [ Select ]
var startVal=((amount/layers)*(j-1)==0)? 0:int((amount/layers)*(j-1)), end=(amount/layers)*j;
var er =[end*layerScale, (end*layerScale)*growthRatio];
if(onoff){
for(;++startVal<=end;){
currs[startVal]=er[1];
targets[startVal]=er[0];
}
}else{
for(;++startVal<=end;){
currs[startVal]=er[0];
targets[startVal]=er[1];
}
}
  1. var startVal=((amount/layers)*(j-1)==0)? 0:int((amount/layers)*(j-1)), end=(amount/layers)*j;
  2. var er =[end*layerScale, (end*layerScale)*growthRatio];
  3. if(onoff){
  4. for(;++startVal<=end;){
  5. currs[startVal]=er[1];
  6. targets[startVal]=er[0];
  7. }
  8. }else{
  9. for(;++startVal<=end;){
  10. currs[startVal]=er[0];
  11. targets[startVal]=er[1];
  12. }
  13. }
  • elibol
  • Novice
  • Novice
  • elibol
  • Posts: 29

Post 3+ Months Ago

yes thats faster, well done mate.

i had a vague sense that it could be better cause of some of the same operations occuring twice, but it didnt ever click to use arrays :D

thanks man, i now know to use arrays for these kinds of math operations.

also i wasn't aware that you could use an array element that was defined before the position of the element your defining all on the same line!

two nifty little tricks.

do you have answers to my other questions by any chance?
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

Well, I guess you can't use the element before in the nexts equation :oops: I've edited my earlier post the array should be,
Code: [ Select ]
[end*layerScale, (end*layerScale)*growthRatio]

Also the first & last element were turning out to be the same so I also ditched the last element & changed the indexes in calling them.

Quote:
does anyone know what kind of calculation goes into a logical operation when its executed?

I have no idea.

Quote:
is there a way to see the amount of calculation thats needed to execute a line of code?
or the amount of calculation thats needed to execute several lines of code?

I the more equations, the longer it takes. The deeper thoose equations go, the longer it takes.
(1+n+(1+n)) VS (1+n+((1+n)+(1+n)))

Quote:
is there a measuring method to give a line of code a value based on the amount of calculation it requires to complete its action? so i can compare smile.gif

Count all of the * , / , + , - , & , << , >> , < , > , % , ^ , ! , = operators ? I really don't know :P

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 9 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.