Forum Topic: I need super efficient code AS3

(344 views • 19 replies)

This topic is 1 page long.

<< < > >>
None

ThePeasant

Reply To Post Reply & Quote

Posted at: 6/24/08 07:31 PM

ThePeasant NEUTRAL LEVEL 08

Sign-Up: 08/30/07

Posts: 447

ok, here's the deal: I've made a Spiral Generator program, found here: http://spamtheweb.com/ul/upload/250608/8 479_Spiral.php . I have a loop that draws a new spiral about 100 times every second. This means I need the loop to run as efficiently as possible. Please, if you can, tell me how I can make the following code more efficient.

Also, a few questions:
1) do comments increase processing time?
2) do variable sizes increase processing time?
3) does calling constants (ie. Math.PI) take more to process than if I were to write '3.14159'?

function tTick(e:TimerEvent):void
{
	for (l=0;l<Speed;l++) //the variable 'Speed' is the number of pixels drawn every cycle
	{
		r+=VrP; // increase the radius value
		i+=ViP; // increase the angle value
		if (i>=360){ //return the angle value to 0; keep numbers low
			i-=360;
		}
		if (r>=Size){ //revert the radius to starting value once it has generated up to its size
			r=Sr;
		}
		//Adjust the colour value to make a gradient
		DCol.greenOffset += (TCol[Tj].greenOffset - TCol[Ti].greenOffset)/Tl;
		DCol.blueOffset += (TCol[Tj].blueOffset - TCol[Ti].blueOffset)/Tl;
		DCol.redOffset += (TCol[Tj].redOffset - TCol[Ti].redOffset)/Tl;
		Tk++;//counts along with the gradient
		if (Tk>=Tl){//once the next colour in the cycle is reached, cycles the colours used in the calculation
			Ti++;
			Tj++;
			Tk=0;
			if (Ti>=Tu){//when it reaches the 'colours used' variable, returns to the first colour in the cycle
				Ti=0;
			}
			if (Tj>=Tu){
				Tj=0;
			}
		}
		art.setPixel(j+r*Math.sin(i*Math.PI/180),k+r*Math.cos(i*Math.PI/180),DCol.color);//draw the pixel
	}
	back.bitmapData = art; //display the 'frame'
}

Thanks a bunch!


None

Coronus42

Reply To Post Reply & Quote

Posted at: 6/24/08 07:45 PM

Coronus42 NEUTRAL LEVEL 07

Sign-Up: 05/04/08

Posts: 10

At 6/24/08 07:31 PM, ThePeasant wrote: ok, here's the deal: I've made a Spiral Generator program, found here: http://spamtheweb.com/ul/upload/250608/8 479_Spiral.php . I have a loop that draws a new spiral about 100 times every second. This means I need the loop to run as efficiently as possible. Please, if you can, tell me how I can make the following code more efficient.

Also, a few questions:
1) do comments increase processing time?
2) do variable sizes increase processing time?
3) does calling constants (ie. Math.PI) take more to process than if I were to write '3.14159'?

1) Comments are not compiled, so they don't affect processing time or filesize after compiling.
2) For what you're doing, variable sizes will eventually affect processing of something like rotation, not for a long while, though, so I wouldn't worry about it.
3) A constant would probably be better since it's already fixed in memory and if it's in a loop, it doesn't need to allocate a new space for a new Number/int/uint.

As far as the framerate goes, are you sure you need 100 fps? I mean, the human eye stops being able to discern the difference between framerates the higher you go. Movies go at around 24 fps, most Flash I do I've found only needs to be 30 fps, but just for kicks I usually try it 60 fps for games, to see if it makes a difference. I only left it that way once.


None

ThePeasant

Reply To Post Reply & Quote

Posted at: 6/24/08 08:04 PM

ThePeasant NEUTRAL LEVEL 08

Sign-Up: 08/30/07

Posts: 447

yeah alright, i dropped the framerate as you suggested. Also renamed all the variables to be single letters (where possible). Might be more worthwhile just to store everything in an array... would that make things more or less efficient?


None

zuperxtreme

Reply To Post Reply & Quote

Posted at: 6/24/08 08:07 PM

zuperxtreme NEUTRAL LEVEL 08

Sign-Up: 01/02/05

Posts: 1,598

You should declare your variables beforehand and assign them a type.

For example:

instead of i=0
do:

var i:Number = 0;


None

GuyWithHisComp

Reply To Post Reply & Quote

Posted at: 6/24/08 08:22 PM

GuyWithHisComp LIGHT LEVEL 27

Sign-Up: 11/10/05

Posts: 4,008

