Skip to content

Instantly share code, notes, and snippets.

@samth
Created June 27, 2021 01:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samth/385182bcd0c1bc26795aa7e58a81f80b to your computer and use it in GitHub Desktop.
Save samth/385182bcd0c1bc26795aa7e58a81f80b to your computer and use it in GitHub Desktop.
#lang typed/racket
(require typed/racket/draw
plot/private/common/parameters ;; just for plot-width, plot-height
)
(: transform-points : (Listof (List Integer Real)) -> (Listof (Pair Real Real)))
(define (transform-points p)
(define-values (min-x min-y max-x max-y)
(for/fold ([min-x : Integer (first (first points))]
[min-y : Real (second (first points))]
[max-x : Integer (first (first points))]
[max-y : Real (second (first points))])
([point : (List Integer Real) (in-list (rest points))])
(define x (first point))
(define y (second point))
(values (min min-x x) (min min-y y) (max max-x x) (max max-y y))))
(define dx (- max-x min-x))
(define dy (- max-y min-y))
(define w (plot-width))
(define h (plot-height))
(: transform : (List Integer Real) -> (Pair Real Real))
(define (transform point)
(define x (first point))
(define y (second point))
(cons (* w (/ (- x min-x) dx))
(* h (- dy (/ (- y min-y) dy)))))
(map transform points))
(: do-manual-plot : (Listof (List Integer Real)) -> Any)
(define (do-manual-plot points)
(define bm (make-object bitmap% (plot-width) (plot-height)))
(define dc (new bitmap-dc% [bitmap bm]))
(send dc set-pen (send the-pen-list find-or-create-pen "purple" 1 'solid))
(define plot-points (transform-points points))
(send dc draw-lines plot-points)
bm)
(: do-manual-plot/individual-lines : (Listof (List Integer Real)) -> Any)
(define (do-manual-plot/individual-lines points)
(define bm (make-object bitmap% (plot-width) (plot-height)))
(define dc (new bitmap-dc% [bitmap bm]))
(send dc set-pen (send the-pen-list find-or-create-pen "purple" 1 'solid))
(define plot-points (transform-points points))
(for ([p1 (in-list plot-points)]
[p2 (in-list (rest plot-points))])
(send dc draw-line (car p1) (cdr p1) (car p2) (cdr p2)))
bm)
'typed
(collect-garbage)
(collect-garbage)
(define points (build-list 100000 (lambda ([x : Integer]) (list x (random)))))
(printf "individual draw-line calls~%")
(time (do-manual-plot/individual-lines points))
(collect-garbage)
(collect-garbage)
(printf "single draw-lines call~%")
(time (do-manual-plot points))
#lang racket
(require racket/draw
plot/private/common/parameters ;; just for plot-width, plot-height
)
(define (transform-points p)
(define-values (min-x min-y max-x max-y)
(for/fold ([min-x (first (first points))]
[min-y (second (first points))]
[max-x (first (first points))]
[max-y (second (first points))])
([point (in-list (rest points))])
(define x (first point))
(define y (second point))
(values (min min-x x) (min min-y y) (max max-x x) (max max-y y))))
(define dx (- max-x min-x))
(define dy (- max-y min-y))
(define w (plot-width))
(define h (plot-height))
(define (transform point)
(define x (first point))
(define y (second point))
(cons (* w (/ (- x min-x) dx))
(* h (- dy (/ (- y min-y) dy)))))
(map transform points))
(define (do-manual-plot points)
(define bm (make-object bitmap% (plot-width) (plot-height)))
(define dc (new bitmap-dc% [bitmap bm]))
(send dc set-pen (send the-pen-list find-or-create-pen "purple" 1 'solid))
(define plot-points (transform-points points))
(send dc draw-lines plot-points)
bm)
(define (do-manual-plot/individual-lines points)
(define bm (make-object bitmap% (plot-width) (plot-height)))
(define dc (new bitmap-dc% [bitmap bm]))
(send dc set-pen (send the-pen-list find-or-create-pen "purple" 1 'solid))
(define plot-points (transform-points points))
(for ([p1 (in-list plot-points)]
[p2 (in-list (rest plot-points))])
(send dc draw-line (car p1) (cdr p1) (car p2) (cdr p2)))
bm)
'untyped
(collect-garbage)
(collect-garbage)
(define points (build-list 100000 (lambda (x) (list x (random)))))
(printf "individual draw-line calls~%")
(time (do-manual-plot/individual-lines points))
(collect-garbage)
(collect-garbage)
(printf "single draw-lines call~%")
(time (do-manual-plot points))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment