Created
April 28, 2021 03:40
-
-
Save jackfirth/3b79d73271c1c9868bd0d70df5dbfd4b to your computer and use it in GitHub Desktop.
twitter-user-@ladyaeva.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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