<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On 20.12.2015, at 19:08, Isaac Gouy via swift-users &lt;<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div class=""><blockquote type="cite" class="">On Sunday, December 20, 2015 9:58 AM, Pascal Urban &lt;<a href="mailto:mail@pscl.de" class="">mail@pscl.de</a>&gt; wrote:<br class=""></blockquote><br class="">...<br class=""><blockquote type="cite" class="">Both of these implementations are slow because they always create binary trees <br class=""></blockquote><br class=""><blockquote type="cite" class="">with a depth of maxDepth instead of, well, the correct depth.<br class=""></blockquote><br class="">Thank you!<br class=""><br class="">As always, I suspected I'd made a dumb mistake - but just wasn't seeing it.<br class="">_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-users<br class=""></div></div></blockquote></div><br class=""><div class=""><br class=""></div><div class="">I was able to speed up David’s solution by almost 50% by changing the method checkTree from this:</div><div class=""><br class=""></div><div class=""><span style="font-family: Menlo; font-size: 11px; color: rgb(187, 44, 162);" class="">func</span><span style="font-family: Menlo; font-size: 11px;" class=""> checkTree(</span><span style="font-family: Menlo; font-size: 11px;" class="">t: </span><span style="font-family: Menlo; font-size: 11px; color: rgb(112, 61, 170);" class="">Array</span><span style="font-family: Menlo; font-size: 11px;" class="">&lt;</span><span style="font-family: Menlo; font-size: 11px; color: rgb(79, 129, 135);" class="">TreeNodeItem</span><span style="font-family: Menlo; font-size: 11px;" class="">&gt;, </span><span style="font-family: Menlo; font-size: 11px; color: rgb(187, 44, 162);" class="">_</span><span style="font-family: Menlo; font-size: 11px;" class=""> i: </span><span style="font-family: Menlo; font-size: 11px; color: rgb(112, 61, 170);" class="">Int</span><span style="font-family: Menlo; font-size: 11px;" class="">) -&gt; </span><span style="font-family: Menlo; font-size: 11px; color: rgb(112, 61, 170);" class="">Int</span></div><div class=""><span style="font-family: Menlo; font-size: 11px; color: rgb(112, 61, 170);" class=""><br class=""></span></div><div class="">to this:</div><div class=""><span style="font-family: Menlo; font-size: 11px; color: rgb(187, 44, 162);" class=""><br class=""></span></div><div class=""><span style="font-family: Menlo; font-size: 11px; color: rgb(187, 44, 162);" class="">func</span><span style="font-family: Menlo; font-size: 11px;" class="">&nbsp;checkTree(</span><span style="color: rgb(187, 44, 162); font-family: Menlo; font-size: 11px;" class="">inout&nbsp;</span><span style="font-family: Menlo; font-size: 11px;" class="">t:&nbsp;</span><span style="font-family: Menlo; font-size: 11px; color: rgb(112, 61, 170);" class="">Array</span><span style="font-family: Menlo; font-size: 11px;" class="">&lt;</span><span style="font-family: Menlo; font-size: 11px; color: rgb(79, 129, 135);" class="">TreeNodeItem</span><span style="font-family: Menlo; font-size: 11px;" class="">&gt;,&nbsp;</span><span style="font-family: Menlo; font-size: 11px; color: rgb(187, 44, 162);" class="">_</span><span style="font-family: Menlo; font-size: 11px;" class="">&nbsp;i:&nbsp;</span><span style="font-family: Menlo; font-size: 11px; color: rgb(112, 61, 170);" class="">Int</span><span style="font-family: Menlo; font-size: 11px;" class="">) -&gt;&nbsp;</span><span style="font-family: Menlo; font-size: 11px; color: rgb(112, 61, 170);" class="">Int</span></div><div class=""><span style="font-family: Menlo; font-size: 11px; color: rgb(112, 61, 170);" class=""><br class=""></span></div><div class="">It completes in about ~10s instead of ~20s on my 2.66GHz i5 iMac for n=20.&nbsp;</div><div class="">This also works together with Pascal’s libdispatch solution. In this case it completes in ~5s.</div><div class="">Here is the modified version:&nbsp;<a href="https://gist.github.com/mauruskuehne/633789417c2357a6bb93" class="">https://gist.github.com/mauruskuehne/633789417c2357a6bb93</a></div><div class=""><br class=""></div><div class="">Could somebody explain to me why this is the case? I know what the inout keyword does, but I don’t understand why it makes the code faster in this case?</div><div class=""><br class=""></div><div class="">Maurus</div></body></html>