OILS / mycpp / examples / files.py View on Github | oilshell.org

64 lines, 38 significant
1#!/usr/bin/env python2
2"""
3files.py
4"""
5from __future__ import print_function
6
7import os
8import sys
9
10from mycpp import mylib
11from mycpp.mylib import log
12
13
14def run_tests():
15 # type: () -> None
16
17 f = mylib.BufWriter()
18 for i in xrange(30):
19 f.write(chr(i + 65))
20
21 contents = f.getvalue()
22 log('Wrote %d bytes to StringIO', len(contents))
23
24 # the f(g(), h()) problem with mark and sweep!!!
25 if 1:
26 log('contents = %s ... %s', contents[:10], contents[-10:])
27
28 # Does not have the problem
29 if 0:
30 a = contents[:10]
31 b = contents[-10:]
32 log('contents = %s ... %s', a, b)
33
34 f2 = mylib.Stdout()
35 f2.write('stdout\n')
36
37
38def run_benchmarks():
39 # type: () -> None
40 n = 10000
41
42 result = 0
43
44 i = 0
45 while i < n:
46 f = mylib.BufWriter()
47 for j in xrange(30):
48 f.write(chr(j + 65))
49
50 result += len(f.getvalue())
51
52 mylib.MaybeCollect()
53
54 i += 1
55 log('Ran %d iterations', n)
56 log('result = %d', result)
57
58
59if __name__ == '__main__':
60 if os.getenv('BENCHMARK'):
61 log('Benchmarking...')
62 run_benchmarks()
63 else:
64 run_tests()