<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><br><div><div>On Dec 9, 2010, at 1:51 PM, Luke Jordan wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><font class="Apple-style-span" face="'courier new', monospace">Here's a rookie question that stems from HtDP 29.3.2.</font><div><font class="Apple-style-span" face="'courier new', monospace"><br></font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">The idea is to test an expression a number of times while timing it and compare to another version of the same function. The expression finds a route in a vector (graph) from node 0 to node 4. The way I would do this in C/Python is with a while loop, while the counter is not zero execute the expression (ignore return value), and wrap it in the timer. In Racket I came up with:</font><div>
<font class="Apple-style-span" face="'courier new', monospace"><br></font></div><div><div><font class="Apple-style-span" face="'courier new', monospace">(time (for-each</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> (lambda (dest) (find-route 0 dest Graph))</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> (make-list 1000 4)))</font></div></div><div><font class="Apple-style-span" face="'courier new', monospace"><br></font></div><div>
<font class="Apple-style-span" face="'courier new', monospace">It seems unnecessarily tricky, but I couldn't think of a better way. Am I missing a piece? Does it only seem tricky because Racket is my first functional language.</font></div>
</div></blockquote><br></div><div>One problem with this, which even a rookie :-) can see, is that the "time" applies not only to finding the routes but to generating the list, which may mess up your statistics. So what you really want is more like</div><div>(let ((fours (make-list 1000 4)))</div><div> (time (for-each (lambda (dest) (find-route 0 dest Graph))</div><div> fours)))</div><div><br></div><div>But you're quite right, it's silly to create a list of 1000 fours just so you can apply for-each to them. A somewhat cleaner way is</div><div>(for [counter (in-range 1 1000)]</div><div> (find-route 0 4 Graph))</div><div><br></div><div>This is (roughly) equivalent to the C++/Java code</div><div>for (int counter = 1; counter <= 1000; ++counter) {</div><div> findRoute(0,4,Graph);</div><div> }</div><div><br></div><br><div>
<span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div>Stephen Bloch</div><div><a href="mailto:sbloch@adelphi.edu">sbloch@adelphi.edu</a></div></div></span></span>
</div>
<br></body></html>