Skip to content

Instantly share code, notes, and snippets.

@groony
Forked from vertexclique/time_slice.rb
Created September 17, 2019 07:41
Show Gist options
  • Save groony/e9f2d339d4d6233b535fb43c7754640f to your computer and use it in GitHub Desktop.
Save groony/e9f2d339d4d6233b535fb43c7754640f to your computer and use it in GitHub Desktop.
Time Slice for Ruby methods (milliseconds)
module TimeSlice
def self.time_slice(slice)
bgntime = Time.now
id = 0
loop do
id=id+1
puts "#{id} works"
# do your work here like above line
# and pass milliseconds for "slice" var
endtime = Time.now
if (((endtime - bgntime)*1000) > Integer(slice))
break
end
end
puts "Time slice ended"
end
end
# 5000 milliseconds time slice
TimeSlice.time_slice(5000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment