Skip to content

Instantly share code, notes, and snippets.

@inouire
Last active July 20, 2023 10:28
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 inouire/784eba4d52717e49767759839aaa5f21 to your computer and use it in GitHub Desktop.
Save inouire/784eba4d52717e49767759839aaa5f21 to your computer and use it in GitHub Desktop.
A simple way to have a rough idea of where the time is spent for Ruby require
$requires_index = {}
if true
# Override the require method to track load times
def custom_require(file)
starting = Process.clock_gettime(Process::CLOCK_MONOTONIC)
result = original_require(file)
ending = Process.clock_gettime(Process::CLOCK_MONOTONIC)
elapsed = ending - starting
$requires_index[file] ||= {:count => 0, :elapsed => 0.0}
$requires_index[file][:count] += 1
$requires_index[file][:elapsed] += elapsed
if elapsed >= 0.01
puts "Required #{file} in #{elapsed} seconds"
end
result
end
# Replace the original 'require' method with our custom implementation
alias original_require require
alias require custom_require
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment