All of lore.kernel.org
 help / color / mirror / Atom feed
* [KJ] what is best: memcpy or for
@ 2006-12-06  7:35 Daniel Marjamäki
  2006-12-06  8:34 ` Christophe Lucas
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Daniel Marjamäki @ 2006-12-06  7:35 UTC (permalink / raw)
  To: kernel-janitors

Hello!

I am wondering if there is any real difference between "for" and "memcpy"..

    int i, a[100], b[100];

    // method 1
    for (i = 0; i < 100; i++)
        a[i] = b[i];

    // method 2
    memcpy(a, b, sizeof(a));

Is there any difference in speed?

With a good compiler, the output should be the same for both methods,
shouldn't it?

Best regards,
Daniel
_______________________________________________
Kernel-janitors mailing list
Kernel-janitors@lists.osdl.org
https://lists.osdl.org/mailman/listinfo/kernel-janitors

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [KJ] what is best: memcpy or for
  2006-12-06  7:35 [KJ] what is best: memcpy or for Daniel Marjamäki
@ 2006-12-06  8:34 ` Christophe Lucas
  2006-12-06 14:13 ` Matthew Wilcox
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Christophe Lucas @ 2006-12-06  8:34 UTC (permalink / raw)
  To: kernel-janitors

Daniel Marjamäki (daniel.marjamaki@gmail.com) wrote:
> Hello!
> 
> I am wondering if there is any real difference between "for" and "memcpy"..
> 
>     int i, a[100], b[100];
> 
>     // method 1
>     for (i = 0; i < 100; i++)
>         a[i] = b[i];
> 
>     // method 2
>     memcpy(a, b, sizeof(a));
> 
> Is there any difference in speed?
> 
> With a good compiler, the output should be the same for both methods,
> shouldn't it?

Hi,

Perhaps I say mistake, but in kernel terms :

	http://lxr.linux.no/source/include/asm-i386/string.h?v=2.6.18#L203

regards,

	Christophe
_______________________________________________
Kernel-janitors mailing list
Kernel-janitors@lists.osdl.org
https://lists.osdl.org/mailman/listinfo/kernel-janitors

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [KJ] what is best: memcpy or for
  2006-12-06  7:35 [KJ] what is best: memcpy or for Daniel Marjamäki
  2006-12-06  8:34 ` Christophe Lucas
@ 2006-12-06 14:13 ` Matthew Wilcox
  2006-12-06 15:25 ` Neil Horman
  2006-12-07  4:59 ` Anupam Kapoor
  3 siblings, 0 replies; 5+ messages in thread
From: Matthew Wilcox @ 2006-12-06 14:13 UTC (permalink / raw)
  To: kernel-janitors

On Wed, Dec 06, 2006 at 08:35:41AM +0100, Daniel Marjam?ki wrote:
> I am wondering if there is any real difference between "for" and "memcpy"..
> 
>     int i, a[100], b[100];
> 
>     // method 1
>     for (i = 0; i < 100; i++)
>         a[i] = b[i];
> 
>     // method 2
>     memcpy(a, b, sizeof(a));
> 
> Is there any difference in speed?
> 
> With a good compiler, the output should be the same for both methods,
> shouldn't it?

Yes, gcc has idiom-recognising techniques for that.  Possibly not in the
version you're using, though.  In any case, the memcpy() gives gcc more
information (that a and b do not overlap) that it would otherwise have
to deduce for itself (it can normally do this, particularly for the
example you gave).
_______________________________________________
Kernel-janitors mailing list
Kernel-janitors@lists.osdl.org
https://lists.osdl.org/mailman/listinfo/kernel-janitors

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [KJ] what is best: memcpy or for
  2006-12-06  7:35 [KJ] what is best: memcpy or for Daniel Marjamäki
  2006-12-06  8:34 ` Christophe Lucas
  2006-12-06 14:13 ` Matthew Wilcox
@ 2006-12-06 15:25 ` Neil Horman
  2006-12-07  4:59 ` Anupam Kapoor
  3 siblings, 0 replies; 5+ messages in thread
From: Neil Horman @ 2006-12-06 15:25 UTC (permalink / raw)
  To: kernel-janitors

On Wed, Dec 06, 2006 at 08:35:41AM +0100, Daniel Marjamäki wrote:
> Hello!
> 
> I am wondering if there is any real difference between "for" and "memcpy"..
> 
>     int i, a[100], b[100];
> 
>     // method 1
>     for (i = 0; i < 100; i++)
>         a[i] = b[i];
> 
>     // method 2
>     memcpy(a, b, sizeof(a));
> 
> Is there any difference in speed?
> 
> With a good compiler, the output should be the same for both methods,
> shouldn't it?
> 
> Best regards,
> Daniel
> _______________________________________________
> Kernel-janitors mailing list
> Kernel-janitors@lists.osdl.org
> https://lists.osdl.org/mailman/listinfo/kernel-janitors

I'm going to say memcpy is better, as the for loop makes the assumption of
normalized 32 bit registers.  memcpy will copy multiple bytes where it is able
to and more efficient to do so.  Furhter, on larger machines, where 4 bit
registers are available, memcpy will likely use 64 bit load/store if its assumed
faster.

Neil

-- 
/***************************************************
 *Neil Horman
 *Software Engineer
 *gpg keyid: 1024D / 0x92A74FA1 - http://pgp.mit.edu
 ***************************************************/
_______________________________________________
Kernel-janitors mailing list
Kernel-janitors@lists.osdl.org
https://lists.osdl.org/mailman/listinfo/kernel-janitors

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [KJ] what is best: memcpy or for
  2006-12-06  7:35 [KJ] what is best: memcpy or for Daniel Marjamäki
                   ` (2 preceding siblings ...)
  2006-12-06 15:25 ` Neil Horman
@ 2006-12-07  4:59 ` Anupam Kapoor
  3 siblings, 0 replies; 5+ messages in thread
From: Anupam Kapoor @ 2006-12-07  4:59 UTC (permalink / raw)
  To: kernel-janitors

On 12/6/06, Daniel Marjamäki <daniel.marjamaki@gmail.com> wrote:
> Hello!
>
> I am wondering if there is any real difference between "for" and "memcpy"..
>
>     int i, a[100], b[100];
>
>     // method 1
>     for (i = 0; i < 100; i++)
>         a[i] = b[i];
>
>     // method 2
>     memcpy(a, b, sizeof(a));
>
> Is there any difference in speed?
>
> With a good compiler, the output should be the same for both methods,
> shouldn't it?

in your example above yes. but i would wager that memcpy in general
would be much better. for example on 64bit machines, it can use 64bit
load/store operations. also, using memcpy, gives more info to the
compiler e.g. whether the regions overlap or not etc.

kind regards
anupam
-- 
In the beginning was the lambda, and the lambda was with Emacs, and
Emacs was the lambda.

_______________________________________________
Kernel-janitors mailing list
Kernel-janitors@lists.osdl.org
https://lists.osdl.org/mailman/listinfo/kernel-janitors

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2006-12-07  4:59 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-12-06  7:35 [KJ] what is best: memcpy or for Daniel Marjamäki
2006-12-06  8:34 ` Christophe Lucas
2006-12-06 14:13 ` Matthew Wilcox
2006-12-06 15:25 ` Neil Horman
2006-12-07  4:59 ` Anupam Kapoor

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.