Working on a CPU-intensive app in flash is a challenging experience. It can be wonderful or frustrating, depending on your mindset.
Based on my experience with the flash chess and other intensive applications, I’ll give some tips and ideas on how to get the most out of your flash project.
The first hard question you need to answer is whether or not you actually need to optimize the performance. Most flash apps are not that intensive in terms of processing so the time would be better spent elsewhere.
당신의 코드를 최적화 시킬 필요가 있는가? 퍼포먼스를 위해 반드시 필요하다.
Your code’s speed needs to be optimized only when there’s a hard time constraint. Some examples would be:
Don’t start coding with optimization in mind. It’s a very common mistake and rather unintuitive, but when you start a project you need to concentrate on functionality rather than speed. Write clean code, document it and make sure it works correctly rather than fast. Any non-trivial application will have bugs during development and having to deal with clever tricks won’t make the debugging process any faster.
코딩을 시작할때부터 최적화에 대해서 신경쓰지 말아라.
Once your application (or framework or whatever) works A to Z and you believe it’s reasonably bug-free, you can start looking at performance, and the first thing to do is to establish some sort of metric, for example frames/second or nodes/second, or time to encode a certain file or something like that.
The problem now lies in identifying the really intensive areas – and it’s not as easy as it seems. Depending on your application’s complexity, you may have anywhere from ten to a hundred classes and tens of thousands of lines of code. You may have a rough idea of the most intensive areas, but it’s difficult to pinpoint it.
The best tool I know of to help you in this stage is the Flex Builder Profiler. It shows you the memory usage, but more importantly, it shows you how much time is spent in each function. At this time, I am not aware of any other tool for Flash that does the same, so it may be worth for you to download trial version of Flex Builder and use it at least for profiling. Even if your project is not based on Flex, you should still be able to make the classes work with it, maybe add a simple interface.
When I first run the profiler on my chess engine, I was quite surprised by the results. I found out that 85% of the time was spent by an little function that was called around 500,000 times. Just by improving that function’s logic (no code optimization per se), the performance increased from 1000 to 7000 nodes/second.
플렉스 빌더 프로파일러를 사용하면 (트라이얼 버젼이라도) 메모리 사용량과 각 함수가 연산할때 걸리는 시간도 알려준다.
Now that you know the most intensive areas, you can start optimizing them, and have a good prioritization.
The first – and one of the most important optimization tips is to always use strongly typed variables. This is especially evident when working with array elements. Since in actionscript arrays can contain any kind of data, the Flash VM must do extra work when processing them.
최적화 시작의 첫번째 - 항상 강력하게 형식화된 변수(특히 배열)를 사용한다.
To give you an example, consider this code:
var crtPiece:int = movesArray[i].piece;
If you rewrite this into
var crtMove:Move = movesArray[i]; var crtPiece:int = crtMove.piece;
you’ll notice a massive speed improvement.
The same goes to a code like this:
var element:int = matrix[i][j];
which can be rewritten into
var row:Array = matrix[i]; var element:int = row[j];
Alternatively, if you’re targeting Flash Player 10, you can define
var movesArray:Vector.<Move>;
or, for the second case
var matrix:Vector.<Vector.<int>>;
and then you won’t need the extra code.
It’s worth mentioning that the speed is actually about the same when using the array strong type method versus the Vector method, so if you already use the first method, there’s no need to use Vectors for performance reasons.
위 코드처럼 복합적인 변수 선언보다는 풀어서 선언해주는것이 좋다.
This is a tip I received from a Flash Player engineer at Adobe.
See, I always thought that when you define something like
상수를 사용하지 말아라.(adobe 개발자의 팁)
public const NAME:String = "foo";
the compiler will actually replace any occurrences of the constant with its value. This is not the case.
Using constants is a very good way to keep the code clean and readable and that’s why I wrote earlier that you should optimize only where absolutely necessary.
In my chess game, I used to have nice constants for piece values, e.g. WHITE_PAWN and even for squares (A1 or H8). By replacing the constants with their values, the code decreased in readability – if (pieceType==3) or if (targetSquare==7) is not terribly intuitive but the speed increase is significant.
컴파일러는 상수를 하나의 케이스로 간주한다. 상수로 깔끔하게 코드를 유지할 수는 있지만 직관적인 속도에 증가는 없다.
This is another potentially painful decision. Years ago, looking at some C code, I was wondering why the author is defining some complex macros like
연산을 간소화 해라.
#define MIN(a,b) ((a)<(b) ? (a) : (b))
instead of writing a function.
It’s because it’s faster to do so. If you call a simple function 100,000 times, you’ll get a benefit from just writing an equivalent inline. This is especially true for functions in the Math class. You can rewrite
var x:int = Math.min(a,b)
into
var x:int = (a<b) ? a : b;
I removed a number of small functions (1-2 lines) that were dealing with conversions or simple math, for some 10-15% speed increase.
In the same vein, don’t use Array methods like push(), pop(), shift() or unshift(); you can rewrite the code a little and get better performance.
int and bitwise operationsWhenever you deal with integer numbers, use int. Interestingly, using uint does not bring any speed benefits for array indexes for example, so don’t bother with it unless you really really need it (in fact I noticed uint to be slower in certain cases).
int 와 비트연산을 사용해라.
Bitwise operations can be anywhere from 2 to 10 times as fast as "normal" operations. This is not a tutorial on bitwise operations (it would help if you understand bits and bytes), but some examples would be:
var x:int = a*2; var y:int = b*16; var z:int = c/4;
can be rewritten as
var x:int = a << 1; //2^1 = 2; var y:int = b << 4; //2^4 = 16; var z:int = c >> 2; //2^2 = 4;
Usually you can test if a number n is a multiple of x like this:
if (n % x == 0)
you can rewrite this is
if (n & (x-1) == 0)
The tips above were related to coding only; this one is about saving time by having data already calculated.
Math functions like sin(), ln(), sqrt() are slow and moreover, you may need the same value over and over again. For example, assuming you only deal with integer angles, it may be useful to precompute the sine for all 360 degrees and place them in an array. This way you don’t need to convert from degrees to radians and call Math.sin() at all, just read sine[25] and that’s it.
In other cases, you may not have the capacity to store all possible values, but if the values result after intensive computations, it’s still worth caching them for possible future use. In the case of my chess game, after I evaluate a branch, I store the result in an object using a unique hash. Because at different times the search algorithm can encounter the same board position, it can look up that position to see if it has been handled before, and if so it’ll just take the result. There are a few catches with caching, the first one being that you must create a unique ID (hash) for each value that you cache. Generating unique IDs is not that easy and must be fast enough so that it does not negate the speed benefit from caching. The other constraint is memory; major chess engines can allocate hundreds of megs for their caches, whereas people don’t tolerate this in a flash app, so you must clean your cache from time to time.
Using the strategies above, I was able to improve the performance of my chess game from 7000 to 14000 nodes/second.
You may use some of them or most of them, depending on the project you’re working on. Precomputing and caching is a good idea regardless of the project and bit operations are too fast not to consider. Other strategies are more time consuming or may affect the structure too much.
Good luck
Louis Vuitton and Marc Jacobs first cooperation has already been 15 years of time,Cheap Gucci although not for the world at the beginning of the cooperation,Cheap Gucci Shoes in view of the cooperation between think they won't last long. Louis Vuitton brand because this is traditional,Louis vuitton store and designer at that time and Jacobs is very young and very funny. However the cooperation between them is like a love anecdotes, let the other parties in the 15 years of the time it becomes more and more powerful and strong.cheap gucci shoes Now, the pair, a full of France's taste and respect for the world, and the other one is still active as before.Cheap Gucci Clothing Now New York designer Marc Jacobs be born is in Paris is Musee des Arts Decoratifs an exhibition at the museum of the Louis Vuitton-Marc Jacobs "exhibition protagonist.Cheap Gucci Shoes Seriously, the two men look doesn't seem to be any common ground, one is the expression serious gentleman Vuitton (at least from the entrance of the museum's portrait of the look the way it does),cheap louis vuitton and the other is wearing a short skirt of Scotland rebellious guy. However, the founder of the brand or a real genius,Cheap Gucci Handbag successfully hold the 19 th century people is fond of sport and innovation hobby, making a suitable for early car the trunk and place the luggage into camping use of folding bed.Prada Handbag Jacobs stylist to the same brand compelling creativity, hoping to break all the rules bound,cheap louis vuitton shoes as the brand's first and only ready-to-wear designers,Cheap Gucci backpack he wants to design costumes and deserve to have an amazing vigor. As in 1896, the designer Louis Vuitton Georges,Armani Clothing Wholesale the son of the brand Logo design with the whole box print the shell, no one can predict to the move to the impact of the brand.cheap prada shoes Who will think of stylist Jacobs will this classic Speedy handbag designer Takashi Murakami series let Japan above picture with big round eyes,Cheap Gucci Belt or let Stephen Sprouse above master graffiti colored the graffiti letters? Founder Louis Vuitton and he created the world in this exhibition hall on the first floor,cheap jordan sneakers suitable for simple travel series small handbag on display in different one display the;New Arrive Shoes Another display shows the used to hold various delicate clothing suitcase-tea party dress, morning gown; Dinner dress and PROM gowns; Countless levels of underwear and enormous bustle,Jordan Shoes then the s women a day seven times of change clothes golden age. But Debbie Harry song soon put you attracted to the second floor of the exhibition hall,Nike Shoes there is a huge handled the screen (curator Pamela Golbin make it a slight bo page form), used to highlight the free spirit of Jacobs advocate the effects of Bertolucci-and movie "South Park" mix together,Winter Clothing "Cat on a Hot Tin Roof" next to SpongeBob SpongeBob, SpongeBob SpongeBob next to German director Rainer Fassbinder's gay film genius "Querelle".Mens Shoes The exhibition is more like a celebration not simple works retrospective exhibition in accordance with the time not the sequence of the display,Mens T Shirts but the exhibition are divided into different subjects on. Jacobs to give each a different theme plays a very attractive name,Adidas Men Shoes each subject inside of the exhibits in the past 15 years is the representative work of carefully selected fashion. Shoes with mechanical legs to the show,Men Suits &Ties they have a dance Busby Berkeley that precise master pace. Artist Richard Prince of five nurses the human body model hat spell "L-O-U-I-S V-U-I-T-T-O-N",Mens Long Sleeve at the same time in the background is continuously came 1963 Kingsmen of famous chorus works: "Louie Louie,......" . The museum these trunks called it "chocolate box",Mens Sweater may produce more attractive for the audience. The display of trunk is like a great little French candy,Mens Jeans whether it is full of LV logo printing white ermine equipment packet to the purple pattern of the large truck suitcase, are displayed to the full and explore the LV brand connotation.Mens Jackets Whether jeans, metallic fabrics, or the woven plastic fabrics, handbag to send out the strong temptation, Louis Vuitton is this may never dream of. http://ixcoin.org/forum/index.php?topic=15359.0 http://ixcoin.org/forum/index.php?topic=15358.0 http://ixcoin.org/forum/index.php?topic=15360.0 http://ixcoin.org/forum/index.php?topic=15361.0 http://ixcoin.org/forum/index.php?topic=15362.0 http://ixcoin.org/forum/index.php?topic=15363.0 http://ixcoin.org/forum/index.php?topic=15364.0 http://ixcoin.org/forum/index.php?topic=15365.0 http://ixcoin.org/forum/index.php?topic=15366.0 http://ixcoin.org/forum/index.php?action=profile;area=summary;u=8275 http://secureink.net/forum/index.php?topic=21678.0 http://secureink.net/forum/index.php?topic=21679.0 http://secureink.net/forum/index.php?topic=21680.0 http://secureink.net/forum/index.php?topic=21681.0 http://secureink.net/forum/index.php?topic=21682.0 http://secureink.net/forum/index.php?topic=21684.0 http://secureink.net/forum/index.php?topic=21685.0 http://secureink.net/forum/index.php?topic=21686.0 http://secureink.net/forum/index.php?topic=21687.0 http://secureink.net/forum/index.php?action=profile;area=summary;u=12091 http://enjoykalkan.com/forum/index.php?topic=9145.0 http://enjoykalkan.com/forum/index.php?topic=9146.0 http://enjoykalkan.com/forum/index.php?topic=9147.0 http://enjoykalkan.com/forum/index.php?topic=9148.0 http://enjoykalkan.com/forum/index.php?topic=9149.0 http://enjoykalkan.com/forum/index.php?topic=9150.0 http://enjoykalkan.com/forum/index.php?topic=9151.0 http://enjoykalkan.com/forum/index.php?topic=9152.0 http://enjoykalkan.com/forum/index.php?topic=9153.0 http://www.cubasi.net/index.php?option=com_fireboard&Itemid=54&func=view&catid=2&id=4797#4797 http://www.cubasi.net/index.php?option=com_fireboard&Itemid=54&func=view&catid=2&id=4798#4798 http://www.cubasi.net/index.php?option=com_fireboard&Itemid=54&func=view&catid=2&id=4799#4799 http://www.cubasi.net/index.php?option=com_fireboard&Itemid=54&func=view&catid=2&id=4800#4800 http://www.cubasi.net/index.php?option=com_fireboard&Itemid=54&func=view&catid=2&id=4801#4801 http://www.cubasi.net/index.php?option=com_fireboard&Itemid=54&func=view&catid=2&id=4802#4802 http://www.cubasi.net/index.php?option=com_fireboard&Itemid=54&func=view&catid=2&id=4803#4803 http://www.cubasi.net/index.php?option=com_fireboard&Itemid=54&func=view&catid=2&id=4804#4804 http://www.cubasi.net/index.php?option=com_fireboard&Itemid=54&func=view&catid=2&id=4805#4805 http://www.cubasi.net/index.php?option=com_fireboard&Itemid=54&func=myprofile&do=show http://www.ipacific.com/forum/index.php?topic=940.0 http://www.ipacific.com/forum/index.php?topic=941.0 http://www.ipacific.com/forum/index.php?topic=942.0 http://www.ipacific.com/forum/index.php?topic=943.0 http://www.ipacific.com/forum/index.php?topic=944.0 http://www.ipacific.com/forum/index.php?topic=946.0 http://www.ipacific.com/forum/index.php?topic=947.0 http://www.ipacific.com/forum/index.php?topic=948.0 http://www.ipacific.com/forum/index.php?topic=949.0 http://www.ipacific.com/forum/index.php?action=profile;area=summary;u=2367 http://raptisoft.com/forum/index.php?topic=3242.0 http://raptisoft.com/forum/index.php?topic=3243.0 http://raptisoft.com/forum/index.php?topic=3244.0 http://raptisoft.com/forum/index.php?topic=3245.0 http://raptisoft.com/forum/index.php?topic=3246.0 http://raptisoft.com/forum/index.php?topic=3247.0 http://raptisoft.com/forum/index.php?topic=3248.0 http://raptisoft.com/forum/index.php?topic=3249.0 http://raptisoft.com/forum/index.php?topic=3250.0 http://raptisoft.com/forum/index.php?action=profile;area=summary;u=7289 http://sober4life.org/forum/index.php?topic=113874.0 http://sober4life.org/forum/index.php?topic=113876.0 http://sober4life.org/forum/index.php?topic=113877.0 http://sober4life.org/forum/index.php?topic=113878.0 http://sober4life.org/forum/index.php?topic=113879.0 http://sober4life.org/forum/index.php?topic=113880.0 http://sober4life.org/forum/index.php?topic=113881.0 http://sober4life.org/forum/index.php?topic=113882.0 http://sober4life.org/forum/index.php?topic=113883.0 http://sober4life.org/forum/index.php?action=profile;u=127684