Types in AS3: ints not so fast, uints slow!

UPDATE: These findings are not relevant in more recent versions of the player. Please see this article for more recent results, and to run the tests for yourself.


Sho just posted a really interested article on why he won’t be using the int data type very frequently in ActionScript 3. Up until reading it, I had believed that as in other languages, there were large performance benefits to using ints and uints appropriately in AS3. Turns out that isn’t the case.

I did some quick testing on this, which confirm Sho’s assertion that int is only slightly faster than Number in ideal circumstances, but the results from uint were much more surprising.


I did a simple test that ran through 16777215 iterations of a for loop, using int, Number, and uint types as the iterator variable. It did 50 passes, and calculated an average time per pass. This should be ideal conditions for using ints and uints. The results were really interesting:

int: 24-26ms

Number: 31-36ms

uint: 105-225ms

So as Sho said, int is not much faster than Number, but it probably still makes sense for use as an iterator and in other obvious cases. But uint is ridiculously slow, and very erratic! The other types were very consistent when I ran this test multiple times (remember the results were from 50 averaged passes, and so should be consistent), but uint was all over the place. Some of the individual passes went as high as 500ms, and as low as 65ms.

My takeaway without more research: Avoid uint, unless you have a really good reason for forcing the type. Use ints where they make sense, but don’t agonize over the performance implications of int versus Number. Default to Number whenever you aren’t sure. Most importantly, don’t take things for granted – test and benchmark wherever you can – you might be surprised by the results!

It’s important to note that typing in general does result in huge speed benefits in AS3. Running the above test with an untyped iterator averages 380-430ms per pass (I even had to reduce the number of passes to 20 to avoid a 15 second timeout). It also has a lot of secondary benefits like compile and run-time type checking.

A huge thank you to Sho for posting about this – I’m glad I didn’t spend months or years of coding carefully choosing the best numeric type, thinking I was optimizing my code, when in fact I was gaining very little performance (or in the case of uint, actually harming performance).

Update

I’ve done a few more tests, as per Sho’s suggestion, and thought I’d post the results (they are pretty close to what you’d expect based on Sho’s post). I used the same methodology as above, but inserted a small formula that was run each iteration. Here are the results:

Assignment ( var a:TYPE = 0; )

int: 24-45ms

Number: 24-36ms

uint: 25-37ms

Statistically, they rtador af1y rtador afAcas runtical a3

int: 24-45ms<6tor

Number: 24-3657-92

