Created
October 21, 2022 06:06
-
-
Save rdp/2ed3e38f42c44b7ab5c96d017f41ea46 to your computer and use it in GitHub Desktop.
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
require "benchmark" | |
require "uuid" | |
Benchmark.ips do |x| | |
x.report { UUID.random } | |
end |
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
require "benchmark" | |
require "uuid" | |
require "random" | |
module Crystal::System::Random | |
GENERATOR = SecureGenerator.new | |
private class SecureGenerator | |
getter buffer : Bytes = Bytes.new(8192).tap { |b| Crystal::System::Random.sys_getrandom b } | |
getter index = 0 | |
def bytes(bytes : Bytes) : Nil | |
if (offset = bytes.bytesize - (buffer.bytesize - index)) > 0 | |
(buffer + index).copy_to bytes | |
@index = 0 | |
Crystal::System::Random.sys_getrandom buffer | |
bytes(bytes + offset) | |
else | |
(buffer + index).copy_to bytes.to_unsafe, bytes.bytesize | |
@index += bytes.bytesize | |
end | |
end | |
end | |
end | |
Benchmark.ips do |x| | |
x.report { UUID.random } | |
end |
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
require "benchmark" | |
require "uuid" | |
require "random" | |
module Crystal::System::Random | |
GENERATOR = SecureGenerator.new | |
private class SecureGenerator | |
getter buffer : Bytes = Bytes.new(8192).tap { |b| Crystal::System::Random.sys_getrandom b } | |
getter index = 0 | |
def bytes(bytes : Bytes) : Nil | |
if (offset = bytes.bytesize - (buffer.bytesize - index)) > 0 | |
(buffer + index).copy_to bytes | |
@index = 0 | |
Crystal::System::Random.sys_getrandom buffer | |
bytes(bytes + offset) | |
else | |
(buffer + index).copy_to bytes.to_unsafe, bytes.bytesize | |
@index += bytes.bytesize | |
end | |
end | |
end | |
end | |
Benchmark.ips do |x| | |
m = Mutex.new | |
x.report { m.synchronize {UUID.random }} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment