Skip to content

Instantly share code, notes, and snippets.

@mjgiarlo
Last active August 29, 2015 14:26
Show Gist options
  • Save mjgiarlo/1b09f710a625ffb92a8f to your computer and use it in GitHub Desktop.
Save mjgiarlo/1b09f710a625ffb92a8f to your computer and use it in GitHub Desktop.
1st 0.000000 0.000000 0.000000 ( 0.001382)
10th 0.010000 0.000000 0.010000 ( 0.002431)
100th 0.000000 0.000000 0.000000 ( 0.001564)
1,000th 0.000000 0.000000 0.000000 ( 0.002074)
10,000th 0.010000 0.000000 0.010000 ( 0.006800)
# These are using YAML.dump and YAML.load
1st 0.020000 0.000000 0.020000 ( 0.014040)
10th 0.010000 0.000000 0.010000 ( 0.012729)
100th 0.010000 0.000000 0.010000 ( 0.013481)
1,000th 0.010000 0.000000 0.010000 ( 0.015883)
10,000th 0.020000 0.000000 0.020000 ( 0.021795)
100,000th 0.060000 0.000000 0.060000 ( 0.066164)
require 'benchmark'
def stateful_minter
File.open('minter-state', File::RDWR | File::CREAT, 0644) do |f|
# grab the state from a file
f.flock(File::LOCK_EX)
current_state = Marshal.load(f.read)
# instantiate a minter with the provided state
minter = described_class.new(current_state)
yield minter if block_given?
# overwrite the state file
f.rewind
# dump the minter state and get it in serializable form
state = Marshal.dump(minter.dump)
# serialize the state
f.write(state)
f.flush
f.truncate(f.pos)
end
# create the minter
minter = described_class.new(template: '.reeddeeddk')
# serialize its initial state (at the beginning of its sequence)
File.open('minter-state', File::RDWR | File::CREAT, 0644) { |f| f.write(Marshal.dump(minter.dump)) }
# test how long it takes to mint an ID at different points in the sequence
Benchmark.bm(7) do |x|
x.report('1st') { stateful_minter(&:mint) }
9.times { stateful_minter(&:mint) }
x.report('10th') { stateful_minter(&:mint) }
90.times { stateful_minter(&:mint) }
x.report('100th') { stateful_minter(&:mint) }
900.times { stateful_minter(&:mint) }
x.report('1,000th') { stateful_minter(&:mint) }
9_000.times { stateful_minter(&:mint) }
x.report('10,000th') { stateful_minter(&:mint) }
90_000.times { stateful_minter(&:mint) }
x.report('100,000th') { stateful_minter(&:mint) }
900_000.times { stateful_minter(&:mint) }
x.report('1,000,000th') { stateful_minter(&:mint) }
1_000_000.times { stateful_minter(&:mint) }
x.report('2,000,000th') { stateful_minter(&:mint) }
1_000_000.times { stateful_minter(&:mint) }
x.report('3,000,000th') { stateful_minter(&:mint) }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment