#!/usr/bin/env python3 # Small tool to compare code size between TinyGo runs (with the -size=short flag). import sys class Comparison: def __init__(self, command, flash0, ram0, flash1, ram1): self.command = command self.flash0 = flash0 self.ram0 = ram0 self.flash1 = flash1 self.ram1 = ram1 @property def ramdiff(self): return self.ram1 - self.ram0 @property def flashdiff(self): return self.flash1 - self.flash0 def readSizes(path): sizes = [] lines = open(path).readlines() for i in range(len(lines)): if not lines[i].strip().startswith('code '): continue # found a size header code, data, bss, flash, ram = map(int, lines[i+1].replace("|", "").split()) command = lines[i-1].strip() sizes.append({ 'command': command, 'flash': flash, 'ram': ram }) return sizes def main(): path0 = sys.argv[1] path1 = sys.argv[2] sizes0 = readSizes(path0) sizes1 = readSizes(path1) comparisons = [] for i in range(len(sizes0)): if i >= len(sizes1): print('%s has more commands than %s' % (path0, path1)) print(' ', sizes0[i]['command']) break if sizes0[i]['command'] != sizes1[i]['command']: print('not the same command!') print(' ', sizes0[i]['command']) print(' ', sizes1[i]['command']) comparisons.append(Comparison(sizes0[i]['command'], sizes0[i]['flash'], sizes0[i]['ram'], sizes1[i]['flash'], sizes1[i]['ram'])) if len(sizes0) < len(sizes1): print('%s has more commands than %s' % (path1, path0)) print(' ', sizes1[len(sizes0)]['command']) comparisons.sort(key=lambda x: x.flashdiff) totalFlash0 = 0 totalFlash1 = 0 totalRam0 = 0 totalRam1 = 0 totalDiff = 0 totalRamDiff = 0 totalProduct = 1 totalRamProduct = 1 print(' flash ram') print(' before after diff before after diff') for comparison in comparisons: diffPct = comparison.flashdiff / comparison.flash0 diffRamPct = comparison.ramdiff / comparison.ram0 print('%7d %7d %6d %6.2f%% %7d %7d %6d %6.2f%% %s' % (comparison.flash0, comparison.flash1, comparison.flashdiff, diffPct * 100, comparison.ram0, comparison.ram1, comparison.ramdiff, diffRamPct * 100, comparison.command)) totalFlash0 += comparison.flash0 totalFlash1 += comparison.flash1 totalDiff += comparison.flashdiff totalProduct *= (1 + diffPct) totalRam0 += comparison.ram0 totalRam1 += comparison.ram1 totalRamDiff += comparison.ramdiff totalRamProduct *= (1 + diffRamPct) geomean = totalProduct ** (1.0 / float(len(comparisons))) geomeanRam = totalRamProduct ** (1.0 / float(len(comparisons))) print('%7d %7d %6d %6.2f%% %7d %7d %6d %6.2f%%' % (totalFlash0, totalFlash1, totalDiff, geomean - 1, totalRam0, totalRam1, totalRamDiff, geomeanRam - 1)) if __name__ == '__main__': main()