www.digitalmars.com         C & C++   DMDScript  

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

reply Charles McAnany <mcanance rose-hulman.edu> writes:
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.
Mar 09 2011
next sibling parent Jesse Phillips <jessekphillips+D gmail.com> writes:
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
prev sibling parent reply spir <denis.spir gmail.com> writes:
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(&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:6UE(&ET('1A:V5S('1O(&1E=&5R;6EN92!T:&4 
 M=&%X:2!N=6UB97)S(&QE<W, =&AA;B`R-3`P,"X #0HJ($%U=&AO<CH 0VAA
 M<FQE<R!-8T%N86YY#0HJ#0HJ+PT*=F]I9"!M86EN*"E[#0H)4W1O<%=A=&-H
 M('-W.PT*"7-W+G-T87)T.PT*"69O<B`H:6YT(&ET97)S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"69O<B`H:6YT(&D /2`P.R!I(#P ;&EM:70[(&DK*RD >PT*"0EI
 M;G0 <F5S(#T ;B`M(&DJ:2II.PT*"0ED;W5B;&4 8W5B95)O;W0 /2!R97->
 M7B Q+S,N,"D[#0H)"6EN="!N96%R97-T26YT(#T 8V%S="AI;G0I("AC=6)E
 M4F]O="`K(#`N-2D[#0H)"61O=6)L92!D:69F(#T *&-U8F52;V]T("T ;F5A
 M<F5S=$EN="D *B`H8W5B95)O;W0 +2!N96%R97-T26YT*3L-" D):68 *&1I
 M9F8 /"`Q92TQ,"D >PT*"0D):68 *&AI=', /"`R("8F(')E<W5L='-;,5T 
 M(3T :2D >PT*"0D)"7)E<W5L='-;:&ET<R`J(#)=(#T :3L-" D)"0ER97-U
 M;'1S6VAI=', *B`R("L ,5T /2!N96%R97-T26YT.PT*"0D)?0T*"0D):&ET
 M<RLK.PT*"0E]#0H)?0T*"7)E='5R;B!T87AI0V%B*&XL<F5S=6QT<ULP72QR
 M97-U;'1S6S%=+')E<W5L='-;,ETL<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
parent vincent feltkamp <vincent.feltkamp gmail.com> writes:
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.
Apr 01 2011