System programming in D (Was: The God Language)

Iain Buclaw ibuclaw at ubuntu.com
Sat Dec 31 06:39:56 PST 2011


On 31 December 2011 13:05, Mike Wey <mike-wey at example.com> wrote:
> On 12/31/2011 02:59 AM, so wrote:
>>
>> On Sat, 31 Dec 2011 03:40:43 +0200, Iain Buclaw <ibuclaw at ubuntu.com>
>> wrote:
>>
>>> Take a pick of any examples posted on this ML. They are far better
>>> fit to use as a test bed. Ideally one that does number crunching and
>>> can't be easily folded away.
>>
>>
>> Well not them but another dummy function, i didn't think it would differ
>> this much.
>>
>
> real test() nothrow pure
>
>
>> real test() // test.d
>> real test() @inline // test_inl.d
>> {
>> real a=423123, b=432, c=10, d=100, e=4045, f=123;
>> a = a / b * c / d + e - f;
>> b = a / b * c / d + e - f;
>> c = a / b * c / d + e - f;
>> d = a / b * c / d + e - f;
>> e = a / b * c / d + e - f;
>> f = a / b * c / d + e - f;
>> a = a / b * c / d + e - f;
>> b = a / b * c / d + e - f;
>> c = a / b * c / d + e - f;
>> d = a / b * c / d + e - f;
>> e = a / b * c / d + e - f;
>> f = a / b * c / d + e - f;
>> a = a / b * c / d + e - f;
>> b = a / b * c / d + e - f;
>> c = a / b * c / d + e - f;
>> d = a / b * c / d + e - f;
>> e = a / b * c / d + e - f;
>> f = a / b * c / d + e - f;
>> return f;
>> }
>>
>> void main()
>> {
>> for(uint i=0; i<1_000_000_0; ++i)
>> test();
>> }
>
>
> When marking the function as pure and nothrow dmd is able to optimize the
> loop:
>
> .text._Dmain    segment
>        assume  CS:.text._Dmain
> _Dmain:
>                push    RBP
>                mov     RBP,RSP
>                xor     EAX,EAX
> L6:             inc     EAX
>                cmp     EAX,0989680h
>                jb      L6
>                xor     EAX,EAX
>                pop     RBP
>                ret
> .text._Dmain    ends
>
>
> --
> Mike Wey

Yep, as I've mentioned earlier, the function has no side effects and
it's return value is not used, hence can be optimised away completely.

-- 
Iain Buclaw

*(p < e ? p++ : p) = (c & 0x0f) + '0';


More information about the Digitalmars-d mailing list