Commit d3717bdf authored by keios's avatar keios Committed by Linus Torvalds

[PATCH] low performance of lib/sort.c

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: default avatarkeios <keios.cn@gmail.com>
Acked-by: default avatarMatt Mackall <mpm@selenic.com>
Acked-by: default avatarZou Nan hai <nanhai.zou@intel.com>
Signed-off-by: default avatarAndrew Morton <akpm@osdl.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@osdl.org>
parent ffc50891
...@@ -49,15 +49,15 @@ void sort(void *base, size_t num, size_t size, ...@@ -49,15 +49,15 @@ void sort(void *base, size_t num, size_t size,
void (*swap)(void *, void *, int size)) void (*swap)(void *, void *, int size))
{ {
/* pre-scale counters for performance */ /* 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) if (!swap)
swap = (size == 4 ? u32_swap : generic_swap); swap = (size == 4 ? u32_swap : generic_swap);
/* heapify */ /* heapify */
for ( ; i >= 0; i -= size) { for ( ; i >= 0; i -= size) {
for (r = i; r * 2 < n; r = c) { for (r = i; r * 2 + size < n; r = c) {
c = r * 2; c = r * 2 + size;
if (c < n - size && cmp(base + c, base + c + size) < 0) if (c < n - size && cmp(base + c, base + c + size) < 0)
c += size; c += size;
if (cmp(base + r, base + c) >= 0) if (cmp(base + r, base + c) >= 0)
...@@ -69,8 +69,8 @@ void sort(void *base, size_t num, size_t size, ...@@ -69,8 +69,8 @@ void sort(void *base, size_t num, size_t size,
/* sort */ /* sort */
for (i = n - size; i >= 0; i -= size) { for (i = n - size; i >= 0; i -= size) {
swap(base, base + i, size); swap(base, base + i, size);
for (r = 0; r * 2 < i; r = c) { for (r = 0; r * 2 + size < i; r = c) {
c = r * 2; c = r * 2 + size;
if (c < i - size && cmp(base + c, base + c + size) < 0) if (c < i - size && cmp(base + c, base + c + size) < 0)
c += size; c += size;
if (cmp(base + r, base + c) >= 0) if (cmp(base + r, base + c) >= 0)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment