;;; -*- lexical-binding: t -*- (define-inline mandelbrot1 (cx cy) (let ((zr 0) (zi 0) (v 0)) (while (and (< v 256) (<= (+ (* zr zr) (* zi zi)) 4)) (setq v (1+ v)) (let ((tmp (+ (* zr zr) (- (* zi zi)) cx))) (setq zi (+ (* (* zr zi) 2) cy)) (setq zr tmp))) ;; samples that hit 256 wrap in the graphic to display as zero ;; exponential 0.8 enhances contrast a bit (floor (* 256 (expt (/ v 256.0) 0.8))))) (defun mandelbrot (&optional w h) (declare (speed 3)) (interactive) (let ((output (make-vector (* w h) 0.0)) (idx 0)) (let ((w (or w 1600)) (h (or h 1200)) (x0 -2.5) (y0 -1.5) (dx 4.0) (dy 3.0)) (let ((dxp (/ dx w)) (dyp (/ dy h))) (dotimes (y h) (let ((cy (+ y0 (* dyp y)))) (dotimes (x w) (let ((v (mandelbrot1 (+ x0 (* dxp x)) cy))) (aset output idx v) (setq idx (1+ idx)))))))) output)) (defun mandelbrot-bench () (interactive) (let (output) (profiler-start 'cpu+mem) (setq output (mandelbrot 320 200)) (profiler-stop) (profiler-report) (message "%S" output))) ;;(mandelbrot 320 200) ;;(mandelbrot 640 480) ;;(mandelbrot 1024 768) ;;(mandelbrot) ;; (profiler-start 'mem) ;; (mandelbrot 320 200) ;; (profiler-stop) ;; (profiler-report) ;; (profiler-start 'cpu+mem) ;; (mandelbrot 320 200) ;; (profiler-stop) ;; (profiler-report) ;; (profiler-start 'cpu) ;; (mandelbrot 320 200) ;; (profiler-stop) ;; (profiler-report) ;;(profiler-start 'cpu+mem) ;;(mandelbrot) ;;(profiler-stop) ;;(profiler-report) ;; (benchmark-run 1 (mandelbrot)) ;; (15.033354357 15 3.9534469799999954) ;;(120.40541861 1947 93.45048212499998) ;;(128.362728323 1942 93.44881820700004)