[racket] Is eval very slow?
On Jul 18, 2012, at 11:58 PM, Joe Gilray wrote:
> Thanks John and Rudiger,
>
> Yes, I can and will rewrite the code... I was just shocked at how slow eval was.
I don't think you should be shocked; I believe that calls to eval are essentially triggering full passes of the parser and compiler, dynamically. It's true that you could optimize for certain special cases (such as yours), but generally speaking these use cases are rare, particularly since I'm guessing that the places where this optimization would be possible are also the places where eval isn't needed.
Corrections and clarifications cheerfully accepted,
John
>
> -Joe
>
>
> On Wed, Jul 18, 2012 at 10:52 PM, Rüdiger Asche <rac at ruediger-asche.de> wrote:
> yes it is:
>
> (define thunk1 (lambda () (/ 100 10)))
> (define thunk2 (lambda () (eval '(/ 100 10))))
> (define iterate (lambda (ct thunk)
> (if (zero? ct)
> #t (begin (thunk) (iterate (- ct 1) thunk)))))
>
> (time (iterate 1000000 thunk1))
> => cpu time: 15 real time: 16 gc time: 0
>
> (time (iterate 1000000 thunk2))
> => cpu time: 21110 real time: 21125 gc time: 436
>
>
>
> ----- Original Message -----
> From: Joe Gilray
> To: Racket mailing list
> Sent: Thursday, July 19, 2012 7:15 AM
> Subject: [racket] Is eval very slow?
>
> Hi,
>
> I've written some code that uses eval:
>
> (define ops (list * / + -))
> (define digits '(1 2 3 4 5))
> (for ([dl (combinations 4 digits)])
> (define lst '())
> (for ([d (permute-all dl)])
> (for* ([o1 ops] [o2 ops] [o3 ops])
> (unless (and (eq? o1 /) (zero? (eval (list o2 (second d) (list o3 (third d) (fourth d)))))) ; avoid divide by zero
> (define val (eval (list o1 (first d) (list o2 (second d) (list o3 (third d) (fourth d))))))
> (when (>= val 1) (set! lst (cons val lst)))) ...
>
> It runs very, very slowly. Am I doing something wrong? Is there a way to speed up eval?
>
> Thanks,
> -Joe
>
>
> ____________________
> Racket Users list:
> http://lists.racket-lang.org/users
>
>
> ____________________
> Racket Users list:
> http://lists.racket-lang.org/users
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4800 bytes
Desc: not available
URL: <http://lists.racket-lang.org/users/archive/attachments/20120719/f01850f6/attachment.p7s>