Use the Correct Type for the Job

Have you been put off from using the correct type for the job after reading the recent infomation from Sho and Grant about speed of uint and int compared to Number? Obviously this information is worth noting, but the 2 post titles ‘Avoid ints in ActionScript‘ and ‘Types in AS3: ints not so fast, uints slow! ‘ are bound to put some people off using them at all and sticking with Number, which they shouldn’t do.

I chatted about their results with Peter Hall the other day, with the conclusion that I will continue to use uint and int in the correct places.

For one, in Grants tests he is doing 16777215 and the results are measured in milliseconds, so it isn’t a major speed issue. Also, using typed vars isn’t all about the execution speed of code in the player, its also practical, and speeds up the development process.

For instance, take a look at the List component and its property selectedIndex. It’s important that only a positive whole number is used here, and therefore uint is ideal for the job, enabling the compiler and player to throw an error if a negative or decimal number is used.

Keep the results posted by Grant and Sho in mind, but also keep in mind using the correct type for the job in hand!

14 Responses to “Use the Correct Type for the Job”

  1. Josh Tynjala says:

    Agreed. Sho and Grant provided good information, but I worried about how people would respond to it. You should almost always continue using ints and uints in your normal code. If you have a section that needs to be high-performance, and you know that changing some variables to Number would give you a healthy boost, then it may be justified. If you want to use Number in a for loop to go through a dozen items in an Array, you might not see much difference.

  2. JesterXL says:

    var foo:*

    * ALL THE WAY!!! St@r 4 teh w1n!!!!

  3. keith peters says:

    Good point Tink. An important programming principle is to not optimize too early. Many optimization “tricks” go against best practices and good programming form, thus often obscuring bottlenecks and other opportunities for more major optimization in the structure of the code itself.

    You can bet that in most programs out there, you can find FAR better opportunities for optimization than the 30 milliseconds saved in a 16 million iteration loop.

    Bottom line, if you have a poorly performing, sluggish program that you need to optimize, I can pretty much guarantee that if you change all your ints and uints to Number, you’re still going to have a poorly performing, sluggish program, now with incorrectly used data types to boot.

  4. [...] eresting stuff, and important to know for the professional programmer. But Tink brings up some good points about the value of using the correct types for the job, [...]

  5. “It’s important that only a positive whole number is used here, and therefore int is ideal for the job”

    Wouldn’t that be a uint, then?

  6. Tink says:

    Wouldn’t that be a uint, then?

    errrr.. cough… splutter… errr yeah! I’ll update that!

  7. nwebb says:

    A good point made about typed vars not being purely about execution speed. Using int/uint also seems to make sense in terms of future proofing code – I’d imagine there’s a possibility of Adobe optimising the performance of int/uint in future releases, so it seems sensible to use those types where applicable, unless you really need that extra performance boost.

    As an aside, I remember reading [somewhere] in the docs that the Flex framework currently uses int rather than uint for array index values (so they can use -1 to represent no value), and uint is recommended for values such as 32 bit colours, because something like 0xFFFFFFFF would be too big to assign to an int.

  8. In real-world practice, this seems like the kind of guideline that would primarily be used with, as Grant demonstrated, really large for loops. I’ll try putting this to the test with per-pixel manipulation of bitmapdata objects on a recent project of mine when I get home.

    Now THERE’S a feat that can never execute too fast!

  9. vijay says:

    Though this post maybe old, I thought I would add my 2p on this subject. Both uint and int are subject to overflows and can cause problems in Flash. Though you might not come across it on a normal basis but its good to know. I have blogged about this here:
    http://vijayram.wordpress.com/2007/02/04/integer-overflow/

    cheers,
    vijay

  10. Marcus Stade says:

    To be honest, if you’re depending on the datatype to fend off “off by one” type errors, I think you got bigger problems with your code than performance issues.

    While I am all about semantics, it makes no sense to use a data type which introduces these kinds of performance issues. More over, it makes no sense coming from any sane language and realising that the reason why integer division is slow is because internally it’s rounding the result. I might have misunderstood the internals on this one and I sure hope I have, but that kind of VB-ism is dangerous at best.

    Now, if you discard the performance issues bundled with int and uint, you’ll still have the issue of overflowing. While this issue is ever apparent in any numeric datatype (it’s just a matter of when), you hit the roof quite a bit more easily when using int or uint instead of a 64-bit datatype.

    That being said, I am as I said all about semantics. I do think you should use integers if what you expect to work with is infact just that, integers. You will most likely come across issues far greater than performance issues if you start using floating point numbers to do integer based calculations. Checking for equality comes to mind.

    Now the real WTF here is the fact that int and uint are so slow. Integers are far easier to deal with and should have really been a no brainer to implement correctly. Kudos to Adobe for screwing that one up.

  11. Christy says:

    different problems require different strategies…thats a case in point for the topic

  12. Zir0 says:

    Hey.

    Nice to meet you.

    I understand what Sho and Grant are saying, and I also understand what you’re saying, but no one mentioned anything about making a game in Flash.

    Often times, you may want to squeeze every ounce you can out of Flash (even with AS3, Flash 9, it’s still slow as Christmas). Those inner loops are what you may need to focus on, especially in games. Coding etiquette and using the right variable type is great and all, but when push comes to shove, all that goes out the window if it means getting rid of some bottleneck. You need all the help you can get in Flash. ;-)

    __________
    -Zir0 out.

  13. [...] 2006 Types in AS3: ints not so fast, uints slow!, by Grant Skinner, date: June 15th, 2006 Use the Correct Type for the Job, by Think, date: June 20th, 2006 Optimization: When to [...]

  14. sandrar says:

    Hi! I was surfing and found your blog post… nice! I love your blog. :) Cheers! Sandra. R.

Leave a Reply