Yes, make the variable local by using var variable:Number
Changing the variable name doesn't help performance at all.
You could keep difficult math (like 180/Math.PI) as a variable outside of the event.
Be sure that you only use Number when you're dealing with floats and int/uint if you're not.
You only seem to use the i variable once and there you're converting it to radians from degrees. You don't think you can have the i already be in radians?
A bitmap's bitmapData is a reference to a BitmapData. So you don't have to re-reference the bitmapdata again, even if you change pixel-colors in it.
++ is apparently a little slower, in ActionScript, than += 1.

BBS Signature

None

ThePeasant

Reply To Post Reply & Quote

Posted at: 6/24/08 08:26 PM

ThePeasant NEUTRAL LEVEL 08

Sign-Up: 08/30/07

Posts: 447

thank you for that. In AS3 assigning variables is mandatory and I have that done already. I just copy&pasted the section of code which is the actual loop because that's what is in need of efficiency.


None

EvilEgg

Reply To Post Reply & Quote

Posted at: 6/24/08 08:29 PM

EvilEgg EVIL LEVEL 17

Sign-Up: 12/05/06

Posts: 750

can we see the updated product then? The first was pretty neat lookin.


None

ThePeasant

Reply To Post Reply & Quote

Posted at: 6/24/08 08:30 PM

ThePeasant NEUTRAL LEVEL 08

Sign-Up: 08/30/07

Posts: 447

At 6/24/08 08:22 PM, GuyWithHisComp wrote: Yes, make the variable local by using var variable:Number
Changing the variable name doesn't help performance at all.

aww... ok... do you know if storing in Arrays does or not?

You could keep difficult math (like 180/Math.PI) as a variable outside of the event.

good idea! I'll get right on it

Be sure that you only use Number when you're dealing with floats and int/uint if you're not.

yup, i got that already sorted out, thx.

You only seem to use the i variable once and there you're converting it to radians from degrees. You don't think you can have the i already be in radians?

hmm... you mean like, convert it beforehand? Maybe... that WOULD save time. I'll test it out, thx.

A bitmap's bitmapData is a reference to a BitmapData. So you don't have to re-reference the bitmapdata again, even if you change pixel-colors in it.

oh... why did I think I did... alright, thanks again

++ is apparently a little slower, in ActionScript, than += 1.

thx, i'll change that up


None

LeechmasterB

Reply To Post Reply & Quote

Posted at: 6/24/08 10:13 PM

LeechmasterB EVIL LEVEL 16

Sign-Up: 04/01/05

Posts: 937

If you want the thing to be more efficient, why don't you just generate the spiral once and do the rotation thingy by copying it using a rotation matrix instead of redrawing several times per second?


None

ThePeasant

Reply To Post Reply & Quote

Posted at: 6/24/08 10:16 PM

ThePeasant NEUTRAL LEVEL 08

Sign-Up: 08/30/07

Posts: 447

because it's not simply a matter of rotation. There's also a cool colour transform effect that can take place on the spirals.


None

ThePeasant

Reply To Post Reply & Quote

Posted at: 6/24/08 11:02 PM

ThePeasant NEUTRAL LEVEL 08

Sign-Up: 08/30/07

Posts: 447

yay! All done! Well, for now...

http://www.newgrounds.com/portal/view/44 5953

my first submission! I hope people like it. Please go check it out and tell me what you think!


Happy

zuperxtreme

Reply To Post Reply & Quote

Posted at: 6/24/08 11:14 PM

zuperxtreme NEUTRAL LEVEL 08

Sign-Up: 01/02/05

Posts: 1,598

Not bad, but you could have taken some time and polished the menu, man. :)

Other than that, good math.


None

ThePeasant

Reply To Post Reply & Quote

Posted at: 6/24/08 11:17 PM

ThePeasant NEUTRAL LEVEL 08

Sign-Up: 08/30/07

Posts: 447

I suck when it comes to art, so I didn't even try. I'm hoping someone will come along and say 'hey man, want me to make you a menu?'

That would kick ass....


None

GustTheASGuy

Reply To Post Reply & Quote

Posted at: 6/25/08 03:38 AM

GustTheASGuy LIGHT LEVEL 08

Sign-Up: 11/02/05

Posts: 11,383

You should lock and unlock the bitmapdata before setpixeling.

One thing is that array access is untyped so field access is hashed. Better cast it when accessing. And store the Color (TCol [n]) locally.

Also, 100 times per second? Try 10000 a frame.

#ngprogramming at irc.freenode.net
haXe | Keel imperative | Spyro! | Thru you


None

ThePeasant

Reply To Post Reply & Quote

Posted at: 6/25/08 05:55 AM

ThePeasant NEUTRAL LEVEL 08

Sign-Up: 08/30/07

Posts: 447

Thanks for the suggestions!

At 6/25/08 03:38 AM, GustTheASGuy wrote: You should lock and unlock the bitmapdata before setpixeling.

I just tried that. Apparently, every time i write to the bitmapdata from then on, it automatically unlocks the bitmap data which means I just have to lock it again afterwards. Doesn't seem like it helped the framerate at all either.

