Here is a graphical example where you can see the difference in speed. <br><br>The life-bitblt-slower.ss requires matrix-slower.ss (with ->d and no bounds checking in the body of the code) and is really slow.<br><br>The life-bitblt-faster.ss requires matrix-faster.ss (with -> and bounds checking in the body of the code) and performs okay.<br>
<br>Other than the contract and the corresponding presence or ansence of bounds checking (to give the same functionality), the code is identical. [The only difference between life-bitblt-slower.ss and life-bitblt-faster.ss is the require statement.]<br>
<br>
The life-bitblt-vector.ss version does the matrix abstraction
internally (using a vector) and doesn't use any external module (or contracts) at all. It is by far the fastest, but
it's kind of an apples and oranges comparison with the others. I'm not sure how much of the speedup is due to the matrix abstraction into a module or the contracts.<br>
<br>
They all do better compiled. But it interesting to see the speed difference.<br>
<br>Note that the code used the science and animated-canvas collections from PLaneT and will download them the first time any of them are run, which takes some time.<br><br>Doug<br><br><div class="gmail_quote">On Sat, Apr 11, 2009 at 1:55 PM, Robby Findler <span dir="ltr"><<a href="mailto:robby@eecs.northwestern.edu" target="_blank">robby@eecs.northwestern.edu</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">->d actually evaluates the expressions (and/c<br>
exact-nonnegative-integer? (</c (matrix-rows matrix))) each time you<br>
call the function, and that evaluation involves building up a contract<br>
combinator (and doing various error checking) before actually checking<br>
the contract. That's where the extra overhead comes from. In<br>
comparison with the -> contract, all of that work is done once, before<br>
the contract is even attached to the value. I have experimented with<br>
various optimizations to avoid this work, but there is a lot to do to<br>
make that happen.<br>
<br>
Robby<br>
<br>
On Sat, Apr 11, 2009 at 2:51 PM, Doug Williams<br>
<div><div></div><div><<a href="mailto:m.douglas.williams@gmail.com" target="_blank">m.douglas.williams@gmail.com</a>> wrote:<br>
> Yes, the -> is fine. And, so is doing the same bounds check in my own<br>
> procedure. I was surprised that the ->d was so much slower. I use contracts<br>
> regularly and was trying to expand my usage of them.<br>
><br>
> On Sat, Apr 11, 2009 at 12:46 PM, Robby Findler<br>
> <<a href="mailto:robby@eecs.northwestern.edu" target="_blank">robby@eecs.northwestern.edu</a>> wrote:<br>
>><br>
>> ->d is definitely substantially slower than the other because the<br>
>> wrappers are more complex. Are you finding the performance overhead of<br>
>> the ordinary -> acceptable?<br>
>><br>
>> Robby<br>
>><br>
>> On Sat, Apr 11, 2009 at 1:17 PM, Doug Williams<br>
>> <<a href="mailto:m.douglas.williams@gmail.com" target="_blank">m.douglas.williams@gmail.com</a>> wrote:<br>
>> > I would like to use ->d to impose a precondition for a function. For<br>
>> > example:<br>
>> ><br>
>> > (matrix-ref<br>
>> > (->d ((matrix matrix?)<br>
>> > (i (and/c exact-nonnegative-integer? (</c (matrix-rows<br>
>> > matrix))))<br>
>> > (j (and/c exact-nonnegative-integer? (</c (matrix-cols<br>
>> > matrix)))))<br>
>> > ()<br>
>> > (result any/c)))<br>
>> ><br>
>> > or<br>
>> ><br>
>> > (matrix-ref<br>
>> > (->d ((matrix matrix?)<br>
>> > (i exact-nonnegative-integer?)<br>
>> > (j exact-nonnegative-integer?))<br>
>> > ()<br>
>> > #:pre-cond (and (< i (matrix-rows matrix))<br>
>> > (< j (matrix-cols matrix)))<br>
>> > (result any/c)))<br>
>> ><br>
>> > instead of just<br>
>> ><br>
>> > (matrix-ref<br>
>> > (-> matrix? exact-nonnegative-integer? exact-nonnegative-integer?<br>
>> > any/c))<br>
>> ><br>
>> > The first two do work, but are really, really slow.<br>
>> ><br>
>> > I like having the bounds check in the contract (as opposed to bounds<br>
>> > check<br>
>> > in the matrix-ref code, but can't accept the performance hit. Any ideas?<br>
>> ><br>
>> > Doug<br>
>> ><br>
>> ><br>
>> > _________________________________________________<br>
>> > For list-related administrative tasks:<br>
>> > <a href="http://list.cs.brown.edu/mailman/listinfo/plt-scheme" target="_blank">http://list.cs.brown.edu/mailman/listinfo/plt-scheme</a><br>
>> ><br>
>> ><br>
><br>
><br>
</div></div></blockquote></div><br>