Re: Fastest way to zero a slice of memory

2018-03-05 Thread kinke via Digitalmars-d-learn

On Sunday, 4 March 2018 at 15:23:41 UTC, Nordlöw wrote:

When zeroing a slice of memory (either stack or heap) such as

enum n = 100;
ubyte[n] chunk;

should I use `memset` such as

memset(chunk.ptr, 0, n/2); // zero first half

or an array assignment such as

chunk[0 .. n/2] = 0; // zero first half

or are they equivalent in release mode?

Further, does it depend on whether the slice length is known at 
compile-time or not?


I'd recommend not concerning yourself with such low-level 
optimizations and let the compiler do that for you, and only jump 
in yourself if profiling/benchmarking shows that there's a 
bottleneck, and prefer nice readable code otherwise.
E.g., LDC will lower `chunk[0 .. n/2] = 0` to a memset() call if 
the length is unknown at compile-time, and otherwise replace it 
with good-looking assembly code (xor vector register and store it 
consecutively to memory): https://run.dlang.io/is/I6Fq9G (click 
on ASM and check the assembly for the two functions).


Re: Fastest way to zero a slice of memory

2018-03-04 Thread bauss via Digitalmars-d-learn

On Sunday, 4 March 2018 at 15:23:41 UTC, Nordlöw wrote:

When zeroing a slice of memory (either stack or heap) such as

enum n = 100;
ubyte[n] chunk;

should I use `memset` such as

memset(chunk.ptr, 0, n/2); // zero first half

or an array assignment such as

chunk[0 .. n/2] = 0; // zero first half

or are they equivalent in release mode?

Further, does it depend on whether the slice length is known at 
compile-time or not?


This is worth reading:

https://stackoverflow.com/questions/3654905/faster-way-to-zero-memory-than-with-memset


Fastest way to zero a slice of memory

2018-03-04 Thread Nordlöw via Digitalmars-d-learn

When zeroing a slice of memory (either stack or heap) such as

enum n = 100;
ubyte[n] chunk;

should I use `memset` such as

memset(chunk.ptr, 0, n/2); // zero first half

or an array assignment such as

chunk[0 .. n/2] = 0; // zero first half

or are they equivalent in release mode?

Further, does it depend on whether the slice length is known at 
compile-time or not?