From: keios <keios.cn@gmail.com>
To: linux-kernel@vger.kernel.org
Cc: "Andrew Morton" <akpm@osdl.org>, "Matt Mackall" <mpm@selenic.com>
Subject: [PATCH] low performance of lib/sort.c , kernel 2.6.18
Date: Thu, 28 Sep 2006 23:18:45 +0800 [thread overview]
Message-ID: <76505a370609280818r3ffc9a4akf4cec6ed366d32e3@mail.gmail.com> (raw)
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>
-----
diff -Nraup a/lib/sort.c b/lib/sort.c
--- a/lib/sort.c 2006-09-20 11:42:06.000000000 +0800
+++ b/lib/sort.c 2006-09-27 21:26:38.000000000 +0800
@@ -49,15 +49,15 @@ void sort(void *base, size_t num, size_t
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
/* 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)
next reply other threads:[~2006-09-28 15:18 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-09-28 15:18 keios [this message]
2006-09-28 22:33 ` Matt Mackall
2006-09-28 22:34 ` Zou Nan hai
2006-09-29 1:56 ` keios
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=76505a370609280818r3ffc9a4akf4cec6ed366d32e3@mail.gmail.com \
--to=keios.cn@gmail.com \
--cc=akpm@osdl.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mpm@selenic.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®