Skip to content

Instantly share code, notes, and snippets.

@lewispb
Last active February 25, 2022 17:19
Show Gist options
  • Save lewispb/577db0cf4c75a23f6e07101df3c3488b to your computer and use it in GitHub Desktop.
Save lewispb/577db0cf4c75a23f6e07101df3c3488b to your computer and use it in GitHub Desktop.
KRedis unique list (sorted set) vs unique list (list)
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "rails"
gem "kredis", github: "lewispb/kredis", branch: "unique_list_with_sorted_set"
end
Kredis.configurator = Class.new { def config_for(name) { db: "1" } end }.new
$VERBOSE = nil
unique_list = Kredis.unique_list "uniquelist_sortedset"
unique_list_legacy = Kredis.unique_list_legacy "uniquelist_list"
# Populate the lists with some initial data
unique_list.append((1_000..100_000).map(&:to_s))
unique_list_legacy.append((1_000..100_000).map(&:to_s))
Benchmark.bm do |benchmark|
benchmark.report("unique_list (sorted set)") do
1_000.times do |n|
unique_list.append(n)
end
end
benchmark.report("unique_list (list)") do
1_000.times do |n|
unique_list_legacy.append(n)
end
end
end
unique_list.del
unique_list_legacy.del
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment