Skip to content

Instantly share code, notes, and snippets.

@pnkfelix
Created January 4, 2013 18:07
Show Gist options
  • Save pnkfelix/4454622 to your computer and use it in GitHub Desktop.
Save pnkfelix/4454622 to your computer and use it in GitHub Desktop.
function ComputeSliceBounds(len, id, n) {
// Computes the bounds for slice |id| of |len| items, assuming |n|
// total slices. If len is not evenly divisible by n, then the
// initial threads may have a bit more work.
var slice = ((len + n - 1) / n) | 0; // i.e. ceil(len / n).
var prevDone = slice * id; // work distributed to threads before this one.
var start = (prevDone >= len) ? len : prevDone; // clamp start by len.
var end = (start + slice) > len ? len : (start + slice); // clamp end by len.
return [start, end];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment