Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Shao Miller <sha0.miller@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: a = b or memset/cpy? |
| Date | 2012-02-07 18:58 -0500 |
| Organization | A noiseless patient Spider |
| Message-ID | <jgsdla$ol5$1@dont-email.me> (permalink) |
| References | <abb85a15-f68c-4e2f-8985-fe8b4945aca0@t8g2000yqg.googlegroups.com> |
On 2/7/2012 12:02, nroberts wrote:
> memset and memcpy are turning up in profiles a lot. I'd like to speed
> things up a bit.
>
You might find that the implementation actually translates a '= { 0
};'-style initializer into a call to 'memset'. An experiment might
reveal whether or not that's the case.
> Sometimes it is clear that using = to initialize a local would be
> better than memset. I might not gain anything, but at least there's a
> chance.
>
I'm not sure how you could gain anything unless the call to 'memset'
actually translates differently than a '= { 0 };'-style initializer.
Did you know that after all subobjects that are explicitly initialized
(by the initializer-list) have been so, the rest are initialized to what
they would have been had the object been declared with 'static' storage
duration? The whole containing object is thus "touched."
> However, can I gain performance improvements when zeroing out say some
> global element in an array like so:
>
> typedef struct x { int var0; char var1[20]; } X;
>
> X gX[30];
>
> void f(int slot)
> {
> X init = {0};
>
> gX[slot] = init;
>
> ...
> }
>
> vs.
> void f(int slot)
> {
> memset(&gX[slot], 0, sizeof(X));
>
> ...
> }
>
Well these aren't the same. The former initializes all sub-objects to
the "zeroey" values that would initialize a 'static'-storage-duration
object having the same type as the sub-object and having no explicit
initializer.
The latter fills the object with bytes with the 'unsigned char' value
'0', which is all-bits-zero.
In your example, the 'struct' type 'X' has an 'int' member. The object
representation of an 'int' can have padding bits that can be used any
way the implementation pleases.
If filling the padding bits with zeroes results in a trap representation
for an 'int', then you might be in for a surprise.
There are similar concerns for other types, including pointers, where a
null pointer value might not be all-bits-zero.
That is why I believe some people consider a '= { 0 };'-style
initializer to be more portable than 'memset'. If portability isn't a
concern, oh well.
> Normally I wouldn't look for a micro-optimization like this but I'm
> kind of stuck with the parameters I'm given.
Optmizing and making portable might not always be compatible. If you
have a particular set of implementations as your target(s), there might
be "compiler intrinsics" that you can use which are
implementation-defined extensions to C that could offer you speed
advantages.
For example, some Microsoft compilers offer '__movsd':
http://msdn.microsoft.com/en-us/library/9d196b9h.aspx
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
a = b or memset/cpy? nroberts <roberts.noah@gmail.com> - 2012-02-07 09:02 -0800
Re: a = b or memset/cpy? Jens Gustedt <jens.gustedt@loria.fr> - 2012-02-07 18:41 +0100
Re: a = b or memset/cpy? James Kuyper <jameskuyper@verizon.net> - 2012-02-07 13:40 -0500
Re: a = b or memset/cpy? Jens Gustedt <jens.gustedt@loria.fr> - 2012-02-07 20:09 +0100
Re: a = b or memset/cpy? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-02-07 11:37 -0800
Re: a = b or memset/cpy? Shao Miller <sha0.miller@gmail.com> - 2012-02-07 18:35 -0500
Re: a = b or memset/cpy? nroberts <roberts.noah@gmail.com> - 2012-02-07 11:07 -0800
Re: a = b or memset/cpy? blp@cs.stanford.edu (Ben Pfaff) - 2012-02-07 11:14 -0800
Re: a = b or memset/cpy? nroberts <roberts.noah@gmail.com> - 2012-02-07 11:41 -0800
Re: a = b or memset/cpy? Shao Miller <sha0.miller@gmail.com> - 2012-02-07 18:39 -0500
Re: a = b or memset/cpy? "Scott Fluhrer" <sfluhrer@ix.netcom.com> - 2012-02-07 14:14 -0500
Re: a = b or memset/cpy? Shao Miller <sha0.miller@gmail.com> - 2012-02-07 18:58 -0500
Re: a = b or memset/cpy? Shao Miller <sha0.miller@gmail.com> - 2012-02-07 21:58 -0500
Re: a = b or memset/cpy? Jens Gustedt <jens.gustedt@loria.fr> - 2012-02-09 21:34 +0100
Re: a = b or memset/cpy? Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-02-07 20:34 -0500
Re: a = b or memset/cpy? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-02-08 18:43 +0000
Re: a = b or memset/cpy? jgk@panix.com (Joe keane) - 2012-02-08 20:05 +0000
Re: a = b or memset/cpy? Ian Collins <ian-news@hotmail.com> - 2012-02-09 09:18 +1300
Re: a = b or memset/cpy? Tim Prince <tim@nospamcomputer.org> - 2012-02-14 08:30 -0500
csiph-web