One thing is that array access is untyped so field access is hashed. Better cast it when accessing.

I don't understand what that means lol, does that mean faster or slower?

:And store the Color (TCol [n]) locally.
The only way I could find to store Color was through the ColorTransform class.
Do local variables run faster than global ones? I don't understand your point, please explain if you can.


None

GustTheASGuy

Reply To Post Reply & Quote

Posted at: 6/25/08 06:06 AM

GustTheASGuy LIGHT LEVEL 08

Sign-Up: 11/02/05

Posts: 11,383

Function-local variables are accessed the fastest. That's why it's a good idea to localise a variable you access a lot.

When a variable is untyped, the compiler doesn't know what fields it has so it's resolved at runtime. The way its done is the field name is hashed to figure out to which field it corresponds. That's slow. When you access something from an array the value is untyped.
http://en.wikipedia.org/wiki/Hash_functi on

It doesn't matter what class you use. I figured you'd define your own.
My point was you should store the 'ColorTransform (TCol [n])' value locally so it's faster to access the three times and casted (also a somewhat expensive operation, but improves access time statically).

#ngprogramming at irc.freenode.net
haXe | Keel imperative | Spyro! | Thru you


None

Kajenx

Reply To Post Reply & Quote

Posted at: 6/25/08 06:30 AM

Kajenx DARK LEVEL 16

Sign-Up: 12/01/06

Posts: 752

Let me translate for Gust:

"Typing" means declaring what a variable is.

Typed:
var Blah:Number

Non - Typed
var Blah

When you don't Type a variable, it has to look up what it is, which is called hashing, and this takes time to do. When you're looping a line of code 1000 times, that lookup time is expounded and slows your stuff way down.

Function Local variables are once you simply declare within you function:

function Blah(){
var Blah:Number
}

These work faster within the function and will free up memory later when the function is done and the variable is deleted.

For the locking and unlocking, do the lock BEFORE your whole loop, then unlock AFTER it. Not sure what you tried, but that's the proper way to do it.

You might find this thread useful, as I basically went over these same things with Gust and Dougy. Thanks again dudes! :3

BBS Signature

None

henke37

Reply To Post Reply & Quote

Posted at: 6/25/08 06:39 AM

henke37 NEUTRAL LEVEL 23

Sign-Up: 09/10/04

Posts: 3,604

The player got a bunch of fast access registers. The compiler will storage your local variables in them. Not only is it fast to access, it usually doesn't even need to save the variable names in the output bytecode. So there is no runtime speed penalty for long local variables.
And the constants in Math is resolved at compile time, you do not need to worry about run time speed there either. The compiler is smart enough to precompute 100*Math.PI/180 into a literal in the bytecode.

I recomend that you read the swf file format specification and start playing around with a decompiler.

Each time someone abuses hittest, God kills a kitten. Please, learn real collision testing.


None

GustTheASGuy

Reply To Post Reply & Quote

Posted at: 6/25/08 06:46 AM

GustTheASGuy LIGHT LEVEL 08

Sign-Up: 11/02/05

Posts: 11,383

At 6/25/08 06:39 AM, henke37 wrote: The player got a bunch of fast access registers. The compiler will storage your local variables in them. Not only is it fast to access, it usually doesn't even need to save the variable names in the output bytecode. So there is no runtime speed penalty for long local variables.

Local variables are stored in the stack. There are no names, even in AS2.

#ngprogramming at irc.freenode.net
haXe | Keel imperative | Spyro! | Thru you


None

ThePeasant

Reply To Post Reply & Quote

Posted at: 6/25/08 07:00 AM

ThePeasant NEUTRAL LEVEL 08

Sign-Up: 08/30/07

Posts: 447

thx guys for the advice. and oh my what a difference it made! I think it runs like, 10x smoother now.
I think it is now good!

Wondering now: I have a very basic system for storing the data (as you can see, it just lists the values). What's the simplest way to... you know... make it shorter?

At 6/25/08 06:30 AM, Kajenx wrote: Let me translate for Gust:

"Typing" means declaring what a variable is.

Typed:
var Blah:Number

Non - Typed
var Blah

When you don't Type a variable, it has to look up what it is, which is called hashing, and this takes time to do. When you're looping a line of code 1000 times, that lookup time is expounded and slows your stuff way down.

Function Local variables are once you simply declare within you function:

function Blah(){
var Blah:Number
}

These work faster within the function and will free up memory later when the function is done and the variable is deleted.

For the locking and unlocking, do the lock BEFORE your whole loop, then unlock AFTER it. Not sure what you tried, but that's the proper way to do it.

You might find this thread useful, as I basically went over these same things with Gust and Dougy. Thanks again dudes! :3

All times are Eastern Standard Time (GMT -5) | Current Time: 09:36 PM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!