mandelbrot.scm 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. (use-modules
  2. (ice-9 futures)
  3. ;; SRFI 19 for time related procedures
  4. (srfi srfi-19))
  5. ;; Just defining a timing macro here to conveniently measure elapsed time of
  6. ;; evaluating expressions.
  7. (define-syntax time
  8. (syntax-rules ()
  9. [(time expr expr* ...)
  10. (begin
  11. (define start-time (current-time time-monotonic))
  12. expr
  13. expr* ...
  14. (define end-time (current-time time-monotonic))
  15. (let* ([diff (time-difference end-time start-time)]
  16. [elapsed-ns (+ (/ (time-nanosecond diff) 1e9)
  17. (time-second diff))])
  18. (display (format #t "~fs~%" elapsed-ns))))]))
  19. (define (mandelbrot iterations x y n)
  20. (let ([ci (- (/ (* 2.0 y) n) 1.0)]
  21. [cr (- (/ (* 2.0 x) n) 1.5)])
  22. (let loop ([i 0] [zr 0.0] [zi 0.0])
  23. (if (> i iterations)
  24. i
  25. (let ([zrq (* zr zr)]
  26. [ziq (* zi zi)])
  27. (cond
  28. [(> (+ zrq ziq) 4.0) i]
  29. [else (loop (+ i 1)
  30. (+ (- zrq ziq) cr)
  31. (+ (* 2.0 zr zi) ci))]))))))
  32. (time
  33. (let ([f (future (lambda () (mandelbrot 10000000 62 501 1000)))])
  34. (list (mandelbrot 10000000 62 500 1000)
  35. (touch f))))
  36. (time
  37. (mandelbrot 10000000 62 501 1000))