How would you solve this 'interview' question in D?
Steven Schveighoffer
schveiguy at yahoo.com
Thu Jun 27 10:48:07 PDT 2013
On Wed, 26 Jun 2013 20:01:16 -0400, Andrej Mitrovic
<andrej.mitrovich at gmail.com> wrote:
> On 6/27/13, Andrej Mitrovic <andrej.mitrovich at gmail.com> wrote:
>> On 6/27/13, H. S. Teoh <hsteoh at quickfur.ath.cx> wrote:
>>> Well, it's still cheating, though. :-P I think the 4-cycle algorithm is
>>> probably still the best one I've seen.
>>
>> What I don't understand is why the CPU is so slow that it takes ages
>> to go through int.min .. int.max in a loop.
>>
>
> I mean this takes 31 seconds on my machine (obviously without
> optimization flags):
>
> -----
> int f(int n) { return n; }
>
> void main()
> {
> foreach (n; int.min..int.max)
> f(f(n));
> }
> -----
>
> Maybe I need an upgrade.
Nope. 4 billion loops is a lot. That's just a fact of life. I have
quad-core i7 at 2.2Ghz, and it takes a while.
You *could* use parallel foreach :)
Note that in that code, you need to test f(f(int.max)) specifically.
-Steve
More information about the Digitalmars-d-learn
mailing list