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 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
|
#from __future__ import generators
#from __future__ import division
import psyco, os, sys, array, operator, traceback
from time import sleep, clock
def time(fn, *args):
"Measure the execution time of fn(*args)."
begin = clock()
try:
result = apply(fn, args)
except:
end = clock()
traceback.print_exc()
result = '<exception>'
else:
end = 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:
f = open(filename, 'r')
for line in f:
for c in line:
k = ord(c)
result[k] = 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:
f = open(filename, 'r')
for c in f.read():
k = ord(c)
result[k] = 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):
za, zb = c.real, c.imag
for i in range(1000):
za2 = za*za
zb2 = zb*zb
if za2+zb2 > 4.0:
if i > 126-33: i = 126-33
return chr(33+i)
za, zb = za2-zb2+c.real, 2*za*zb+c.imag
del za2, zb2
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 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 go(f, *args):
print '-'*80
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:
s = ', Psyco is %.2f times faster' % (t2/float(t1))
else:
s = ''
print 'Python got the same result in %s seconds%s' % (t2, s)
else:
print v2
print '^^^ by Python in %s seconds' % t2
print '*'*80
print '!!! different results !!!'
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 f11(prefix):
#prefix = [60, 115, 116, 114, 114, 114, 114, 62]
table = [-1] + ([0]*len(prefix))
for i in range(len(prefix)):
table[i+1] = table[i]+1
while table[i+1] > 0 and prefix[i] != prefix[table[i+1]-1]:
table[i+1] = table[table[i+1]-1]+1
#code.extend(table[1:]) # don't store first entry
print table[1:]
def f12(arg):
def g(x, y=arg):
return x+y
return g(123)
def f13(arg):
x,y = arg
print x, y
u,v = x,y
print u, v
return v,u
def f15(x):
print x
def f14(n):
f15(n==5)
def f16(x):
return ~x
def f17(x, y=0, *args):
return x, y, args
#def f18():
# yield 5
# yield 6
def f19(seq):
class C:
def __init__(self):
print "hello"
c = C()
seq = list(seq)
seq1 = seq
seq *= 3
n = seq[0]
n /= 3
return seq, seq1 is seq, n
def f20(code, n, fill=0):
a = array.array(code, [fill]*n)
for i in range(n):
item = i*10
if code == 'c':
item = chr(item)
a[i] = item
return [a[i] for i in range(len(a))]
def f21(lst):
return operator.indexOf(lst, 9)
def f22(n):
try:
x = 1/n
except:
print "Catch!"
def f23(n):
f22(n)
try:
x = undefined_name
except:
f22(n)
print sys.exc_type.__name__
def f24(n):
return 1/n
def f25(n):
try:
return f24(n)
except AttributeError:
auwsziazsi
def f26():
import psyco.classes
import array
import urllib
from types import FloatType, ListType
from psyco.core import full
return FloatType, ListType, urllib, psyco.classes, full
def test_getframe():
import sys
i = 0
print 'test_getframe():'
while 1:
try:
f = sys._getframe(i)
except ValueError:
break
#print '%-26s %-60s %-40s' % (f, f.f_code, f.f_locals.keys())
print f.f_code.co_name.replace('<module>', '?')
i += 1
def test_getframe1():
return test_getframe()
psyco.cannotcompile(test_getframe1)
def test_getframe_b():
import sys
i = 0
print 'test_getframe_b():'
f = sys._getframe()
while f is not None:
print f.f_code.co_name.replace('<module>', '?')
f = f.f_back # walk the stack with f_back
def test_getframe_b1():
return test_getframe_b()
psyco.cannotcompile(test_getframe_b1)
def f28():
test_getframe()
return N
def f27():
global N
N = 5
a = f28()
N = 6
b = f28()
N = 7
c = f28()
return a,b,c
def f28_1():
test_getframe1()
return N
def f27_1():
global N
N = 51
a = f28_1()
N = 61
b = f28_1()
N = 71
c = f28_1()
return a,b,c
def f28_b():
test_getframe_b()
return N
def f27_b():
global N
N = 95
a = f28_b()
N = 96
b = f28_b()
N = 97
c = f28_b()
return a,b,c
def f28_b1():
test_getframe_b1()
return N
def f27_b1():
global N
N = 951
a = f28_b1()
N = 961
b = f28_b1()
N = 971
c = f28_b1()
return a,b,c
def f29(lst):
lst = lst[:]
del lst[2]
lst[3] = 6
return lst
def f30():
Baz
def f31():
f30()
def f32(p):
if p[1:2] == ':':
return p[0:2], p[2:]
return '', p
def f33(n):
if ((n+1) & n) == 0:
print n
def f34(m=2000000):
for i in xrange(m):
f33(i)
def f35():
try:
import Numeric
except ImportError:
print "[[5 5 5]"
print " [5 5 5]"
print " [5 5 5]]"
else:
a = Numeric.zeros((3,3))
a += 5
print a
def f36():
class X:
pass
class Y(X):
pass
return Y(), Y.__module__, __in_psyco__
def f37(x):
print 'ZERO' in globals().keys()
dir()
print eval('ZERO+1')
def f38(n):
f = f1
r = [
f(n, *[]),
f(n),
apply(f, (n,)),
apply(f, [n]),
f(*(n,)),
f(*[n]),
f(n=n),
f(**{'n': n}),
apply(f, (n,), {}),
apply(f, [n], {}),
f(*(n,), **{}),
f(*[n], **{}),
f(n, **{}),
f(n, *[], **{}),
f(n=n, **{}),
f(n=n, *[], **{}),
f(*(n,), **{}),
f(*[n], **{}),
f(*[], **{'n':n}),
]
return r
if __name__ == "__main__":
print "Break!"
sleep(0.5)
#go(f36)
#go(f38, 100)
psyco.full()
try:
print f35()
#f23(0.0)
finally:
psyco.dumpcodebuf()
|