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 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(&UE87-U<F5S('1H92!T:6UE(&ET('1A:V5S('1O(&1E=&5R;6EN92!T:&4 M('-W.PT*"7-W+G-T87)T.PT*"69O<B`H:6YT(&ET97)S(#T ,#L :71E<G,\ M*3L-" D)?0T*"7T-" ES=RYS=&]P.PT*"51I8VMS('1I;65486ME;B`]('-W M5&%X:2!D971E<FUI;F5S(&EF(&%N(&EN=&5G97( <&%S<V5D('1O(&ET(&ES M(&$ =&%X:2!N=6UB97(L('1H870 :7,L('1H97)E(&5X:7-T<PT**B!A;B!A M+"!B+"!C+"!A;F0 9"P ;F]N92!O9B!T:&5M('1H92!S86UE+"!S=6-H('1H M=6YD+B!I9B!O;FQY(&]N92!S;VQU=&EO;B!T;R!X7C,K>5XS('=A<R!F;W5N M<F%M971E<G,Z(`T**B!N(&ES(&%N(&EN=&5G97( =&AA="!I<R!T;R!B92!C M;G0 <F5S(#T ;B`M(&DJ:2II.PT*"0ED;W5B;&4 8W5B95)O;W0 /2!R97-> 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;'1S6VAI=', *B`R("L ,5T /2!N96%R97-T26YT.PT*"0D)?0T*"0D):&ET M:6YD<R!T:&4 ;&%R9V5S="!I;G1E9V5R(' <W5C:"!T:&%T('A>,R`\(&X 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
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. Denisbegin 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 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(&UE87-U<F5S('1H92!T:6UE(&ET('1A:V5S('1O(&1E=&5R;6EN92!T:&4 M('-W.PT*"7-W+G-T87)T.PT*"69O<B`H:6YT(&ET97)S(#T ,#L :71E<G,\ M*3L-" D)?0T*"7T-" ES=RYS=&]P.PT*"51I8VMS('1I;65486ME;B`]('-W M5&%X:2!D971E<FUI;F5S(&EF(&%N(&EN=&5G97( <&%S<V5D('1O(&ET(&ES M(&$ =&%X:2!N=6UB97(L('1H870 :7,L('1H97)E(&5X:7-T<PT**B!A;B!A M+"!B+"!C+"!A;F0 9"P ;F]N92!O9B!T:&5M('1H92!S86UE+"!S=6-H('1H M=6YD+B!I9B!O;FQY(&]N92!S;VQU=&EO;B!T;R!X7C,K>5XS('=A<R!F;W5N M<F%M971E<G,Z(`T**B!N(&ES(&%N(&EN=&5G97( =&AA="!I<R!T;R!B92!C M;G0 <F5S(#T ;B`M(&DJ:2II.PT*"0ED;W5B;&4 8W5B95)O;W0 /2!R97-> 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;'1S6VAI=', *B`R("L ,5T /2!N96%R97-T26YT.PT*"0D)?0T*"0D):&ET M:6YD<R!T:&4 ;&%R9V5S="!I;G1E9V5R(' <W5C:"!T:&%T('A>,R`\(&X 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 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(&UE87-U<F5S('1H92!T:6UE(&ET('1A:V5S('1O(&1E=&5R;6EN92!T:&4 M('-W.PT*"7-W+G-T87)T.PT*"69O<F5A8V *&EN="!I=&5R(#LQ+BX ,C4P M<W<N<W1O<#L-" E4:6-K1'5R871I;VX =&EM951A:V5N(#T <W<N<&5E:SL M+R]4:6-K<PT*"7=R:71E9FQN*"(E<R!M:6QL:7-E8W,B+'1I;65486ME;BYM M<V5C<RD[+R\N=&]396-O;F1S(69L;V%T*3L-" ES=RYR97-E=#L-" T*?0T* M960 =&\ :70 :7, 82!T87AI(&YU;6)E<BP =&AA="!I<RP =&AE<F4 97AI M;64L('-U8V =&AA="`-"BH 85XS*V)>,R`](&X /6->,R`K9%XS+B`-"BH M86YD(&0 :68 9F]U;F0N(&EF(&]N;'D ;VYE('-O;'5T:6]N('1O('A>,RMY M7C, =V%S(&9O=6YD+"`-"BH 8R!A;F0 9"!W:6QL(&)E('IE<F\N($EF(&YO M;F4 =V5R92!F;W5N9"P =&AE;B!A(&%N9"!B('=I;&P 8F4 >F5R;R!A<R!W M(&ES('1O(&)E(&-H96-K960 9F]R('1A>&D ;G5M8F5R+6YE<W,N(`T**B\- M('L-" D):6YT(')E<R`](&X +2!I*FDJ:3L-" D)9&]U8FQE(&-U8F52;V]T M(#T <F5S7EXH,2\S+C`I.PT*"0EI;G0 ;F5A<F5S=$EN="`](&-A<W0H:6YT M*2`H8W5B95)O;W0 *R`P+C4I.PT*"0ED;W5B;&4 9&EF9B`]("AC=6)E4F]O M"6EF("AD:69F(#P ,64M,3`I('L-" D)"6EF("AH:71S(#P ,B`F)B!R97-U M" D)"6AI=',K*SL-" D)?0T*"7T-" ER971U<FX =&%X:4-A8BAN+')E<W5L M='-;,%TL<F5S=6QT<ULQ72QR97-U;'1S6S)=+')E<W5L='-;,UTI.PT*?0T* M7C, /"!N(`T*:6YT(&-U8F52;V]T1FQO;W(H:6YT(&XI>PT*"61O=6)L92!C ?*2!C=6)E4F]O=#L-" ER971U<FX 8W)&;#L-"GT-" `` ` end
Apr 01 2011