A few range questions

Charles Smith via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jan 5 13:48:20 PST 2016


On Tuesday, 5 January 2016 at 19:42:42 UTC, Ali Çehreli wrote:
> You are traversing the array per index value (1000 times), 
> which can be done once up front:

Good point. I guess I assumed `map!` was magic.


>
>     // Now they get their own arrays:
>     auto benchmarks = benchmark!(() => algorithmSort(arr.dup),
>                                  () => 
> countingSort(arr.dup))(1);
>

I'm not sure what this explicitly is doing... Are you defining an 
anonymous function? If so, that seems really useful.

>
>     uint[valueCount] hist;
>     arr.each!(a => ++hist[a]);
>
>     auto result = hist[].enumerate.map!(t => 
> t[0].repeat(t[1])).joiner;
>     // To make sure that we consume the lazy range:
>     return result.sum;
> }
> I think .joiner is what you're looking for.
>

Thanks a lot for this. Coming from a webdev background I'm 
ashamed I didn't guess `join`. That said, I don't think the 
example for `chain` should compile then. Just as an aside, I was 
searching the terms `concat` and `flatten ` when I was looking 
for this.

> > 3. It's kind of hard to compare performance because of (1),
> but is there
> > a better way to do this?
>
> I changed the code to pass each function a different array.
>
> My results with -O -inline -noboundscheck:
>
> Algorithm's Sort: 76 ms, 910 μs, and 8 hnsecs
> Counting Sort   : 21 ms, 563 μs, and 9 hnsecs
>
> Ali

Awesome



More information about the Digitalmars-d-learn mailing list