Ruby equivalent of Ipython% timeit
TL, dr; How can I find out how long Ruby is taking?
Ipython %timeit
and %%timeit
are two of my most used IPython Magic commands (A Python Interactive shell). Usually %timeit
no different from a function like this:
def timeit(f, *args, **kwargs):
num_trials = estimate_number_of_trials_for_f(f, args, kwargs)
start = time.time()
for i in range(num_trials):
f(args, kwargs)
return time.time() - start
The advantage is that something like this can be done interactively (not a great use case I know of):
In [119]: %%timeit
.....: data = json.loads(json_data)
.....: ret = process_data(data)
.....: json.dumps(ret)
I was wondering if there is a ruby โโequivalent to them (irb or pry) or an idiomatic way to implement an equivalent at all (timeit that only accepts one method is not exactly the same, it would be nice to get the magic equivalent of% timeit for any arbitrary block of code like in the next example).
source to share
Benchmark -
http://ruby-doc.org/stdlib-2.2.2/libdoc/benchmark/rdoc/Benchmark.html#method-c-benchmark
can help you with this.
Example:
require "benchmark"
how_long = Benchmark.measure do
(1..100).each { |i| i }
end
puts how_long
source to share