Skip to content

Instantly share code, notes, and snippets.

@jackfirth
Created April 28, 2021 03:40
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 jackfirth/3b79d73271c1c9868bd0d70df5dbfd4b to your computer and use it in GitHub Desktop.
Save jackfirth/3b79d73271c1c9868bd0d70df5dbfd4b to your computer and use it in GitHub Desktop.
twitter-user-@ladyaeva.md
#lang typed/racket
(define-type Vec2 (Vector Flonum Flonum))
(: mad (Flonum Flonum Flonum . -> . Flonum))
(define (mad a b c)
(+ (* a b) c))
(: vec2-mad (Vec2 Vec2 Vec2 . -> . Vec2))
(define (vec2-mad a b c)
(define out (vector 0.0 0.0))
(vector-set!
out 0
(mad (vector-ref a 0)
(vector-ref b 0)
(vector-ref c 0)))
(vector-set!
out 1
(mad (vector-ref a 1)
(vector-ref b 1)
(vector-ref c 1)))
out)
(: flops (Integer -> Void))
(define (flops iterations)
(define start-seconds (/ (current-inexact-milliseconds) 1000))
(for ([i (in-range iterations)])
(vec2-mad (vector (random) (random))
(vector (random) (random))
(vector (random) (random))))
(define stop-seconds (/ (current-inexact-milliseconds) 1000))
(define delta-seconds (- stop-seconds start-seconds))
(define ops (* iterations 6))
(define flops (/ ops delta-seconds))
(printf "ran for ~a seconds at ~a operations per second\n" delta-seconds flops))
(module+ main
(flops 10000000))
#lang racket
(require racket/unsafe/ops)
(define (mad a b c)
(unsafe-fl+ (unsafe-fl* a b) c))
(define (vec2-mad a b c)
(define out (vector 0.0 0.0))
(unsafe-vector-set!
out 0
(mad (unsafe-vector-ref a 0)
(unsafe-vector-ref b 0)
(unsafe-vector-ref c 0)))
(unsafe-vector-set!
out 1
(mad (unsafe-vector-ref a 1)
(unsafe-vector-ref b 1)
(unsafe-vector-ref c 1)))
out)
(define (flops iterations)
(define start-seconds (/ (current-inexact-milliseconds) 1000))
(for ([i (in-range iterations)])
(vec2-mad (vector (random) (random))
(vector (random) (random))
(vector (random) (random))))
(define stop-seconds (/ (current-inexact-milliseconds) 1000))
(define delta-seconds (- stop-seconds start-seconds))
(define ops (* iterations 6))
(define flops (/ ops delta-seconds))
(printf "ran for ~a seconds at ~a operations per second\n" delta-seconds flops))
(module+ main
(flops 10000000))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment