<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Tahoma
}
--></style></head>
<body class='hmmessage'><div dir='ltr'>
This test isn't the best but it somehow shows,<BR><br><BR>(define test (build-list 500000 (lambda (x) (cons (+ 1 x) 0))))<br><br>(define (handlers-example lst)<br>&nbsp; &nbsp; (display "Using \"with-handlers\" to handle possible errors") (newline)<br>&nbsp; &nbsp; (time<br>&nbsp;  &nbsp; &nbsp; (for-each<br>&nbsp;   &nbsp; &nbsp; &nbsp; (lambda (x)<br>&nbsp;     &nbsp; &nbsp; &nbsp; &nbsp; (with-handlers ((exn:fail? (lambda (x) 1)))<br>&nbsp;       &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (/ (car x) (cdr x))))<br>&nbsp;   &nbsp; &nbsp; &nbsp; lst)))<br><br>(define (if-example lst)<br>&nbsp; &nbsp; (display "Using \"if\" to handle possible errors") (newline)<br>&nbsp; &nbsp; (time<br>&nbsp;  &nbsp; &nbsp; (for-each<br>&nbsp;   &nbsp; &nbsp; &nbsp; (lambda (x)<br>&nbsp;     &nbsp; &nbsp; &nbsp; &nbsp; (if (= 0 (cdr x))<br>&nbsp;         &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1<br>&nbsp;         &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (/ (car x) (cdr x))))<br>&nbsp;   &nbsp; &nbsp; &nbsp; lst)))<br><br>(handlers-example test)<br>(handlers-example test)<br>(handlers-example test)<br>(if-example test)<br>(if-example test)<br>(if-example test)<BR><br><BR><br><BR>Results in<BR><br><BR>Using "with-handlers" to handle possible errors<br>cpu time: 6875 real time: 8016 gc time: 1202<br>Using "with-handlers" to handle possible errors<br>cpu time: 6687 real time: 6906 gc time: 1130<br>Using "with-handlers" to handle possible errors<br>cpu time: 6703 real time: 7438 gc time: 1077<br>Using "if" to handle possible errors<br>cpu time: 47 real time: 47 gc time: 0<br>Using "if" to handle possible errors<br>cpu time: 62 real time: 62 gc time: 0<br>Using "if" to handle possible errors<br>cpu time: 63 real time: 63 gc time: 0<BR><br><BR><br><BR><br><BR>Is it supposed to behave that way?<BR>This isn't the best example because I found about this trying to use list-ref with bigger numbers than the list length, and for just 20 to 30 exceptions it was taking nearly 20 seconds.<BR>Is there anyway to make exception handling faster? Putting "if"s everywhere looks ugly.<BR>                                               </div></body>
</html>