问题
Flash is known to behave in very unpredictable ways ways when it comes to manipulating data. I'm curious that if there is any performance/memory benefit to using Numbers instead of ints aside from values that need precision. I have heard that some basic operations in Flash may convert multiple times between the two type to resolve the expression. I've also heard that Flash runtime, under the hood, actually maps ints to non-precision Numbers/Floats during runtime. Is any of this true?
回答1:
Flash runtime is a dark place indeed.
- As you mentioned AVM2 does convert big ints into Number.
- Whole Numbers are actualy ints.
- And there's more stuff about ints.
- Uints used to be slow used in a loop BUT NOW THEY ARE NOT (results in the article seem to be a combination of weird bytecode generation and JIT optimizations).
- Numbers take more space in memory but this is
nothingcompared to even a single JPEG file. - Logically it feels better to use
uintsin loops. - Numbers are floating point falues you have to be careful comparing them.
Jackson Dunstan does pretty good tests of different AS3 language constructs performance. Of course it's always good to check results yourself. From the series about 10.2 performance you can see that with every new Flash Player version they optimize something but other things might get slower: 1 2 3.
P.S. This answer might get old very soon and might as well be cited in a couple of years later which of course will be wrong.
回答2:
You don't get any real performance benefit with int over Number. So if you're not using a variable for stuff like loop indices or things that require exact increments, Number is fine. In fact, a Number can be NaN if you get an invalid result, which is a nice benefit.
来源:https://stackoverflow.com/questions/9525203/are-ints-always-faster-than-numbers-floats-in-as3