Debian Codegraph @

test/optimized/bm_spectral_norm.py

raw symbols SCIP
1 """
2 MathWorld: "Hundred-Dollar, Hundred-Digit Challenge Problems", Challenge #3.
3 http://mathworld.wolfram.com/Hundred-DollarHundred-DigitChallengeProblems.html
4
5 The Computer Language Benchmarks Game
6 http://benchmarksgame.alioth.debian.org/u64q/spectralnorm-description.html#spectralnorm
7
8 Contributed by Sebastien Loisel
9 Fixed by Isaac Gouy
10 Sped up by Josh Goldfoot
11 Dirtily sped up by Simon Descarpentries
12 Concurrency by Jason Stitt
13 """
14
15 from six.moves import xrange, zip as izip
16
17 DEFAULT_N = 130
18
19
20 def eval_A(i, j):
21 return 1.0 / ((i + j) * (i + j + 1) // 2 + i + 1)
22
23
24 def eval_times_u(func, u):
25 return [func((i, u)) for i in xrange(len(list(u)))]
26
27
28 def eval_AtA_times_u(u):
29 return eval_times_u(part_At_times_u, eval_times_u(part_A_times_u, u))
30
31
32 def part_A_times_u(i_u):
33 i, u = i_u
34 partial_sum = 0
35 for j, u_j in enumerate(u):
36 # EDB WAS:
37 # partial_sum += eval_A(i, j) * u_j
38 ij = i + j
39 partial_sum += (1.0 / ((ij) * (ij + 1) // 2 + i + 1)) * u_j
40 return partial_sum
41
42
43 def part_At_times_u(i_u):
44 i, u = i_u
45 partial_sum = 0
46 for j, u_j in enumerate(u):
47 # EDB WAS:
48 # partial_sum += eval_A(j, i) * u_j
49 ij = i + j
50 partial_sum += (1.0 / ((ij) * (ij + 1) // 2 + j + 1)) * u_j
51 return partial_sum
52
53
54 def bench_spectral_norm(loops):
55 range_it = xrange(loops)
56 # t0 = pyperf.perf_counter()
57
58 for _ in range_it:
59 u = [1] * DEFAULT_N
60
61 for dummy in xrange(10):
62 v = eval_AtA_times_u(u)
63 u = eval_AtA_times_u(v)
64
65 vBv = vv = 0
66
67 for ue, ve in izip(u, v):
68 vBv += ue * ve
69 vv += ve * ve
70
71 return # pyperf.perf_counter() - t0
72
73
74 if __name__ == "__main__":
75 bench_spectral_norm(10)
76 # runner = pyperf.Runner()
77 # runner.metadata['description'] = (
78 # 'MathWorld: "Hundred-Dollar, Hundred-Digit Challenge Problems", '
79 # 'Challenge #3.')
80 # runner.bench_time_func('spectral_norm', bench_spectral_norm)