[racket] with-handlers very slow?

From: Tomás Coiro (tomcoiro at hotmail.com)
Date: Thu Oct 11 13:14:26 EDT 2012

This test isn't the best but it somehow shows,


(define test (build-list 500000 (lambda (x) (cons (+ 1 x) 0))))

(define (handlers-example lst)
    (display "Using \"with-handlers\" to handle possible errors") (newline)
    (time
       (for-each
          (lambda (x)
              (with-handlers ((exn:fail? (lambda (x) 1)))
                  (/ (car x) (cdr x))))
          lst)))

(define (if-example lst)
    (display "Using \"if\" to handle possible errors") (newline)
    (time
       (for-each
          (lambda (x)
              (if (= 0 (cdr x))
                      1
                      (/ (car x) (cdr x))))
          lst)))

(handlers-example test)
(handlers-example test)
(handlers-example test)
(if-example test)
(if-example test)
(if-example test)




Results in


Using "with-handlers" to handle possible errors
cpu time: 6875 real time: 8016 gc time: 1202
Using "with-handlers" to handle possible errors
cpu time: 6687 real time: 6906 gc time: 1130
Using "with-handlers" to handle possible errors
cpu time: 6703 real time: 7438 gc time: 1077
Using "if" to handle possible errors
cpu time: 47 real time: 47 gc time: 0
Using "if" to handle possible errors
cpu time: 62 real time: 62 gc time: 0
Using "if" to handle possible errors
cpu time: 63 real time: 63 gc time: 0






Is it supposed to behave that way?
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.
Is there anyway to make exception handling faster? Putting "if"s everywhere looks ugly.
 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20121011/c7db4d5f/attachment.html>

Posted on the users mailing list.