# Emulating Ruby's Benchmark.real\_time in Python Using a Context Manager

DevFeed: [Emulating Ruby's Benchmark.real\_time in Python Using a Context Manager](<https://devfeed.tech/articles/emulating-ruby-s-benchmark-real-time-in-python-using-a-context-manager-28230.md>)

Original publisher: [Read original article](<http://fuzzyblog.io/blog/python/2020/03/06/emulating-ruby-s-benchmark-realtime-in-python-using-a-context-manager.html>)

Author: Fuzzygroup

Published: 2020-03-06T00:00:00Z

Content type: tutorial

Language: en

Sources: [Scott Johnson](<https://devfeed.tech/sources/scott-johnson.md>)

Topics: [benchmarking](<https://devfeed.tech/topics/benchmarking.md>), [Python](<https://devfeed.tech/topics/python.md>), [Ruby](<https://devfeed.tech/topics/ruby.md>), [Programming](<https://devfeed.tech/topics/programming.md>), [Code](<https://devfeed.tech/topics/code.md>)

Tags: [benchmark](<https://devfeed.tech/tags/benchmark.md>), [benchmarking](<https://devfeed.tech/tags/benchmarking.md>), [code](<https://devfeed.tech/tags/code.md>), [programming](<https://devfeed.tech/tags/programming.md>), [python](<https://devfeed.tech/tags/python.md>), [ruby](<https://devfeed.tech/tags/ruby.md>), [rust](<https://devfeed.tech/tags/rust.md>)

## AI overview

This tutorial shows how to emulate Ruby's Benchmark.realtime in Python with a context manager. It introduces a timer using Python's contextlib.contextmanager and time modules, then applies the approach in a production refactor to reduce code complexity and duplication.

## Source excerpt

One of the most confusing aspects of multi language programming is when you know how to do it in your preferred language and when you are trying to figure it out in your new language. And, as any reader of this blog knows, Ruby is always, always, always my preferred language. But, as my perspective on languages widens, I suspect you could say that: Ruby is my wife but I have a serious relationship with Python And I'm now flirting with Rust and seeing if I want to bring her to the dance ... But enough of that foolishness, today's topic is code benchmarking. Here's what I would do in Ruby: benchmarks = {} results = nil elapsed_time = Benchmark.realtime do results = some_long_running_method(foo, bar, baz) end benchmarks['some_long_running_method'] = elapsed_time By defining the results variable outside the block of code (the bits between do and end), it basically gets a transitive scope that allows what is computed inside the block to exist outside the block. And then I set the associative array (also known as a hash) to have a key for the method, some_long_running_method, and also the elapsed time. This way I can build up a hash of all my methods and how long they take. Note: A block is a "lexical scope" during which (the code between the do and end), variables are local only to that scope (unless you "shadow" it with a variable of the same name). My pairing partner on this project, Grant, has told me "You could use a context manager for this". And this led me to these Python docs: ContextLib Geeks for Geeks I found both of these very pythonic - which, for me, means I didn't understand them at all. Happily I was able to cobble together a basic example: from contextlib import contextmanager import time def some_long_running_method1(amount): print(amount) def some_long_running_method2(amount): print(amount) @contextmanager def timer(benchmarks, benchmark_key): t_0 = time.time() yield run_time = time.time() - t_0 benchmarks[benchmark_key] = run_time benchmarks = {} with ti