#! /usr/local/bin/python3 import time class Timer: def __enter__(self): self.start = time.time() return self def __exit__(self, a, b, c): self.stop = time.time() diff = self.stop - self.start print(f'Elapsed time: {diff} secs') def make_tarai(): cache = {} def tarai(x, y, z): if x <= y: return y key = x, y, z val = cache.get(key) if val is not None: return val val = tarai( tarai(x-1, y, z), tarai(y-1, z, x), tarai(z-1, x, y) ) cache[key] = val return val return tarai tarai = make_tarai() with Timer(): val = tarai(100, 50, 0) print(val)