<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> (display "Using \"with-handlers\" to handle possible errors") (newline)<br> (time<br> (for-each<br> (lambda (x)<br> (with-handlers ((exn:fail? (lambda (x) 1)))<br> (/ (car x) (cdr x))))<br> lst)))<br><br>(define (if-example lst)<br> (display "Using \"if\" to handle possible errors") (newline)<br> (time<br> (for-each<br> (lambda (x)<br> (if (= 0 (cdr x))<br> 1<br> (/ (car x) (cdr x))))<br> 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>