提高Clojure Mandelbrot发电机的效率

时间:2015-11-24 12:28:46

标签: clojure mandelbrot quil

我想学习Clojure,并开始使用quil的Mandelbrot生成器,我让它工作 - 但是生成图像需要一些时间,并且是一个巨大的资源浪费。有关如何使其更快或者如果您发现我的代码中任何非clojure-esque部分的任何建议。

Core.clj

(ns frac.core
  (:require [quil.core :as q])
  (:require [frac.complex :refer :all]))

(def scale (atom 0.01))
(def xoff (atom -200))
(def yoff (atom -200))
(def its 50)

(defn mandelbrot [r i]
 (count (take its (take-while #(<= (modu %) 2) (iterate #( add (mul % %) [r i]) [r i])))))

(defn gen []
  (let [p (q/pixels)
        w (q/width)
        h (q/height)]
    (doseq [x (range w) y (range h)]
      (let [m (mandelbrot (* @scale (+ x @xoff)) (* @scale (+ y @yoff)))
            c (if (= m its) 0 m)]
        (aset p (+ x (* y w)) (q/color (* 1.5 c) (* 4 c) (* 5.2 c))))))
  (q/update-pixels))

(defn setup []
  (gen))

(defn draw [])

(defn click []
  (swap! xoff #(+ (q/mouse-x) (- (/ (q/width) 2)) %))
  (swap! yoff #(+ (q/mouse-y) (- (/ (q/height) 2)) %))
  (gen))

(defn wheel [z] 
  (swap! scale #(if (pos? z) (* 1.1 %) (* 0.9 %)))
  (prn @scale)
  (gen))

(q/defsketch example
  :title "Mandel"
  :setup setup
  :draw draw
  :size [400 400]
  :mouse-clicked click
  :mouse-wheel wheel)

(defn -main [& args])

Complex.clj

(ns frac.complex)

(defn mul [z1 z2]
  (let [r1 (first z1)
        i1 (second z1)
        r2 (first z2)
        i2 (second z2)]
    [(- (* r1 r2) (* i1 i2)) (+ (* r1 i2) (* r2 i1))]))

(defn add [z1 z2]
  (let [r1 (first z1)
        i1 (second z1)
        r2 (first z2)
        i2 (second z2)]
    [(+ r1 r2) (+ i1 i2)]))

(defn modu [z]
  (let [r (first z)
        i (second z)]
    (Math/sqrt (+ (* r r) (* i i)))))

1 个答案:

答案 0 :(得分:0)

尝试设置:

(set! *unchecked-math* :warn-on-boxed)

并删除所有警告。根据需要使用类型提示。