Faster sort?

John Colvin via Digitalmars-d digitalmars-d at puremagic.com
Thu Apr 7 01:23:09 PDT 2016


On Thursday, 7 April 2016 at 07:57:11 UTC, Andrea Fontana wrote:
> On Wednesday, 6 April 2016 at 18:54:08 UTC, tsbockman wrote:
>> On Wednesday, 6 April 2016 at 08:15:39 UTC, Andrea Fontana 
>> wrote:
>>> Using ldmd2 -O -release -noboundscheck -inline sort.d && 
>>> ./sort
>>>  instead:
>>>
>>> 2 inputs:
>>> Faster: 0 hnsecs
>>> Phobos: 33 μs and 5 hnsecs
>>>
>>> ...
>>>
>>> 65536 inputs:
>>> Faster: 0 hnsecs (???)
>>> Phobos: 7 secs, 865 ms, 450 μs, and 6 hnsecs
>>
>> Can you share the benchmark code?
>>
>> "0 hnsecs" results generally mean that your test was too 
>> simple and/or didn't have any obvious side effects, and so the 
>> optimizer just removed it completely.
>
> A simple test just written:
>
>    Duration total;
>
>    foreach(_; 0..10000)
>    {
>       auto arr = generate!( () => uniform(0,2)).map!(x => 
> cast(bool)x).take(65536).array;
>       StopWatch sw;
>       sw.start;
>       boolSort(arr);
>       total += sw.peek.to!Duration;
>       sw.stop;
>
>    }
>
>
> andrea at ububocs:/tmp$ ./sort
> 0 hnsecs
>
> I don't think compiler can remove a random generated array...

But it definitely can eliminate an unused result. My prediction: 
you took an array and sorted it, then did nothing with the 
result, so it rightly concluded that there was no point doing the 
sort. In any given case the compiler could be removing some or 
all of the work.

Laborious approach that defeats the optimisations you don't want 
while keeping the ones you do:

% cat modA.d
float[] codeToBenchmark(int someParam, float[] someOtherParam)
{
     /* blah blah code */
}

% cat modB.d
// Do not import modA here

auto codeToBenchmark(int, float[]);

void main()
{
     // start loop and timers:
         codeToBenchmark(/*blah params */);
     // end timers and loop
}
% ldc2 -c <optimisation options here> modA.d
% ldc2 <opt options, less important> modA.o modB.d
% ./modB
<reliable results come out here>


I really need to write an article about bencharking in D...


More information about the Digitalmars-d mailing list