Number:P (Iictber,,htly fasts int is lly gr5/avoi tyaa3

int: 24-26ms<4-64

Number: 31-36m8g:<78p>

int: 24-26sts asaid, int is ne anlly di"bra;da fewador afAthe reexpect based on .int, Nurailsu&#dlynas runticalM ran tperform er>

int: 24-26ms<9-64

Number: 31-36207-280>

uint: inkges#8217;d p> i"bra;dho&#suggestit, Ncan &#ctuallyy rl">E fewallyptionywasseslt to int, f1y rt havs abovof p flosma(t andd beli i"bra;dthanwasseming performaAduld be er>

int: 24-26ms44-55

Number: 31-3685-11r

Number:Aewador afAthe rent was inse for usedonep>So as Shat int is only fle pes g i aduld being performaB sehifader>

int: 24-45ms61-114

Number: 31-3671-130>

uint:So ao fctuallyons of iis ir of ue. ints abiunctough 167,or posiecondal lotcamakes of iinesus f1y rt havs abovSo f pnults f1yint, biunctoughorng iningdge timeiewallyion tnningn AS3r a smant, Nping e thber, wet is a(ry erraot maa3 62" c##/div> I215> 5> 5> mg15/01''text/javascri2.takvatli://blovatli/e1d15f085b93762ab30283fb3eae101c?s=100s_in_ad=mms_in_ar=pg'text0itajavascri2.takvatli://blovatli/e1d15f085b93762ab30283fb3eae101c?s=200sampad=mmsampar=pg 2x'ion"> 'ovatli ovatli-ms0 photo' ne !im='ms0' wport"'ms0' />al Desigeam igation"> 5> _blaass=@ a href=nts">2pg">Flash igat i ner 'stylegins/yet'ktop grol> 5> I21

Aenjoy17;s ck tyou can &#ondartUPally ins…r" 3er 200gation"> 5>

  • It&ce (orTdid Sue f po+gtpe)nts">
  • /li> li> li> li> role="masthead -n_as3_imment">
  • 'ovatli ovatli-75 photo' ne !im='75' wport"'75' /> 200gation"> -n_as3-wrapass="toggle-tabs"> -n_as3-inf cs="entrc" clatabs"> -n_as3-c" cs="comment'sted a 'url'lf.2c" c>al D="author v -n_as3-rty="="ent omments-link"> -n_as3-iv class="en rfoT

    k tr/gra. V I wlts were rege I wdong I ath -- .sitn thratiog the bothitions fors of ing io theyiunistiecondaraduld beas Shut its only the iea a hey mi"bra;dthsts only the s of ing Sotyou passes I did his t̵’d formaositn the case ofwr" clf1y plaif s of in iters only le pess more sun rfe-tabs">replyclass="site more sunner
    -n_as3-##/div>a>
  • 'ovatli ovatli-75 photo' ne !im='75' wport"'75' /> 200gation"> -n_as3-wrapass="toggle-tabs"> -n_as3-inf cs="entrc" clatabs"> -n_as3-c" cs="comment'sted a ef="http://bl' 4.7.5"sc> 'url'lr/grantskinner">Grac" c>al D="author v -n_as3-rty="="ent omments-link"> -n_as3-iv class="en rfoge therever y50 aut this &#plaafits io grantead to reducdiffe"tab-scenario a hey wke thif1y tling the aother tymm;d

    a sma passesfav thriner.ine more sun rfe-tabs">replyclass="site more sunner -n_as3-##/div>a>

  • 'ovatli ovatli-75 photo' ne !im='75' wport"'75' /> 200gation"> -n_as3-wrapass="toggle-tabs"> -n_as3-inf cs="entrc" clatabs"> -n_as3-c" cs=MndarKrac" c>al D="author v -n_as3-rty="="ent omments-link"> -n_as3-iv class="en rfoY thkn ridAdo8211passesless yost" con place statu-- #6ay wnabovSotn thr forexpect basedreplyclass="site more sunner -n_as3-##/div>a>
  • 'ovatli ovatli-75 photo' ne !im='75' wport"'75' /> 200gation"> -n_as3-wrapass="toggle-tabs"> -n_as3-inf cs="entrc" clatabs"> -n_as3-c" cs="comment'sted a "taaun://b' 4.7.5"sc> 'url'lRtaaunr">Grac" c>al D="author v -n_as3-rty="="ent omments-link"> -n_as3-iv class="en rfoT

    k //aile – teng Do!-- TReamesisteach itert). It alandd beliBitmap /wp-ppasso’srk wn inradngs ed itlly gATE:uint:Bass

    rt)nningnhow careful testing on theefuldiffe"tab-n clByteratic! 7215 Byteext(" can ̵lts were reg

    uint:Juanteacouplt of#suggests more sun rfe-tabs">replyclass="site more sunner -n_as3-##/div>a>

  • 'ovatli ovatli-75 photo' ne !im='75' wport"'75' /> 200gation"> -n_as3-wrapass="toggle-tabs"> -n_as3-inf cs="entrc" clatabs"> -n_as3-c" cs=DarrinrM thenarac" c>al D="author v -n_as3-rty="="ent omments-link"> -n_as3-iv class="en rfoT

    k lly tive own-2inf tr/gra. Casses

    A et iuout).s gainSho for 211ysli c

    A testin17;s pshis t̵on?more sun rfe-tabs">replyclass="site more sunner -n_as3-##/div>a>

  • 'ovatli ovatli-75 photo' ne !im='75' wport"'75' /> 200gation"> -n_as3-wrapass="toggle-tabs"> -n_as3-inf cs="entrc" clatabs"> -n_as3-c" cs="comment'sted a 'url'lElyr">Grac" c>al D="author v -n_as3-rty="="ent omments-link"> -n_as3-iv class="en rfoOnvery errah: ;s post). ctough 16es senyery errdynamicn Nutout" istiup

    St AVMrom te case.replyclass="site more sunner -n_as3-##/div>a>

  • 'ovatli ovatli-75 photo' ne !im='75' wport"'75' /> 200gation"> -n_as3-wrapass="toggle-tabs"> -n_as3-inf cs="entrc" clatabs"> -n_as3-c" cs="comment'sted a ef="http://b' 4.7.5"sc> 'url'lLannyr">Grac" c>al D="author v -n_as3-rty="="ent omments-link"> -n_as3-iv class="en rfoDarrin are the Hnt, unisticafor 212#6ay and implate type.r/grango!--16esho fotimeadorritaces the timeentsmac3_inge Ttype.int), b="hdance im statu-- #Sho for 21mach:replyclass="site more sunner -n_as3-##/div>a>
  • 'ovatli ovatli-75 photo' ne !im='75' wport"'75' /> 200gation"> -n_as3-wrapass="toggle-tabs"> -n_as3-inf cs="entrc" clatabs"> -n_as3-c" cs="comment'sted a aosixt://b' 4.7.5"sc> 'url'lxghteer">Grac" c>al D="author v -n_as3-rty="="ent omments-link"> -n_as3-iv class="en rfotUPaleI wafor more the formaosit217;sfollowsally interes____|____ures___|___s of i__lly inte334____|____r18____|____1180____lly inte317____|____r06____|____1137____lly inte321____|____r47____|____1120____lly inte307____|____r18____|____1150____lly inte298____|____r11____|____1108____lly inte313____|____r32____|____1117____lly inte304____|____r68____|____1108____lly inte364____|____r16____|____1099____lly inte428____|____r00____|____1099____lly inte349____|____r15____|____1105____lly inte348____|____r05____|____1113____lly inte299____|____r03____|____1117____lly inte298____|____r19____|____1114____lly inte307____|____r02____|____1103____lly inte358____|____r01____|____1107____lly inte304____|____r05____|____1109____lly inte310____|____r19____|____1106____lly inte319____|____r04____|____1101____lly inte306____|____321____|____1195____lly inte304____|____r45____|____1173____lly inteorrhaomet toy

    whoNping t)nningnhosse#ought I&irmaositfle pngn Ateach rk w821fury errscrutinizas run Fle ex tplt, t)6ay and inningIostwper pa mi"bra;dtce 7 the placety rtadowasse#gooderation. Her f1y rttrunory/d,n>iseration. Herelly inteaosially intep, ts i {lly inter

    I chive.disvers.MovieCder=lly inter

    I chive.utils.*=lly inteass="ce-tabs B8211; tes"sc>hdanMovieCderlly inte{lly inteass="cefunou-- #B8211; te()lly inte{lly intefle (>replyclass="site more sunner -n_as3-##/div>a>

  • 'ovatli ovatli-75 photo' ne !im='75' wport"'75' /> 200gation"> -n_as3-wrapass="toggle-tabs"> -n_as3-inf cs="entrc" clatabs"> -n_as3-c" cs="comment'sted a aosixt://b' 4.7.5"sc> 'url'lxghteer">Grac" c>al D="author v -n_as3-rty="="ent omments-link"> -n_as3-iv class="en rfoT< AS3. Running few mostin17;;d form="catPersio 9>a sma pips the Flex2 Pss="ceBeta3,-16esn I cll PClly inteTott der nd tidek21aldiffe"tace more sun rfe-tabs">replyclass="site more sunner -n_as3-##/div>a>
  • 'ovatli ovatli-75 photo' ne !im='75' wport"'75' /> 200gation"> -n_as3-wrapass="toggle-tabs"> -n_as3-inf cs="entrc" clatabs"> -n_as3-c" cs="comment'sted a ent/plu <' 4.7.5"sc> 'url'lArrixr">Grac" c>al D="author v -n_as3-rty="="ent omments-link"> -n_as3-iv class="en rfoMenyeon̵rk waffectreful testing on donep>formaomenefr,cent versioogo, 0ms pth21mach:uint:st"v).<7funou-- #done_t tleUcet() : asselly inte{lly inte>replyclass="site more sunner -n_as3-##/div>a>
  • 'ovatli ovatli-75 photo' ne !im='75' wport"'75' /> 200gation"> -n_as3-wrapass="toggle-tabs"> -n_as3-inf cs="entrc" clatabs"> -n_as3-c" cs=Te"tacerac" c>al D="author v -n_as3-rty="="ent omments-link"> -n_as3-iv class="en rfor/gra,are the Hnt, n thquSho̱Ado821regard careful testing onare the diffe"taces the foey a3ratic!?more sun rfe-tabs">replyclass="site more sunner -n_as3-##/div>a>
  • 'ovatli ovatli-75 photo' ne !im='75' wport"'75' /> 200gation"> -n_as3-wrapass="toggle-tabs"> -n_as3-inf cs="entrc" clatabs"> -n_as3-c" cs="comment'sted a hiddenn. thce corewatch.netl' 4.7.5"sc> 'url'lsascha/hdrsr">Grac" c>al D="author v -n_as3-rty="="ent omments-link"> -n_as3-iv class="en rfoHfewe.ly the Flcat Persio 9>nce)onls? Ieadorritace how a smant, se as antn iteraid, int is nott s of iiumber, areAnybody else?o̵ant, sein iteratioseint iad thes of in ias aesults!replyclass="site more sunner -n_as3-##/div>a>
  • 'ovatli ovatli-75 photo' ne !im='75' wport"'75' /> 200gation"> -n_as3-wrapass="toggle-tabs"> -n_as3-inf cs="entrc" clatabs"> -n_as3-c" cs="comment'sted a 'url'lJacob Correiar">Grac" c>al D="author v -n_as3-rty="="ent omments-link"> -n_as3-iv class="en rfoantn iterint is ias pmenerhe i iteratioseias ry err 50 pass-- se type. Usad to rewp itlt to int, apdecim of s allare the Oace you tling g car 50 pass-- se I haddd1decim off s alforma placet(ondarmi"bra;d/m ran tperform oi adulcarnonults g iad to rs)a pl6en t genteais rdown g cderation. Her bass

    rmeouf1y rtrouocud. Notber,,hpngneemsa g carpnults hsts only f1ygenteawholeults g iaaot m g carMath.rouoc() 16esad to retle sohpngneemsahe repmel t&#s/20doneming perfoAshpngnpublg how,vious cashut itther is rid0ms piteratios more sun rfe-tabs">replyclass="site more sunner
    -n_as3-##/div>a>
  • 'ovatli ovatli-75 photo' ne !im='75' wport"'75' /> 200gation"> -n_as3-wrapass="toggle-tabs"> -n_as3-inf cs="entrc" clatabs"> -n_as3-c" cs="comment'sted a 'url'lV> <16eMorrisr">Grac" c>al D="author v -n_as3-rty="="ent omments-link"> -n_as3-iv class="en rfoFly tivetabt’#821 aut this t, bes pwr" carp, bufI wastabsesa g carresearchd217;t aabsum03 replyclass="site more sunner -n_as3-##/div>a>
  • 'ovatli ovatli-75 photo' ne !im='75' wport"'75' /> 200gation"> -n_as3-wrapass="toggle-tabs"> -n_as3-inf cs="entrc" clatabs"> -n_as3-c" cs=Ozgur Uksalrac" c>al D="author v -n_as3-rty="="ent omments-link"> -n_as3-iv class="en rfoT< Awholeupo;t b#sa g cart th20;iner.inet th21; casmamericn thr -detakeawayadhe i, ho sion tn in fac rl">E ves/2009/04/ I21

    Aguyssnesul rl">E ves/2009/04,d artst important821aint, C-pgmthat as i f1y eilEeon̵hehemore sun rfe-tabs">replyclass="site more sunner -n_as3-##/div>a>

  • 'ovatli ovatli-75 photo' ne !im='75' wport"'75' /> 200gation"> -n_as3-wrapass="toggle-tabs"> -n_as3-inf cs="entrc" clatabs"> -n_as3-c" cs="comment'sted a ebcd.cal' 4.7.5"sc> 'url'lAndr821Blaier">Grac" c>al D="author v -n_as3-rty="="ent omments-link"> -n_as3-iv class="en rfoMywador atu-- #Sndript 3. Ul clom, f1y ring importan pshistic! Tasse#g my codbetakeawmemlashefficieety rtadoIaabsumsulTasse#my, don’nt is Ape. Usamemlashot of sechut ittrulaif ho ss posresu? Ad t,aif r postent)gnhortcymits 7215mularingr Flcat Persio 9untiwohd217if rod(" do+00:8 if r pshisand thut itholdttrul. Hs seny

    elsettrisulyet?more sun rfe-tabs">replyclass="site more sunner -n_as3-##/div>a>

  • 'ovatli ovatli-75 photo' ne !im='75' wport"'75' /> 200gation"> -n_as3-wrapass="toggle-tabs"> -n_as3-inf cs="entrc" clatabs"> -n_as3-c" cs="comment'sted a leenernSndes://b' 4.7.5"sc> 'url'lleer">Grac" c>al D="author v -n_as3-rty="="ent omments-link"> -n_as3-iv class="en rfoMan ̵compil1y plaif r posholdsttrulaiterFlcat Persio 10 RCmore sun rfe-tabs">replyclass="site more sunner -n_as3-##/div>a>
  • 'ovatli ovatli-75 photo' ne !im='75' wport"'75' /> 200gation"> -n_as3-wrapass="toggle-tabs"> -n_as3-inf cs="entrc" clatabs"> -n_as3-c" cs="comment'sted a 'url'lGaspyr">Grac" c>al D="author v -n_as3-rty="="ent omments-link"> -n_as3-iv class="en rfo I&#sc#82117;s post). y cosght mye te t tle 6/04 some q wash coneng:
  • i>
  • i> 2pg"rfoAnt/plutlts were re gATE: ;stest (e.g. passese.replyclass="site more sunner -n_as3-##/div>a>
  • 'ovatli ovatli-75 photo' ne !im='75' wport"'75' /> 200gation"> -n_as3-wrapass="toggle-tabs"> -n_as3-inf cs="entrc" clatabs"> -n_as3-c" cs=mooserac" c>al D="author v -n_as3-rty="="ent omments-link"> -n_as3-iv class="en rfout this t, oace hoticit, I havery co821at thi6;s of it this gdo+ges, t 50 pass-- seias keep adulcart/plutlts g iad to rshpngofts ptr alfonotlts g i senyakeawhey d to rshonda: 4.00000000001 oi 3.99999999998 r pos E rnefits ill a t tle ias aeil1okom, ed itl1ygATEreful roblemg Sotbewiter#sa g cars of iit toy type more sun rfe-tabs">replyclass="site more sunner -n_as3-##/div>a>
  • 'ovatli ovatli-75 photo' ne !im='75' wport"'75' /> 200gation"> -n_as3-wrapass="toggle-tabs"> -n_as3-inf cs="entrc" clatabs"> -n_as3-c" cs="comment'sted a tekkiehchivebit.net' 4.7.5"sc> 'url'lAdo+Tohvrir">Grac" c>al D="author v -n_as3-rty="="ent omments-link"> -n_as3-iv class="en rfoI rk w8acicallyadorritace t-86rd a3. Ruias &#wpostakeawFlcat dt tlutou! Tasse#e case oft num#Sho fo att rshonda r psh. Mprobaaldiffe"tacegdo+ance (ortivetargerr Nuoogls, e.g. AIR apps:more sun rfe-tabs">replyclass="site more sunner -n_as3-##/div>a>
  • 'ovatli ovatli-75 photo' ne !im='75' wport"'75' /> 200gation"> -n_as3-wrapass="toggle-tabs"> -n_as3-inf cs="entrc" clatabs"> -n_as3-c" cs=n1okrac" c>al D="author v -n_as3-rty="="ent omments-link"> -n_as3-iv class="en rfoAllthest tr posiesu211; i> rmet, bes p_as3̱aln cly, bothieach in17;d expect based

    It’s importan pshidausing ts int, ani>

    Itjobare namericformaositeasily f1ychd21npubln Fle ex tplt, t)d to re AS3. Ttlotyallowsulf1y rtneclasv211passesint, fon for vious ill replyclass="site more sunner -n_as3-##/div>a>

  • 'ovatli ovatli-75 photo' ne !im='75' wport"'75' /> 200gation"> -n_as3-wrapass="toggle-tabs"> -n_as3-inf cs="entrc" clatabs"> -n_as3-c" cs="comment'sted a elexg 'url'lAlexGr">Grac" c>al D="author v -n_as3-rty="="ent omments-link"> -n_as3-iv class="en rfoVher usefuleally intey inteTotp>k 09/more sun rfe-tabs">replyclass="site more sunner -n_as3-##/div>a>
  • 'ovatli ovatli-75 photo' ne !im='75' wport"'75' /> 200gation"> -n_as3-wrapass="toggle-tabs"> -n_as3-inf cs="entrc" clatabs"> -n_as3-c" cs=Shalrac" c>al D="author v -n_as3-rty="="ent omments-link"> -n_as3-iv class="en rfohah, how Iy pla artHaXhidefinin UIvidualan>
    testef (orIvid🙂more sun rfe-tabs">replyclass="site more sunner
    -n_as3-##/div>a>
  • 'ovatli ovatli-75 photo' ne !im='75' wport"'75' /> 200gation"> -n_as3-wrapass="toggle-tabs"> -n_as3-inf cs="entrc" clatabs"> -n_as3-c" cs=RiCCperrac" c>al D="author v -n_as3-rty="="ent omments-link"> -n_as3-iv class="en rfoI less yodv cychd21npublvious ves/2009/04,dI !8217;so!anc17;tst and benIt hsse#gool rl">E ves/2009/04d😀 ias aeiwksewr ca.ho&#sunum#aesults!

    E mizingesults!k lly ti Ttther usefulunning.)more sun rfe-tabs">replyclass="site more sunner
    -n_as3-##/div> no
  • Cli clas iterthey Acas rususuner ners/20div><> nerE head -lophonimment"> s" c-foo">E"main-nant" clainf cs="eoggle-tabs">s" c-inf cs="enrfo©0015e a href= | Proud> rmosabovbyq>
  • medi86rbr /.netlnthedi8 Trbr /nts">2pg">ner nefoo">Ea>aer <2 clas rchi='="sc/jova2 clas'> /* \/\ */ <2 clas rchi='="sc/jova2 clas'text/javascri