Re: The Why's of Loop Unrolling
BY <[email protected]> Fri, 10 Nov 2006 13:13:55 -0500
| Newsgroups | gmane.comp.web.dom.wdf |
|---|---|
| Message-ID | <[email protected]> |
Chris, Edwin is correct. Since JS is interpreted, not compiled, any low-level optimizations like loop unrolling will not produce conclusive results. The link you reference actually accomplishes gains by reducing operations / loop (using variables with precalc'd values). Loop unrolling is actually getting rid of a loop by writing the code out for each iteration. Not the same thing (and usually only done for low-level optimization where the number of iterations is small and known ahead of time). Duff's Device is an interesting combination of loop with case statement though no obvious use comes to mind. -- Ben V.Y. On 10/23/06, Chris <[email protected]> wrote: > Hi Edwin, I had the same objections when I first saw the performance > claims of loop unrolling in Javascript - but it really works. I suggest > you try it, and see for yourself. > > The performance benefit is also well-documented: > http://www.google.com/search?q=javascript+loop+unrolling > > If you rather not write your own unrolled loop, the second to last test > has one for you to try: > http://home.earthlink.net/~kendrasg/info/js_opt/jsOptMain.html > > Click the second-to-last test button (the last one is miswired to the > previous test..) and you'll see the performance numbers speak for > themselves. My post here is an investigation into why it does work. > -Chris > > > Edwin Martin wrote: > > Chris schreef: > > > >> I came across some interesting information today that some here might > >> find interesting. > >> > >> Often in discussing Javascript loop optimization, loop unrolling and > >> Duff's Device come up: > >> http://home.earthlink.net/~kendrasg/info/js_opt/jsOptMain.html > >> (bottom of page) > >> > >> And often shortly thereafter, the "But why...?" question arises, and I'm > >> yet to find a good answer, just guesses. > >> > > The only purpose of loop unrolling is preventing jumps (from the bottom > > of the loop to the top of the loop). > > > > This has it uses in assembly, where speed it very critical. > > > > It has almost no use in JavaScript. Because the JavaScript code gets > > bigger with loop unrolling, it takes longer to download and probably > > makes your site slower instead of faster. Loop unrolling is also a > > maintanance nightmare. > > > > > >> 2) A typical floating-point operation (for example) requires 4 CPU > >> ticks, but a new one can be fed to the pipeline each tick. That means > >> executing f++; 5 times would normally take 5*4=20 ticks, but with > >> Out-Of-Order it can be done in 8 ticks. Note that from what I've read, > >> Pentium 4 series CPUs have even longer pipelines, some ops taking over > >> 20 ticks. > >> > >> > > But this is of no use in JavaScript. JavaScript is interpreted, not > > compiled to machine code. > > > > You mix up two things of different scales. Like you're looking at a > > globe trying to optimize city traffic. > > > > Because JavaScript is interpreted, you really don't know how the CPU > > 'sees' your code. If you do happen to optimize your JavaScript for a > > certain CPU, it is almost certainly not worth the time spend (the gains > > will be minimal). > > > > To optimize JavaScript, there are other and better tricks. > > > > Edwin Martin > > > > > > Unsubscribe > [email protected] > > List info > http://www.quirksmode.org/dom/list.html > Yahoo! Groups Links > > > > >