Tuple assignment

Russel Winder via Digitalmars-d digitalmars-d at puremagic.com
Sun May 10 01:18:11 PDT 2015


On Sat, 2015-05-09 at 13:07 -0700, Walter Bright via Digitalmars-d wrote:
> On 5/9/2015 10:16 AM, Russel Winder via Digitalmars-d wrote:
> > Python has tuple assignment so you see things like:
> > 
> >          previous, current = current, previous + current
> > 
> > especially if you are doing silly things like calculating Fibonacci
> > Sequence values. Translating this to D, you end up with:
> > 
> >          TypeTuple!(current, next) = tuple(next , current +next);
> > 
> > I am assuming this is horrendously inefficient at run time 
> > compared to
> > having the intermediate value explicit:
> > 
> >          auto t = next;
> >          next = current + next;
> >          current = t;
> > 
> > or is it?
> 
> It probably depends on the compiler. The way to find out is to look 
> at the 
> generated assembler.

Using LDC, the tuple version generates more code unoptimized, but with
optimization, the exact same assembly language code is generated for
the two cases.

Win. 

Albeit the D syntax is not as nice as the Python syntax.


-- 
Russel.
=============================================================================
Dr Russel Winder      t: +44 20 7585 2200   voip: sip:russel.winder at ekiga.net
41 Buckmaster Road    m: +44 7770 465 077   xmpp: russel at winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: This is a digitally signed message part
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20150510/090fd3a9/attachment.sig>


More information about the Digitalmars-d mailing list