Skip to content

Instantly share code, notes, and snippets.

@nycjv321
Last active January 4, 2016 10:49
Show Gist options
  • Save nycjv321/3f2afb163fce12b152f3 to your computer and use it in GitHub Desktop.
Save nycjv321/3f2afb163fce12b152f3 to your computer and use it in GitHub Desktop.
Sleep logarithmic(ly) ?
# Performs a logarithmic sleep based on a set of integers or up to an input integer
def logarithmic_sleep(set, constant_multiplier=1, stepping_factor=1, debugging=false)
require 'logger'
logger = Logger.new(STDOUT)
logger.level = Logger::DEBUG
total_sleep_time = 0
if !set.is_a? Array and !set.is_a? Range
set = (1 .. set)
end
logger.debug("Constant multiplier for logarithmic scale is set to #{constant_multiplier}") if debugging
logger.debug("Stepping factor is #{stepping_factor}") if stepping_factor > 1 and stepping_factor != set.size and debugging
set.step(stepping_factor) do |x|
logarithm = Math.log x
sleep_time = logarithm * constant_multiplier
logger.debug("Iteration #{x} sleeping for #{sleep_time} seconds (zZz)") if debugging
yield sleep_time
sleep(sleep_time)
total_sleep_time += sleep_time
end
logger.debug("Slept for #{total_sleep_time} seconds (zZzZz)") if debugging
end
logarithmic_sleep(20, 1, 2) do |y|
puts y
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment