aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/sizediff
blob: 84cdea4494d66244a7cba0eb35bb40b15863ed39 (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
#!/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, code0, data0, bss0, code1, data1, bss1):
        self.command = command
        self.code0 = code0
        self.data0 = data0
        self.bss0 = bss0
        self.code1 = code1
        self.data1 = data1
        self.bss1 = bss1

    @property
    def flash0(self):
        return self.code0 + self.data0

    @property
    def flash1(self):
        return self.code1 + self.data1

    @property
    def codediff(self):
        return self.code1 - self.code0

    @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 = map(int, lines[i+1].split()[:3])
        command = lines[i-1].strip()
        sizes.append({
            'command': command,
            'code':    code,
            'data':    data,
            'bss':     bss,
        })
    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]['code'], sizes0[i]['data'], sizes0[i]['bss'], sizes1[i]['code'], sizes1[i]['data'], sizes1[i]['bss']))
    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)
    totalCode0 = 0
    totalCode1 = 0
    totalDiff = 0
    print(' before   after   diff')
    for comparison in comparisons:
        print('%7d %7d %6d %6.2f%%  %s' % (comparison.flash0, comparison.flash1, comparison.flashdiff, comparison.flashdiff / comparison.flash0 * 100, comparison.command))
        totalCode0 += comparison.flash0
        totalCode1 += comparison.flash1
        totalDiff += comparison.flashdiff
    print('%7d %7d %6d %6.2f%%  sum' % (totalCode0, totalCode1, totalDiff, totalDiff / totalCode0 * 100))


if __name__ == '__main__':
    main()