import subprocess commands_file_name = "mlir-tablegen-commands.txt" def get_target(command): # get target -o parts = command.split() for i in range(len(parts)): if parts[i] == "-o": return parts[i+1] return "???" def run_commands(path_replace, TimesToRun): Profile={} Count = 0; with open(commands_file_name) as commands_file: for line in commands_file: # extract the target that is being generated, as a key for the map. target = get_target(line) # Replace paths with target paths if path_replace != "": line = line.replace("upstream_clean", path_replace) line = line.strip() + " --time-phases 2>&1 | grep \"Total Execution\"" total_time = 0; for xx in range(TimesToRun): result = subprocess.check_output(line, shell=True, text=True).strip() # Total Execution Time: 0.0040 seconds (0.0040 wall clock) time = float(result.split()[3]) total_time += time #print(target, ":", total_time) if target in Profile: print("Key {} already exists!".format(target)) Profile[target] += time else: Profile[target] = time #Count += 1 #if Count == 10: # break; return Profile def truncate_path(p, n): if len(p) <= n: return p return p[-n:] # main code TimesToRun = 20 print("Running each command line {} times", TimesToRun) ProfileBase = run_commands("", TimesToRun) #ProfileBase = run_commands("upstream_llvm/llvm-project", TimesToRun) ProfileNew = run_commands("upstream_llvm/llvm-project", TimesToRun) #ProfileNew = run_commands("", TimesToRun) # Diff the 2 dictionaries per target, as well as total time. total_old = 0 total_new = 0 for k in ProfileBase.keys(): if not k in ProfileNew: printf("Key {} missing in new profile".format(k)) continue time_old = ProfileBase[k] time_new = ProfileNew[k] total_old += time_old total_new += time_new perc = (time_new - time_old) * 100.0 / time_old print("{0:<40} {1:<10.4f} {2:<10.4f} {3:.4f}%".format(truncate_path(k, 39), time_old, time_new, perc)) perc = (total_new - total_old) * 100.0 / total_old print("==============================================================================") print("{0:<50} {1:<10.4f} {2:<10.4f} {3:<10.4f}".format("Total time", total_old, total_new, perc))