summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkeios <keios.cn@gmail.com>2006-10-03 01:13:49 -0700
committerLinus Torvalds <torvalds@g5.osdl.org>2006-10-03 08:03:41 -0700
commitd3717bdf8f08a0e1039158c8bab2c24d20f492b6 (patch)
tree9c8617cb3f58faace4941a0aa09ea1820d287b7c
parentffc5089196446c08d9a005cf0dd7cab18d119606 (diff)
[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: keios <keios.cn@gmail.com> Acked-by: Matt Mackall <mpm@selenic.com> Acked-by: Zou Nan hai <nanhai.zou@intel.com> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
-rw-r--r--lib/sort.c10
1 files changed, 5 insertions, 5 deletions
diff --git a/lib/sort.c b/lib/sort.c
index 5f3b51ffa1dc..488788b341cb 100644
--- a/lib/sort.c
+++ b/lib/sort.c
@@ -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)
@@ -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)