Skip to content

Commit

Permalink
[PATCH] low performance of lib/sort.c
Browse files Browse the repository at this point in the history
It is a non-standard heap-sort algorithm implementation because the index
of child node is wrong .  The sort function still outputs right result, but
the performance is O( n * ( log(n) + 1 ) ) , about 10% ~ 20% worse than
standard algorithm.

Signed-off-by: keios <[email protected]>
Acked-by: Matt Mackall <[email protected]>
Acked-by: Zou Nan hai <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
keios authored and Linus Torvalds committed Oct 3, 2006
1 parent ffc5089 commit d3717bd
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions lib/sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ void sort(void *base, size_t num, size_t size,
void (*swap)(void *, void *, int size))
{
/* pre-scale counters for performance */
int i = (num/2) * size, n = num * size, c, r;
int i = (num/2 - 1) * size, n = num * size, c, r;

if (!swap)
swap = (size == 4 ? u32_swap : generic_swap);

/* heapify */
for ( ; i >= 0; i -= size) {
for (r = i; r * 2 < n; r = c) {
c = r * 2;
for (r = i; r * 2 + size < n; r = c) {
c = r * 2 + size;
if (c < n - size && cmp(base + c, base + c + size) < 0)
c += size;
if (cmp(base + r, base + c) >= 0)
Expand All @@ -69,8 +69,8 @@ void sort(void *base, size_t num, size_t size,
/* sort */
for (i = n - size; i >= 0; i -= size) {
swap(base, base + i, size);
for (r = 0; r * 2 < i; r = c) {
c = r * 2;
for (r = 0; r * 2 + size < i; r = c) {
c = r * 2 + size;
if (c < i - size && cmp(base + c, base + c + size) < 0)
c += size;
if (cmp(base + r, base + c) >= 0)
Expand Down

0 comments on commit d3717bd

Please sign in to comment.