Skip to content

Instantly share code, notes, and snippets.

@HarlemSquirrel
Last active February 22, 2023 18:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HarlemSquirrel/701f83783486794001a75cb8cf5576ba to your computer and use it in GitHub Desktop.
Save HarlemSquirrel/701f83783486794001a75cb8cf5576ba to your computer and use it in GitHub Desktop.
Benchmarking time zone lookups
# frozen_string_literal: true
require "benchmark"
# https://github.com/zverok/wheretz
require "json" # Should be required by wheretz but it isn't currently
require "wheretz"
# https://github.com/HarlemSquirrel/tzf-rb
require "tzf"
ITERATIONS = 1000
THREAD_COUNT = 4
def lookup(lib_name:, lat:, lng:)
case lib_name
in :wheretz
# WhereTZ raises an argument error when the point is out of any known time zone.
WhereTZ.lookup(lat, lng) rescue ArgumentError
in :tzf
TZF.tz_name(lat, lng)
end
end
def iteration_lookups(lib_name:)
ITERATIONS.times do |i|
offset = (360.0 / ITERATIONS) * i
lat = (180 - offset)
lng = (180 - offset)
lookup(lib_name:, lat:, lng:)
lat = (-180 + offset)
lng = (180 - offset)
lookup(lib_name:, lat:, lng:)
lat = (180 - offset)
lng = (-180 + offset)
lookup(lib_name:, lat:, lng:)
end
end
puts "Ruby #{RUBY_VERSION}"
puts "Benchmarking #{ITERATIONS} lookups in #{THREAD_COUNT} threads..."
Benchmark.bmbm do |x|
x.report("WhereTZ.lookup") do
threads = []
THREAD_COUNT.times do
threads << Thread.new { iteration_lookups(lib_name: :wheretz) }
end
threads.each(&:join)
end
x.report("TZF.tz_name") do
threads = []
THREAD_COUNT.times do
threads << Thread.new { iteration_lookups(lib_name: :tzf) }
end
threads.each(&:join)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment