-
Notifications
You must be signed in to change notification settings - Fork 0
/
ptx-stats.py
executable file
·49 lines (41 loc) · 1.52 KB
/
ptx-stats.py
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
#!/bin/env python
# -*- coding: utf-8 -*-
###############################################################################
# File: ptx-stats.py
###############################################################################
"""Tabulate PTX assembly instruction calls.
"""
from __future__ import (division, absolute_import, print_function)
#-----------------------------------------------------------------------------#
import re
import sys
from collections import defaultdict
###############################################################################
START_RE = re.compile(r'^\s*\{\s*$')
CMD_RE = re.compile(r'^\s*(\w+)\.')
def process(f):
result = defaultdict(int)
for line in f:
if START_RE.match(line):
break
for line in f:
match = CMD_RE.match(line)
if match is not None:
result[match.group(1)] += 1
return dict(result)
def main():
(_, filename) = sys.argv
if filename == '-':
instructions = process(sys.stdin)
else:
with open(filename, 'r') as f:
instructions = process(f)
for k in sorted(instructions):
print("{:10s} {:4d}".format(k, instructions[k]))
print("{:10s} {:4d}".format("TOTAL", sum(instructions.values())))
#-----------------------------------------------------------------------------#
if __name__ == '__main__':
main()
###############################################################################
# end of ptx-stats.py
###############################################################################