From 62297132215490e9cb406e1a21f03aff40d421cb Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Sat, 30 Aug 2003 01:24:19 +0000 Subject: Eliminate unnecessary eval() in tests --- Lib/random.py | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/Lib/random.py b/Lib/random.py index 7932ac7..7210f62 100644 --- a/Lib/random.py +++ b/Lib/random.py @@ -687,17 +687,16 @@ class WichmannHill(Random): ## -------------------- test program -------------------- -def _test_generator(n, funccall): +def _test_generator(n, func, args): import time - print n, 'times', funccall - code = compile(funccall, funccall, 'eval') + print n, 'times', func.__name__ total = 0.0 sqsum = 0.0 smallest = 1e10 largest = -1e10 t0 = time.time() for i in range(n): - x = eval(code) + x = func(*args) total += x sqsum = sqsum + x*x smallest = min(x, smallest) @@ -711,21 +710,21 @@ def _test_generator(n, funccall): def _test(N=2000): - _test_generator(N, 'random()') - _test_generator(N, 'normalvariate(0.0, 1.0)') - _test_generator(N, 'lognormvariate(0.0, 1.0)') - _test_generator(N, 'vonmisesvariate(0.0, 1.0)') - _test_generator(N, 'gammavariate(0.01, 1.0)') - _test_generator(N, 'gammavariate(0.1, 1.0)') - _test_generator(N, 'gammavariate(0.1, 2.0)') - _test_generator(N, 'gammavariate(0.5, 1.0)') - _test_generator(N, 'gammavariate(0.9, 1.0)') - _test_generator(N, 'gammavariate(1.0, 1.0)') - _test_generator(N, 'gammavariate(2.0, 1.0)') - _test_generator(N, 'gammavariate(20.0, 1.0)') - _test_generator(N, 'gammavariate(200.0, 1.0)') - _test_generator(N, 'gauss(0.0, 1.0)') - _test_generator(N, 'betavariate(3.0, 3.0)') + _test_generator(N, random, ()) + _test_generator(N, normalvariate, (0.0, 1.0)) + _test_generator(N, lognormvariate, (0.0, 1.0)) + _test_generator(N, vonmisesvariate, (0.0, 1.0)) + _test_generator(N, gammavariate, (0.01, 1.0)) + _test_generator(N, gammavariate, (0.1, 1.0)) + _test_generator(N, gammavariate, (0.1, 2.0)) + _test_generator(N, gammavariate, (0.5, 1.0)) + _test_generator(N, gammavariate, (0.9, 1.0)) + _test_generator(N, gammavariate, (1.0, 1.0)) + _test_generator(N, gammavariate, (2.0, 1.0)) + _test_generator(N, gammavariate, (20.0, 1.0)) + _test_generator(N, gammavariate, (200.0, 1.0)) + _test_generator(N, gauss, (0.0, 1.0)) + _test_generator(N, betavariate, (3.0, 3.0)) # Create one instance, seeded from current time, and export its methods # as module-level functions. The functions share state across all uses -- cgit v0.12