4 ms·
The other two comments are right, but the context you might be missing is why you would unroll a loop. Due to cache behavior (and other low-level details), doin
by silentbicycle 16y ago
The other two comments are right, but the context you might be missing is why you would unroll a loop. Due to cache behavior (and other low-level details), doing something eight times and then checking if it needs to be done another eight times has less overhead than checking after each loop iteration. Why eight? It was probably a sweet spot in time vs. code size, at some point. (Processor caches are larger now.)
Duff's device just gets the (remainder of N/8) steps out of the way the first time through, then drops down to looping eight at a time. If it seems more complicated than that, you're probably overthinking it. It's "just" a creative abuse of C syntax, a bunch of offsets and gotos.
Sometimes low-level optimization like this makes a huge difference, but make sure it's a hotspot first, and that the compiler isn't already doing those things for you. Measurements will keep you objective.
Also, if you're doing a lot with C, check out Lua!
- philbo 16y agoThanks. Lua is on "my list" actually, Fortunately I am only forced to work in C for one particular project (sorry if that sounds negative, C-heads, I'm just a messy enough person to require garbage collection to stop me from messing up too bad).
- silentbicycle 16y agoLua is, IMHO, "Javascript done right". It's nice to have the option of writing C code but letting Lua do garbage-collection on common userdata. Lua replaced Python for me - it's a great language on its own. TCO and pattern matching (http://github.com/silentbicycle/tamale http://github.com/silentbicycle/tamale) go a long way.
- rbanffy 16y ago> make sure it's a hotspot first A wise friend of mine once said it's much easier to optimize a correct program than debugging a heavily optimized one.