diff --git a/cache_magic/__init__.py b/cache_magic/__init__.py index 69a4fa3..3f17d53 100644 --- a/cache_magic/__init__.py +++ b/cache_magic/__init__.py @@ -3,6 +3,7 @@ import pickle import os +import time import hashlib import datetime import shutil @@ -118,7 +119,11 @@ def _create_new_value(self, shell, var_folder_path, var_data_path, var_info_path # calculate the new Value in user-context cmd = self._reconstruct_expression(var_name, var_value) + + # time the shell command + start_time = time.time() result = shell.run_cell(cmd) + compute_time = time.time() - start_time if not result.success: raise CacheCallException( @@ -131,7 +136,8 @@ def _create_new_value(self, shell, var_folder_path, var_data_path, var_info_path info = dict(expression_hash=self.hash_line(var_value), store_date=datetime.datetime.now(), - version=version) + version=version, + compute_time=compute_time) with open(var_info_path, 'wb') as fp: pickle.dump(info, fp) @@ -153,12 +159,15 @@ def _show_all(base_dir): try: info = CacheCall.get_from_file(var_info_path) - vars.append([var_name, size, info["store_date"], info["version"], info["expression_hash"]]) + vars.append([var_name, size, info["store_date"], + "%.1f" % info.get("compute_time", 0.0), + info["version"], info["expression_hash"]]) except IOError: print("Warning: failed to read info variable '" + var_name + "'") - display(HTML(tabulate(vars, headers=["var name", "size(byte)", "stored at date", "version", "expression(hash)"], + display(HTML(tabulate(vars, headers=["var name", "size(byte)", "stored at date", + "time(s)", "version", "expression(hash)"], tablefmt="html"))) @staticmethod