[pypy-commit] extradoc extradoc: sqrt benchmark from blogpost

hakanardo noreply at buildbot.pypy.org
Wed Jun 8 21:08:33 CEST 2011


Author: Hakan Ardo <hakan at debian.org>
Branch: extradoc
Changeset: r3620:07dc50054298
Date: 2011-06-08 20:09 +0200
http://bitbucket.org/pypy/extradoc/changeset/07dc50054298/

Log:	sqrt benchmark from blogpost

diff --git a/talk/iwtc11/benchmarks/benchmark.sh b/talk/iwtc11/benchmarks/benchmark.sh
new file mode 100755
--- /dev/null
+++ b/talk/iwtc11/benchmarks/benchmark.sh
@@ -0,0 +1,14 @@
+#!/bin/sh
+
+echo
+echo $*
+if [ $1 == "gcc" ]; then
+    $* sqrt/sqrt_double.c; /usr/bin/time -f %e ./a.out > /dev/null
+    $* sqrt/sqrt_long.c; /usr/bin/time -f %e ./a.out > /dev/null
+    $* sqrt/sqrt_fix16.c; /usr/bin/time -f %e ./a.out > /dev/null
+    rm a.out
+else
+    $* sqrt/time_sqrt.py float
+    $* sqrt/time_sqrt.py int
+    $* sqrt/time_sqrt.py Fix16
+fi
diff --git a/talk/iwtc11/benchmarks/runall.sh b/talk/iwtc11/benchmarks/runall.sh
new file mode 100755
--- /dev/null
+++ b/talk/iwtc11/benchmarks/runall.sh
@@ -0,0 +1,9 @@
+#!/bin/sh
+
+./benchmark.sh pypy
+./benchmark.sh pypy --jit enable_opts=intbounds:rewrite:virtualize:heap:unroll
+./benchmark.sh pypy --jit enable_opts=intbounds:rewrite:virtualize:heap
+./benchmark.sh gcc
+./benchmark.sh gcc -O2
+./benchmark.sh gcc -O3 -march=native
+./benchmark.sh python
diff --git a/talk/iwtc11/benchmarks/sqrt/sqrt.py b/talk/iwtc11/benchmarks/sqrt/sqrt.py
new file mode 100644
--- /dev/null
+++ b/talk/iwtc11/benchmarks/sqrt/sqrt.py
@@ -0,0 +1,50 @@
+def sqrt(y, n=10000):
+    x = y / 2
+    while n > 0:
+        assert y > 0 and x > 0
+        n -= 1
+        x = (x + y/x) / 2
+    return x
+
+class Fix16(object):
+    def __init__(self, val, scale=True):
+        if isinstance(val, Fix16):
+            self.val = val.val
+        else:
+            if scale:
+                self.val = int(val * 2**16)
+            else:
+                self.val = val
+
+    def __add__(self, other):
+        return  Fix16(self.val + Fix16(other).val, False)
+
+    def __sub__(self, other):
+        return  Fix16(self.val - Fix16(other).val, False)
+
+    def __mul__(self, other):
+        return  Fix16((self.val >> 8) * (Fix16(other).val >> 8), False)
+
+    def __div__(self, other):
+        return  Fix16((self.val << 16) / Fix16(other).val, False)
+
+
+    def __float__(self):
+        return float(self.val) / float(2**16)
+
+    def __int__(self):
+        return self.val >> 16
+
+    def __cmp__(self, other):
+        return cmp(self.val, Fix16(other).val)
+
+    def __str__(self):
+        return str(float(self))
+
+    __radd__ = __add__
+    __rmul__ = __mul__
+    def __rsub__(self, other):
+        return  Fix16(Fix16(other).val - self.val, False)
+    def __rdiv__(self, other):
+        return  Fix16((Fix16(other).val << 16) / self.val, False)
+
diff --git a/talk/iwtc11/benchmarks/sqrt/sqrt_double.c b/talk/iwtc11/benchmarks/sqrt/sqrt_double.c
new file mode 100644
--- /dev/null
+++ b/talk/iwtc11/benchmarks/sqrt/sqrt_double.c
@@ -0,0 +1,14 @@
+#include <stdio.h>
+
+int main() {
+  double y = 1234.0;
+  double x = y / 2.0;
+  long n = 100000000;
+  while (n>0) {
+    n -= 1;
+    x = (x + y/x) / 2.0;
+  }
+  printf("%f\n", x);
+  fprintf(stderr, "sqrt(float):   ");
+  return 0;
+}
diff --git a/talk/iwtc11/benchmarks/sqrt/sqrt_fix16.c b/talk/iwtc11/benchmarks/sqrt/sqrt_fix16.c
new file mode 100644
--- /dev/null
+++ b/talk/iwtc11/benchmarks/sqrt/sqrt_fix16.c
@@ -0,0 +1,14 @@
+#include <stdio.h>
+
+int main() {
+  long y = 1234 << 16;
+  long x = y / 2;
+  long n = 100000000;
+  while (n>0) {
+    n -= 1;
+    x = ((x + (y << 16)/x)) / 2;
+  }
+  printf("%f\n", ((double) x) / ((double) (1<<16)));
+  fprintf(stderr, "sqrt(Fix16):   ");
+  return 0;
+}
diff --git a/talk/iwtc11/benchmarks/sqrt/sqrt_long.c b/talk/iwtc11/benchmarks/sqrt/sqrt_long.c
new file mode 100644
--- /dev/null
+++ b/talk/iwtc11/benchmarks/sqrt/sqrt_long.c
@@ -0,0 +1,14 @@
+#include <stdio.h>
+
+int main() {
+  long y = 1234;
+  long x = y / 2;
+  long n = 100000000;
+  while (n>0) {
+    n -= 1;
+    x = (x + y/x) / 2;
+  }
+  printf("%d\n", x);
+  fprintf(stderr, "sqrt(int):     ");
+  return 0;
+}
diff --git a/talk/iwtc11/benchmarks/sqrt/test_sqrt.py b/talk/iwtc11/benchmarks/sqrt/test_sqrt.py
new file mode 100644
--- /dev/null
+++ b/talk/iwtc11/benchmarks/sqrt/test_sqrt.py
@@ -0,0 +1,6 @@
+import math
+from sqrt import sqrt, Fix16
+
+for i in range(2,10):
+    print i, sqrt(i), '%4.2f' % sqrt(float(i)), \
+          '%4.2f' % float(sqrt(Fix16(i))), '%4.2f' % math.sqrt(i)
diff --git a/talk/iwtc11/benchmarks/sqrt/time_sqrt.py b/talk/iwtc11/benchmarks/sqrt/time_sqrt.py
new file mode 100644
--- /dev/null
+++ b/talk/iwtc11/benchmarks/sqrt/time_sqrt.py
@@ -0,0 +1,17 @@
+import sys, time
+from sqrt import sqrt, Fix16
+
+try:
+    import pypyjit
+    pypyjit.set_param(trace_limit=20000)
+except ImportError:
+    pass
+
+type1 = eval(sys.argv[1])
+a = time.time()
+sqrt(type1(123456), 100000000)
+b = time.time()
+name = 'sqrt(%s):' % sys.argv[1]
+print '%12s  ' % name, b - a
+
+    


More information about the pypy-commit mailing list