The Daily Static
  The Daily Static
UF Archives
Register
UF Membership
Ad Free Site
Postcards
Community

Geekfinder
UFie Gear
Advertise on UF

Forum Rules
& FAQ


Username

Password


Create a New Account

 
 

Back to UserFriendly Strip Comments Index

recursive -> iterative by dennismv2004-12-21 02:24:43
  I don't have time to give a full answer... by Liava2004-12-21 03:55:22
    Qsort by dennismv2004-12-21 04:15:00
      I'd still call that a recursive solution, though by Liava2004-12-21 04:30:46
        recursion by dennismv2004-12-21 04:50:19
          Well, the algorithm is as follows: by Liava2004-12-21 11:46:07
            ok by dennismv2004-12-21 19:34:10
              Not what I meant by Liava 2004-12-21 22:05:31

By doing any of this, you will _increase_ the number of heap allocations. My point was that, if you make a struct, then you will reduce the number of allocations from pushing every variable, one at a time, onto your stack. Using the heap is way slower than just using the built-in stack.

This type of thing *will* slow down your algorithm, unless you can find patterns in the algorithm.

E.g., quicksort has two recursive calls on the bottom that look like this:

quicksort(array, left, pivot - 1);
quicksort(array, pivot + 1, right);

You'll notice that, aside from as parameters, left, right, and pivot will never be used again (since this is the last part of the function). Also, array never changes, so there's no point in storing that. Therefore, I could modify quicksort to not make a recursive call by doing this instead:

push(new pair(0, array.count - 1));

while (!stack_empty) {
    pop
    // Do quicksort on popped pair
    push(new pair(left, pivot - 1));
    push(new pair(pivot + 1, right));
}

*If* you implement that correctly, it *might* be faster than the pure recursive version. However, if every pair is a heap allocation that is pushed onto the heap, I'd say it's not going to be good enough. Chances are, you'd need a vector with a large pre-allocated buffer that simulated a stack to actually see a performance improvement.

Keep in mind, however, that pushing/popping on the CPU's stack are usually single instructions, plus the stack is likely to be hot on the cache, and a function call is very low overhead. Finally, you can probably get better performance by marking the function fastcall (pass parameters in registers), rather than trying to tune things like this.

[ Reply ]
                Much of this could be avoided. by williamashbless2004-12-21 23:32:22

 

[Todays Cartoon Discussion] [News Index]

Come get yer ARS (Account Registration System) Source Code here!
All images, characters, content and text are copyrighted and trademarks of J.D. Frazer except where other ownership applies. Don't do bad things, we have lawyers.
UserFriendly.Org and its operators are not liable for comments or content posted by its visitors, and will cheerfully assist the lawful authorities in hunting down script-kiddies, spammers and other net scum. And if you're really bad, we'll call your mom. (We're not kidding, we've done it before.)