diff options
author | keios <keios.cn@gmail.com> | 2006-10-03 04:13:49 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@g5.osdl.org> | 2006-10-03 11:03:41 -0400 |
commit | d3717bdf8f08a0e1039158c8bab2c24d20f492b6 (patch) | |
tree | 9c8617cb3f58faace4941a0aa09ea1820d287b7c | |
parent | ffc5089196446c08d9a005cf0dd7cab18d119606 (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.c | 10 |
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, | |||
49 | void (*swap)(void *, void *, int size)) | 49 | void (*swap)(void *, void *, int size)) |
50 | { | 50 | { |
51 | /* pre-scale counters for performance */ | 51 | /* pre-scale counters for performance */ |
52 | int i = (num/2) * size, n = num * size, c, r; | 52 | int i = (num/2 - 1) * size, n = num * size, c, r; |
53 | 53 | ||
54 | if (!swap) | 54 | if (!swap) |
55 | swap = (size == 4 ? u32_swap : generic_swap); | 55 | swap = (size == 4 ? u32_swap : generic_swap); |
56 | 56 | ||
57 | /* heapify */ | 57 | /* heapify */ |
58 | for ( ; i >= 0; i -= size) { | 58 | for ( ; i >= 0; i -= size) { |
59 | for (r = i; r * 2 < n; r = c) { | 59 | for (r = i; r * 2 + size < n; r = c) { |
60 | c = r * 2; | 60 | c = r * 2 + size; |
61 | if (c < n - size && cmp(base + c, base + c + size) < 0) | 61 | if (c < n - size && cmp(base + c, base + c + size) < 0) |
62 | c += size; | 62 | c += size; |
63 | if (cmp(base + r, base + c) >= 0) | 63 | if (cmp(base + r, base + c) >= 0) |
@@ -69,8 +69,8 @@ void sort(void *base, size_t num, size_t size, | |||
69 | /* sort */ | 69 | /* sort */ |
70 | for (i = n - size; i >= 0; i -= size) { | 70 | for (i = n - size; i >= 0; i -= size) { |
71 | swap(base, base + i, size); | 71 | swap(base, base + i, size); |
72 | for (r = 0; r * 2 < i; r = c) { | 72 | for (r = 0; r * 2 + size < i; r = c) { |
73 | c = r * 2; | 73 | c = r * 2 + size; |
74 | if (c < i - size && cmp(base + c, base + c + size) < 0) | 74 | if (c < i - size && cmp(base + c, base + c + size) < 0) |
75 | c += size; | 75 | c += size; |
76 | if (cmp(base + r, base + c) >= 0) | 76 | if (cmp(base + r, base + c) >= 0) |