Re: How do I iterate over enum members at runtime?

2011-04-09 Thread Cliff Hudson
You could probably make a template out of the ugly __traits invocation as
well?

On Sat, Apr 9, 2011 at 3:37 PM, Andrej Mitrovic
wrote:

> On 4/10/11, Jonathan M Davis  wrote:
> >> What the.. I was sure mixin wouldn't work in a foreach loop.
> >
> > Whyever not? mixins work in most places. I believe that the problem is
> that
> > they have to be a whole expression or statement rather than just a piece
> of
> > one, so sometimes you can't use a mixin for something small and have to
> put
> > more of the code in a mixin, but string mixins do work in most places.
> >
> > - Jonathan M Davis
> >
>
> Well in this case it works, but it's not always so easy. For example:
>
> import std.conv;
> enum Metrics : int
> {
>val0,
>val1,
>val2
> }
>
> void foo(int m)
> {
> }
>
> void main()
> {
>foreach (index; 0..3)
>   {
>   foo(mixin("Metrics.val" ~ to!string(index)));
>   }
> }
>
> So even though you might think "hey, it's obvious index in this case
> can never be anything other than 0, 1, or 2", you still won't be able
> to compile this. CTFE is a tricky thing.
>


Re: speed of low-level C funcs: example of memmove

2011-04-09 Thread Cliff Hudson
If you take a look at the implementation of memmove (grab the std C lib
source) you'll see a rather optimized assembly loop which is very smart
about doing machine-word aligned moves, and using processor block-copy
instructions.  I suspect that is the reason you see the difference.  For
smaller data sets, you won't see as much gain, if any, because it can't take
advantage of those other semantics to the same degree.

- Cliff

On Sat, Apr 9, 2011 at 10:17 AM, spir  wrote:

> On 04/09/2011 07:08 PM, spir wrote:
>
>> Hello,
>>
>> To insert of delete an array slice, I tried to use C's memmove, thinking
>> it
>> would be far faster than "manually" copying bit per bit (by any kind of
>> magic).
>> But I still wrote a D versions just to check what the actual speed gain
>> is. To
>> my great surprise, the C-memmove and D-manual versions perform *exactly*
>> at the
>> same speed (considering measure imprecision).
>> Note: this remains true when elements are bigger; speed slows down slowly
>> (eg
>> dchar's take only 1/3 more time).
>>
>
> Correction: memmove can be from 5 to 10 times faster on big arrays. For
> instance, with 1_000_000 char array:
>
>
> void chrono () {
>char[] s;
>d_time t;
>enum N = 100;
>
>// C memmove
>s = ("0".mult(1_000_000)).dup;
>
>t = getUTCtime();
>foreach (_ ; 0..N) {
>s.shiftEndPartC(3,5);
>s[3..5] = "--";
>s.shiftEndPartC(5,3);
>}
>t = getUTCtime() - t;
>writefln("C time: %s", t);
>
>// D manual
>s = ("0".mult(1_000_000)).dup;
>
>t = getUTCtime();
>foreach (_ ; 0..N) {
>s.shiftEndPartD(3,5);
>s[3..5] = "--";
>s.shiftEndPartD(5,3);
>}
>t = getUTCtime() - t;
>writefln("D time: %s", t);
> }
>
> --
> _
> vita es estrany
> spir.wikidot.com
>
>