aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/sizediff
blob: 64a61a67f6f80f75f96a7ef06f94b3a33bcd27c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/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()