* Improved Swaping Method In sort.c
@ 2008-04-30 18:09 Soumyadip Das Mahapatra
2008-04-30 18:17 ` Andi Kleen
2008-04-30 18:19 ` Ray Lee
0 siblings, 2 replies; 3+ messages in thread
From: Soumyadip Das Mahapatra @ 2008-04-30 18:09 UTC (permalink / raw)
To: linux-kernel
Thanks Jan,
Actually it should be
static void u32_swap(void *a, void *b)
{
*(u32 *)b ^= *(u32 *)a;
*(u32 *)a ^= *(u32 *)b;
*(u32 *)b ^= *(u32 *)a;
}
But this code saves memory equaling two integers. What about that ?
Meet people who discuss and share your passions. Go to http://in.promos.yahoo.com/groups/bestofyahoo/
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Improved Swaping Method In sort.c
2008-04-30 18:09 Improved Swaping Method In sort.c Soumyadip Das Mahapatra
@ 2008-04-30 18:17 ` Andi Kleen
2008-04-30 18:19 ` Ray Lee
1 sibling, 0 replies; 3+ messages in thread
From: Andi Kleen @ 2008-04-30 18:17 UTC (permalink / raw)
To: Soumyadip Das Mahapatra; +Cc: linux-kernel
Soumyadip Das Mahapatra <dip_kernel@yahoo.co.in> writes:
> But this code saves memory equaling two integers. What about that ?
It won't because the compiler uses registers not memory for the
temporary of course. In fact your version will cause more memory
accesses because the compiler doesn't know that a and b do not alias
each other, so it cannot cache the immediate value and has to always
go through memory. The difference will not be large on x86 because L1
cache accesses are reasonably fast, but they're still much slower than
a register access.
-Andi
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Improved Swaping Method In sort.c
2008-04-30 18:09 Improved Swaping Method In sort.c Soumyadip Das Mahapatra
2008-04-30 18:17 ` Andi Kleen
@ 2008-04-30 18:19 ` Ray Lee
1 sibling, 0 replies; 3+ messages in thread
From: Ray Lee @ 2008-04-30 18:19 UTC (permalink / raw)
To: Soumyadip Das Mahapatra; +Cc: linux-kernel
On Wed, Apr 30, 2008 at 11:09 AM, Soumyadip Das Mahapatra
<dip_kernel@yahoo.co.in> wrote:
> Thanks Jan,
> Actually it should be
> static void u32_swap(void *a, void *b)
> {
> *(u32 *)b ^= *(u32 *)a;
> *(u32 *)a ^= *(u32 *)b;
> *(u32 *)b ^= *(u32 *)a;
> }
> But this code saves memory equaling two integers. What about that ?
The above prevents gcc from storing the intermediary in a register. So
you're trading fast register access for slow memory access.
Really, you shouldn't be prosing optimizations unless you do two
things: check the output of gcc (on at least one platform, but really
should be several), and include timings of before/after.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2008-04-30 18:19 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-04-30 18:09 Improved Swaping Method In sort.c Soumyadip Das Mahapatra
2008-04-30 18:17 ` Andi Kleen
2008-04-30 18:19 ` Ray Lee
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®