[Python-checkins] benchmarks: Port PyPy's float benchmark.

brett.cannon python-checkins at python.org
Sat Sep 15 00:12:12 CEST 2012


http://hg.python.org/benchmarks/rev/21dbe371dfdc
changeset:   169:21dbe371dfdc
user:        Brett Cannon <brett at python.org>
date:        Fri Sep 14 10:54:44 2012 -0400
summary:
  Port PyPy's float benchmark.

files:
  performance/bm_float.py |  81 ++++++++++++----------------
  1 files changed, 35 insertions(+), 46 deletions(-)


diff --git a/performance/bm_float.py b/performance/bm_float.py
--- a/performance/bm_float.py
+++ b/performance/bm_float.py
@@ -1,79 +1,68 @@
-#! /usr/bin/env python
+from compat import xrange
+import util
 
-"""
-Semi-micro benchmark for floating point performance.
-
-This is a Python implementation of a floating point benchmark originally on the
-Factor language blog:
-http://factor-language.blogspot.com/2009/08/performance-comparison-between-factor.html
-
-Local changes:
-- Reduced the number of points from 5000000 to 20000. This reduces individual
-  iteration times, but we compensate by increasing the number of iterations.
-"""
-
-__author__ = "alex.gaynor at gmail.com (Alex Gaynor)"
-
-# Python imports
+from math import sin, cos, sqrt
 import optparse
 import time
-from math import sin, cos, sqrt
-
-# Local imports
-import util
-from compat import xrange, next
-
 
 class Point(object):
+
     def __init__(self, i):
         self.x = x = sin(i)
         self.y = cos(i) * 3
         self.z = (x * x) / 2
 
+    def __repr__(self):
+        return "<Point: x=%s, y=%s, z=%s>" % (self.x, self.y, self.z)
+
     def normalize(self):
-        norm = sqrt(self.x ** 2 + self.y ** 2 + self.z ** 2)
-        self.x = self.x / norm
-        self.y = self.y / norm
-        self.z = self.z / norm
+        x = self.x
+        y = self.y
+        z = self.z
+        norm = sqrt(x * x + y * y + z * z)
+        self.x /= norm
+        self.y /= norm
+        self.z /= norm
 
     def maximize(self, other):
         self.x = self.x if self.x > other.x else other.x
         self.y = self.y if self.y > other.y else other.y
         self.z = self.z if self.z > other.z else other.z
+        return self
 
 
 def maximize(points):
-    points = iter(points)
-    cur = next(points)
-    for p in points:
-        cur.maximize(p)
-    return cur
+    next = points[0]
+    for p in points[1:]:
+        next = next.maximize(p)
+    return next
 
-
-def benchmark():
-    points = []
-    for i in xrange(20000):
-        points.append(Point(i))
+def benchmark(n):
+    points = [None] * n
+    for i in xrange(n):
+        points[i] = Point(i)
     for p in points:
         p.normalize()
-    maximize(points)
+    return maximize(points)
 
+POINTS = 100000
 
-def test_float(count):
+def main(arg):
+    # XXX warmup
+    
     times = []
-    for _ in xrange(count):
+    for i in xrange(arg):
         t0 = time.time()
-        benchmark()
-        t1 = time.time()
-        times.append(t1 - t0)
+        o = benchmark(POINTS)
+        tk = time.time()
+        times.append(tk - t0)
     return times
-
-
+    
 if __name__ == "__main__":
     parser = optparse.OptionParser(
         usage="%prog [options]",
-        description="Test the performance of various floating point ops")
+        description="Test the performance of the Float benchmark")
     util.add_standard_options_to(parser)
     options, args = parser.parse_args()
 
-    util.run_benchmark(options, options.num_runs, test_float)
+    util.run_benchmark(options, options.num_runs, main)

-- 
Repository URL: http://hg.python.org/benchmarks


More information about the Python-checkins mailing list