Skip to content

Instantly share code, notes, and snippets.

@mijoharas
Created August 30, 2016 16:04
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 mijoharas/f7968a546d4681ab7f5a83115f5bf2d7 to your computer and use it in GitHub Desktop.
Save mijoharas/f7968a546d4681ab7f5a83115f5bf2d7 to your computer and use it in GitHub Desktop.
profiling ruby file approaches
require 'ruby-prof'
filename = '/tmp/example.txt'
File.delete(filename)
# profile first method
RubyProf.start
(1..100_000).each do |i|
File.open(filename, 'ab') do |file|
file.puts "line: #{i}"
end
end
result = RubyProf.stop
filename = '/tmp/example2.txt'
# profile second
RubyProf.start
@file = File.open(filename, 'wb')
(1..100_000).each do |i|
@file.puts "line: #{i}"
end
@file.close
result2 = RubyProf.stop
# print
printer = RubyProf::GraphPrinter.new(result)
printer2 = RubyProf::GraphPrinter.new(result2)
printer.print(STDOUT, {})
printer2.print(STDOUT, {})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment