- Run
$ createdb uniq-db-test
to create DB - Run example with Ruby (e.g.,
$ ruby 1_find_or_create_by_single_thread.rb
)
With many successful INSERTs
Warming up --------------------------------------
create_or_find_by! 58.000 i/100ms
with_lock + find_or_create_by! 33.000 i/100ms
create! + rescue 52.000 i/100ms
Calculating -------------------------------------
create_or_find_by! 694.517 (±22.9%) i/s - 9.570k in 15.013421s
with_lock + find_or_create_by! 358.963 (±16.2%) i/s - 5.181k in 15.053770s
create! + rescue 803.886 (±15.7%) i/s - 11.648k in 15.055700s
Comparison:
create! + rescue: 803.9 i/s
create_or_find_by!: 694.5 i/s - same-ish: difference falls within error
with_lock + find_or_create_by!: 359.0 i/s - 2.24x (± 0.00) slower
When row already exists
Warming up --------------------------------------
create_or_find_by! 57.000 i/100ms
with_lock + find_or_create_by! 59.000 i/100ms
create! + rescue 50.000 i/100ms
Calculating -------------------------------------
create_or_find_by! 525.534 (±12.7%) i/s - 7.695k in 15.003879s
with_lock + find_or_create_by! 530.299 (±21.1%) i/s - 7.257k in 15.065549s
create! + rescue 491.853 (± 6.1%) i/s - 7.350k in 15.000505s
Comparison:
with_lock + find_or_create_by!: 530.3 i/s
create_or_find_by!: 525.5 i/s - same-ish: difference falls within error
create! + rescue: 491.9 i/s - same-ish: difference falls within error