## digitalmars.D.learn - Commenting out a print slows my code?

• Charles McAnany (68/68) Mar 09 2011 Hi, all. I'm in college, taking a freshman-level CS class. (I'm actually...
• Jesse Phillips (2/10) Mar 09 2011 I couldn't get the code to compile with the stop watch. So instead I rem...
• spir (12/78) Mar 10 2011 No idea.
• vincent feltkamp (52/52) Apr 01 2011 I managed to get the file, and even compile it, but apparently the type ...
```Hi, all. I'm in college, taking a freshman-level CS class. (I'm actually a
senior chemist with free time.)
Anyhoo, the warm-up assignment was Hardy Taxi problem, phrased like this:
[Exposition removed.] 1729 is the smallest number such that for
(a!=b!=c!=d)>0, there exists a combination of a, b, c, and d where a^3+b^3 =
1729 = c^3+d^3.
The task was to find all other numbers with this property less than 25,000.
The code is basically

for (int iters = 1; iters <= 25_000; iters++){
if(isTaxiNumber(iters)){
writefln("%s is a taxi number", iters);
}
}

(There's also a StopWatch timing things, and isTaxiNumber returns a struct,
not a bool. See attached code.)
This code runs in about 0.09 seconds.
If I comment out the writefln, it takes 0.11 seconds.
(These are collected from about 20 runs of each. Times are very consistent.)
Any ideas why commenting out the write makes things slower?
Cheers,
Charles.
begin 644 taxi.d
M:6UP;W)T('-T9"YD871E=&EM93L +R\ 9F]R('1H92!3=&]P5V%T8V  <W1R
M=6-T+B`-"FEM<&]R="!S=&0N<W1R:6YG.R`O+V9O<B!T:&4 9F]R;6%T*"DN
M(`T*:6UP;W)T('-T9"YS=&1I;SL +R]P<FEN=&EN9RX #0HO+R\ 02!S=')U
M8W0 =&AA="!R97!R97-E;G1S(&\$ 8V%B+B!)="!D;V5S;B=T(&1O('1H92!C
M:&5C:VEN9R!T:&%T('1H92!O;F4 :6X =&AE(&%S<VEG;FUE;G0 9&]E<RP
M=&AO=6=H+B`-"G-T<G5C="!T87AI0V%B>PT*"6EN="!T87AI3G5M8F5R.PT*
M"6EN="!A+"!B+"!C+"!D.PD-" ES=')I;F< =&]3=')I;F<H*7L-" D)<F5T
M=7)N(&9O<FUA=" B8V%R("5S(#T 6R5S("5S("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PT*"7)E='5R;B!C<D9L.PT*?0``
`
end
```
Mar 09 2011
```Charles McAnany Wrote:

(There's also a StopWatch timing things, and isTaxiNumber returns a struct,
not a bool. See attached code.)
This code runs in about 0.09 seconds.
If I comment out the writefln, it takes 0.11 seconds.
(These are collected from about 20 runs of each. Times are very consistent.)
Any ideas why commenting out the write makes things slower?
Cheers,
Charles.

I couldn't get the code to compile with the stop watch. So instead I removed
that and used time. In this case I get user times between .150 and .200
approximately. Maybe with the print statement your program is given higher
priority than without, that would mean the operating system would let your
application run longer or more frequently than other programs (don't remember
if one or both of those is changed).
```
Mar 09 2011
```On 03/10/2011 01:44 AM, Charles McAnany wrote:
Hi, all. I'm in college, taking a freshman-level CS class. (I'm actually a
senior chemist with free time.)
Anyhoo, the warm-up assignment was Hardy Taxi problem, phrased like this:
[Exposition removed.] 1729 is the smallest number such that for
(a!=b!=c!=d)>0, there exists a combination of a, b, c, and d where a^3+b^3 =
1729 = c^3+d^3.
The task was to find all other numbers with this property less than 25,000.
The code is basically

for (int iters = 1; iters<= 25_000; iters++){
if(isTaxiNumber(iters)){
writefln("%s is a taxi number", iters);
}
}

(There's also a StopWatch timing things, and isTaxiNumber returns a struct,
not a bool. See attached code.)
This code runs in about 0.09 seconds.
If I comment out the writefln, it takes 0.11 seconds.
(These are collected from about 20 runs of each. Times are very consistent.)
Any ideas why commenting out the write makes things slower?

No idea.

But more idiomatic D would rather b:
foreach (n ; 1..25000)
{...}

Also I don't understand why your loop var is called 'iters'.

Finally, the attachment is not readible by me: see below.

Denis

begin 644 taxi.d
M:6UP;W)T('-T9"YD871E=&EM93L +R\ 9F]R('1H92!3=&]P5V%T8V  <W1R
M=6-T+B`-"FEM<&]R="!S=&0N<W1R:6YG.R`O+V9O<B!T:&4 9F]R;6%T*"DN
M(`T*:6UP;W)T('-T9"YS=&1I;SL +R]P<FEN=&EN9RX #0HO+R\ 02!S=')U
M8W0 =&AA="!R97!R97-E;G1S(&\$ 8V%B+B!)="!D;V5S;B=T(&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L<F5S=6QT<ULS72D[#0I]#0H-"B\O+R!F
M:6YD<R!T:&4 ;&%R9V5S="!I;G1E9V5R('  <W5C:"!T:&%T('A>,R`\(&X
M#0II;G0 8W5B95)O;W1&;&]O<BAI;G0 ;BE[#0H)9&]U8FQE(&-U8F52;V]T
M(#T ;B!>7B`H,2\S+C`I.PT*"6EN="!C<D9L(#T 8V%S="AI;G0I(&-U8F52
6;V]T.PT*"7)E='5R;B!C<D9L.PT*?0``
`
end

--
_________________
vita es estrany
spir.wikidot.com
```
Mar 10 2011
```I managed to get the file, and even compile it, but apparently the type Ticks
doesn't exists any more, it is now (DMD2.052)
TickDuration, with methods TickDuration.seconds (msecs, hnsecs, ...).

on my computer, it does not generate slower code without the writeln than with.
( .38-.39 secs with, .37-.38 without)
am including the file as changed to make it work. It does vary a lot though:
the first run after building takes .48 secs, the
next ones are in the ranges above. I guess it has more to do with priorities
than with code efficiency.
begin 644 taxi.d
M:6UP;W)T('-T9"YD871E=&EM93L +R\ 9F]R('1H92!3=&]P5V%T8V  <W1R
M=6-T+B`-"FEM<&]R="!S=&0N<W1R:6YG.R`O+V9O<B!T:&4 9F]R;6%T*"DN
M(`T*:6UP;W)T('-T9"YS=&1I;SL +R]P<FEN=&EN9RX #0HO+R\ 02!S=')U
M8W0 =&AA="!R97!R97-E;G1S(&\$ 8V%B+B!)="!D;V5S;B=T(&1O('1H92!C
M:&5C:VEN9R!T:&%T('1H92!O;F4 :6X =&AE(&%S<VEG;FUE;G0 9&]E<RP
M=&AO=6=H+B`-"G-T<G5C="!T87AI0V%B>PT*"6EN="!T87AI3G5M8F5R.PT*
M"6EN="!A+"!B+"!C+"!D.PD-" ES=')I;F< =&]3=')I;F<H*7L-" D)<F5T
M=7)N(&9O<FUA=" B8V%R("5S(#T 6R5S("5S("5S("5S72(L=&%X:4YU;6)E
M<BP 82P 8BP 8RP 9"D[#0H)?0T*?0T*#0HO*BH-"BH 4')O9W)A;2!T:&%T
M(&UE87-U<F5S('1H92!T:6U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