Skip to content

Instantly share code, notes, and snippets.

@jasom
Created December 13, 2017 23:35
Show Gist options
  • Save jasom/d9dc24a6d65e2d0ab848c450985708d0 to your computer and use it in GitHub Desktop.
Save jasom/d9dc24a6d65e2d0ab848c450985708d0 to your computer and use it in GitHub Desktop.
(defvar *fast-prng-cipher* nil)
(defun fast-random (nbytes)
(unless *fast-prng-cipher*
(setf *fast-prng-cipher*
(ironclad:make-cipher 'ironclad:salsa20
:key (ironclad:random-data 32) :mode :stream)))
(let ((result (make-array nbytes :element-type '(unsigned-byte 8))))
(ironclad:encrypt *fast-prng-cipher* (make-array nbytes :element-type '(unsigned-byte 8) :initial-element 0) result)
result))
#|
For best multi-core performance, make *fast-prng-cipher* thread-local.
3.0GHz Ryzen 1700 generates 10M random 16-byte strings in 2 seconds on a single core:
CL-USER> (time (loop repeat 10000000 do (fast-random 16)))
Evaluation took:
2.082 seconds of real time
2.082169 seconds of total run time (1.975959 user, 0.106210 system)
100.00% CPU
7,065,394,718 processor cycles
639,991,792 bytes consed
|#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment