Skip to content

Instantly share code, notes, and snippets.

@jodosha
Created December 4, 2017 20:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jodosha/a00192eb0b4108c3eab8b542b9f1ad39 to your computer and use it in GitHub Desktop.
Save jodosha/a00192eb0b4108c3eab8b542b9f1ad39 to your computer and use it in GitHub Desktop.
Ruby benchmark `SecureRandom.hex(16)` vs `SecureRandom.uuid`
#!/usr/bin/env ruby
# frozen_string_literal: true
require "benchmark/ips"
require "securerandom"
Benchmark.ips do |x|
x.report("hex(16)") { SecureRandom.hex(16) }
x.report("uuid") { SecureRandom.uuid }
x.compare!
end
__END__
Result:
Warming up --------------------------------------
hex(16) 38.576k i/100ms
uuid 16.630k i/100ms
Calculating -------------------------------------
hex(16) 439.159k (±15.0%) i/s - 2.083M in 5.037059s
uuid 184.132k (± 9.1%) i/s - 931.280k in 5.109522s
Comparison:
hex(16): 439159.0 i/s
uuid: 184132.3 i/s - 2.39x slower
Ruby:
ruby 2.4.2p198 (2017-09-14 revision 59899) [x86_64-darwin16]
Hardware:
Hardware Overview:
Model Name: MacBook Pro
Model Identifier: MacBookPro12,1
Processor Name: Intel Core i7
Processor Speed: 3,1 GHz
Number of Processors: 1
Total Number of Cores: 2
L2 Cache (per Core): 256 KB
L3 Cache: 4 MB
Memory: 16 GB
Boot ROM Version: MBP121.0171.B00
SMC Version (system): 2.28f7
Software:
System Software Overview:
System Version: macOS 10.12.6 (16G1036)
Kernel Version: Darwin 16.7.0
Time since boot: 25 days 10:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment