File: test1.py

package info (click to toggle)
psyco-doc 1.6-1
  • links: PTS
  • area: contrib
  • in suites: lenny
  • size: 1,832 kB
  • ctags: 3,236
  • sloc: ansic: 23,895; python: 5,646; perl: 1,309; makefile: 153
file content (211 lines) | stat: -rw-r--r-- 4,936 bytes parent folder | download | duplicates (7)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import os
import psyco

def time(fn, *args):
    "Measure the execution time of fn(*args)."
    import time, traceback
    begin = time.clock()
    try:
        result  = fn(*args)
    except:
        end = time.clock()
        traceback.print_exc()
        result = '<exception>'
    else:
        end = time.clock()
    return result, end-begin


ZERO = 0

def f1(n):
    "Arbitrary test function."
    i = 0
    x = 1
    while i<n:
        j = ZERO
        while j<=i:
            j = j + 1
            x = x + (i&j)
        i = i + 1
    return x

def f2(x):
    "Trivial function."
    return x*2

def f3(n):
    "Prints f2(x) on all non-null items of the sequence n."
    for x in n:
        if x == 0:
            continue
        print f2(x)

def f4(filelist):
    "Count all characters of all files whose names are in filelist."
    result = [0]*256
    for filename in filelist:
        if filename[:4] == 'BIN:': 
            f = open(filename[4:], 'rb')
        else:
            f = open(filename, 'r')
        for line in f:
            for c in line:
                k = ord(c)
                result[k] += 1
        f.close()
    d = {}
    for i in range(256):
        if result[i]:
            d[chr(i)] = result[i]
    return d

def f5(filelist):
    "Same as f4() but completely loads the files in memory."
    result = [0]*256
    for filename in filelist:
        if filename[:4] == 'BIN:': 
            f = open(filename[4:], 'rb')
        else:
            f = open(filename, 'r')
        for c in f.read():
            k = ord(c)
            result[k] += 1
        f.close()
    d = {}
    for i in range(256):
        if result[i]:
            d[chr(i)] = result[i]
    return d

def f6(n, p):
    "Computes the factorial of n modulo p."
    factorial = 1
    for i in range(2, n+1):
        factorial = (factorial * i) % p
    return factorial

def mandelbrot(c):
    z = 0j
    for i in xrange(1000):
        z = z*z + c
        if abs(z) > 2:
            if i > 126-33: i = 126-33
            return chr(33+i)
    return " "

def f7(start, end, step):
    "Computes the Mandelbrot set. All args are complex numbers."
    width = int((end.real - start.real) / step.real)
    while start.imag < end.imag:
        line = [mandelbrot(start + n*step.real) for n in range(width)]
        print ''.join(line)
        start += complex(0, step.imag)

def mandelbrotbis(c_r, c_i):
    z_r = z_i = 0.0
    for i in xrange(1000):
        z_r, z_i = z_r*z_r-z_i*z_i + c_r, 2*z_r*z_i + c_i
        if z_r*z_r+z_i*z_i > 4.0:
            if i > 126-33: i = 126-33
            return chr(33+i)
    return " "

def f7bis((start_r, start_i), (end_r, end_i), (step_r, step_i)):
    "Computes the Mandelbrot set. All args are (real, imag) tuples."
    width = int((end_r - start_r) / step_r)
    while start_i < end_i:
        line = [mandelbrotbis(start_r + n*step_r, start_i) for n in range(width)]
        print ''.join(line)
        start_i += step_i

def f8():
    try:
        x = 1.0 / 0.0
    #except StandardError, e:
    #    print "in except clause:", e
    finally:
        print "in finally clause"
        x = 5
    print "x is now", x

def f9(n):
    for i in range(n):
        print i,
    print

def f10():
    apply(f9, (50,))

def f11(n):
    def subtracter(x, y=n):
        return x-y
    return subtracter(1000)


ALLTESTS = []

def go(f, *args):
    print '-'*78
    v1, t1 = time(psyco.proxy(f), *args)
    print v1
    print '^^^ computed by Psyco in %s seconds' % t1
    v2, t2 = time(f, *args)
    if v1 == v2:
        if t1 and t2:
            ok = '%.2f times faster' % (t2/float(t1))
            s = ', Psyco is %s' % ok
        else:
            ok = s = ''
        print 'Python got the same result in %s seconds%s' % (t2, s)
    else:
        print v2
        print '^^^ by Python in %s seconds' % t2
        print '*'*78
        print '!!! different results !!!'
        ok = 'DIFFERENT RESULTS'
    ALLTESTS.append((f.func_name,t2,t1,ok))

def print_results():
    import sys
    print
    print '='*23, 'Summary (Python %d.%d.%d)' % sys.version_info[:3], '='*23
    format = '%-15s  %-15s  %-15s  %s'
    print format % ('function', 'Python time', 'Psyco time', '')
    for r in ALLTESTS:
        print format % r

def go1(arg=2117):
    go(f1, arg)

FILEPATH = '../c'
FILELIST = [os.path.join(FILEPATH, s) for s in os.listdir(FILEPATH)]
FILELIST = [s for s in FILELIST if os.path.isfile(s) and s!='psyco.dump']

def go4(arg=FILELIST):
    go(f4, arg)

def go5(arg=FILELIST):
    go(f5, arg)

def go6(n=100000, p=100000000000001L):
    go(f6, n, p)

def go7(start=-2-1j, end=1+1j, step=0.04+0.08j):
    go(f7, start, end, step)

def go7bis(start=(-2.0, -1.0), end=(1.0, 1.0), step=(0.04, 0.08)):
    go(f7bis, start, end, step)


if __name__ == "__main__":
    go(f11, 50)
    go1()
    go4()
    go5()
    go6()
    go7()
    go7bis()
    go(f10)
    psyco.dumpcodebuf()
    print_results()