This is a benchmark of operator overloading in boo. Benchmarks a=
re shown for boo without operator overloading, boo with function calls in p=
lace of arithmetic operators, boo with operator overloading using a simple =
struct called "myNum", boo with operator overloading using a clas=
s instead of a struct, boo "duck-typed" operators, and Python (wi=
thout operator overloading). The benchmark computes a piece of the Mandelbr=
ot set. The times to complete the benchmark on a 1.7GHz Pentium M IBM T41p =
running Windows XP Professional SP1:

=20
=20
=20
=20
=20
=20
=20
=20
=20
=20
=20
=20
=20
=20
=20
=20
=20
=20
=20
=20
=20
=20
=20
=20
=20
=20
=20
=20
=20
=20
=20
=20
=20
=20
=20
=20

=20
Benchmark | Normalized time | Actual time (secs) |
---|---|---|

boo - no overloaded operators | 1 | 0.35 |

boo - function calls | 3 | 1.07 |

boo - overloaded operators (struct version) = | 16 | 5.45 |

boo - overloaded operators (class version) <= /p> | 17-20 | 5.8 - 7 |

ActivePython 2.3.2 | 74 | 25.75 |

boo - duck operators | 189 | 66 |

At its best the class version is only marginally slower than the struct = version. However, the variability of the class-based version was much highe= r, sometimes it took up to 7 seconds to run, presumably because garbage col= lection kicked in. 190,917,418 myNum objects were created while running the= class-based version. Only 129,586 myNums were created while running the st= ruct-based version. Its surprising how much overhead the struct "copy = semantics" incurs - the struct version is 5 times slower than the func= tion calls version.

=20The benchmark code is reproduced below for normal operators, function calls, overloaded operators - struct version, overloaded operato= rs - class version, duc= k operators, and Pyth= on. The benchmark computes a square of the Mandelbrot set 250 by 250 pi= xels wide.

=20=20

=20

/* boo unoverloaded version - Mandelbrot Benchmark by Bill Wood */ def mb_d(xl as double, xs as double, yl as double, ys as double, ix as int,= iyl as int, iyh as int, r as double, iterations as int, buf as (short)): bp =3D 0 for iy in range(iyl, iyh): cx =3D xl + ix * xs ci =3D yl + iy * ys rx2 =3D ri2 =3D rx =3D ri =3D 0.0 count =3D 0 while ((rx2 + ri2) <=3D r) and (count < iterations): ri =3D (rx + rx) * ri + ci rx =3D rx2 - ri2 + cx rx2 =3D rx * rx ri2 =3D ri * ri count +=3D 1 if (rx2 + ri2 > r) and (count <=3D iterations): buf[bp] =3D count bp +=3D 1 else: buf[bp] =3D 0 bp +=3D 1 def main(argv as (string)): xl =3D -0.74526593488600 yl =3D 0.11303858131900 xh =3D -0.74525997120900 yh =3D 0.11304454499600 r =3D 4.0 size =3D int.Parse(argv[0]) iterations =3D int.Parse(argv[1]) print ("size =3D $size, iterations =3D $iterations") buf =3D array(short, size) xs =3D (xh - xl) / (size - 1) ys =3D (yh - yl) / (size - 1) start =3D date.Now for ix in range(0, size): mb_d(xl, xs, yl, ys, ix, 0, size, r, iterations, buf) elapsed =3D date.Now.Subtract(start) print ("Boo elapsed time =3D $elapsed") for i in buf: System.Console.Write(i + " ") main(("250", "1000"))=20

=20

=20

/* boo myNum function calls version - Mandelbrot Benchmark by Bill Wood */ def op_Multiply(x as double, j as int): =09return x*j def op_Multiply(x as double, y as double): =09return x*y def op_Division(x as double, y as double): =09return x/y def op_Addition(x as double, j as int): =09return x + j def op_Addition(x as double, y as double): =09return x + y def op_Subtraction(x as double, j as int): =09return x - j def op_Subtraction(x as double, y as double): =09return x - y def op_GreaterThan(x as double, y as double): =09return x > y def op_LessThan(x as double, y as double): =09return x < y def op_LessThanOrEqual(x as double, y as double): =09return x <=3D y def mb_d(xl as double, xs as double, yl as double, ys as double, ix as int,= \ =09=09iyl as int, iyh as int, r as double, iterations as int, buf as (short= )): =09bp =3D 0 =09iy =3D iyl =09while op_LessThan(iy, iyh): =09=09cx =3D op_Addition(xl, op_Multiply(ix, xs)) =09=09ci =3D op_Addition(yl, op_Multiply(iy, ys)) =09=09rx2 =3D ri2 =3D rx =3D ri =3D 0.0 =09=09count =3D 0 =09=09while (op_LessThanOrEqual(op_Addition(rx2, ri2), r) and op_LessThan(c= ount, iterations)): =09=09=09ri =3D op_Addition(op_Multiply(op_Addition(rx, rx), ri), ci) =09=09=09rx =3D op_Addition(op_Subtraction(rx2, ri2), cx) =09=09=09rx2 =3D op_Multiply(rx, rx) =09=09=09ri2 =3D op_Multiply(ri, ri) =09=09=09count =3D op_Addition(count, 1) =09=09if (op_GreaterThan(op_Addition(rx2, ri2), r) and op_LessThanOrEqual(c= ount, iterations)): =09=09=09buf[bp] =3D count =09=09=09bp =3D op_Addition(bp, 1) =09=09else: =09=09=09buf[bp] =3D 0 =09=09=09bp =3D op_Addition(bp, 1) =09=09iy =3D op_Addition(iy, 1) def main(argv as (System.String)): =09xl =3D -0.74526593489 =09yl =3D 0.11303858132 =09xh =3D -0.74525997121 =09yh =3D 0.113044545 =09r =3D 4.0 =09size =3D int.Parse(argv[0]) =09iterations =3D int.Parse(argv[1]) =09print("size =3D $size, iterations =3D $iterations") =09buf =3D array(short, size) =09xs =3D op_Division(op_Subtraction(xh, xl), op_Subtraction(size, 1)) =09ys =3D op_Division(op_Subtraction(yh, yl), op_Subtraction(size, 1)) =09start =3D date.Now =09ix =3D 0 =09while op_LessThan(ix, size): =09=09mb_d(xl, xs, yl, ys, ix, 0, size, r, iterations, buf) =09=09ix =3D op_Addition(ix, 1) =09elapsed =3D date.Now - start =09print ("Boo elapsed time =3D $elapsed") =09for i in buf: =09=09System.Console.Write(i + " ") main(("250", "1000"))=20

=20

=20

/* boo myNum struct version - Mandelbrot Benchmark by Bill Wood */ struct myNum: =09public static n as int =09public i as double =09def constructor(j as int): =09=09i =3D j =09=09++n =09def constructor(y as double): =09=09i =3D y =09=09++n =09def constructor(x as myNum): =09=09i =3D x.i =09=09++n =09static def op_Multiply(x as myNum, j as int): =09=09x.i =3D x.i * j =09=09return x =09static def op_Multiply(x as myNum, y as myNum): =09=09x.i =3D x.i * y.i =09=09return x =09static def op_Division(x as myNum, y as myNum): =09=09x.i =3D x.i / y.i =09=09return x =09static def op_Addition(x as myNum, j as int): =09=09x.i =3D x.i + j =09=09return x =09static def op_Addition(x as myNum, y as myNum): =09=09x.i =3D x.i + y.i =09=09return x =09static def op_Subtraction(x as myNum, j as int): =09=09x.i =3D x.i - j =09=09return x =09static def op_Subtraction(x as myNum, y as myNum): =09=09x.i =3D x.i - y.i =09=09return x =09static def op_GreaterThan(x as myNum, y as myNum): =09=09return x.i > y.i =09static def op_LessThan(x as myNum, y as myNum): =09=09return x.i < y.i =09static def op_LessThanOrEqual(x as myNum, y as myNum): =09=09return x.i <=3D y.i =09def ToString(): =09=09return i.ToString() def mb_d(xl as myNum, xs as myNum, yl as myNum, ys as myNum, ix as myNum,\ =09iyl as myNum, iyh as myNum, r as myNum, iterations as myNum, buf as (myN= um)): bp =3D myNum(0) iy =3D iyl while iy < iyh: cx =3D xl + ix * xs ci =3D yl + iy * ys rx2 =3D ri2 =3D rx =3D ri =3D myNum(0) count =3D myNum(0) while ((rx2 + ri2) <=3D r) and (count < iterations): ri =3D (rx + rx) * ri + ci rx =3D rx2 - ri2 + cx rx2 =3D rx * rx ri2 =3D ri * ri count +=3D 1 if (rx2 + ri2 > r) and (count <=3D iterations): buf[bp.i] =3D count bp +=3D 1 else: buf[bp.i] =3D myNum(0) bp +=3D 1 iy +=3D 1 def main(argv as (string)): xl =3D myNum(-0.74526593488600) yl =3D myNum(0.11303858131900) xh =3D myNum(-0.74525997120900) yh =3D myNum(0.11304454499600) r =3D myNum(4.0) size =3D myNum(int.Parse(argv[0])) iterations =3D myNum(int.Parse(argv[1])) print ("size =3D $(size.i), iterations =3D $(iterations.i)") buf =3D array(myNum, size.i) xs =3D (xh - xl) / (size - 1) ys =3D (yh - yl) / (size - 1) start =3D date.Now ix =3D myNum(0) while ix < size: mb_d(xl, xs, yl, ys, ix, myNum(0), size, r, iterations, buf) ix +=3D 1 elapsed =3D date.Now.Subtract(start) print ("Boo elapsed time =3D $elapsed") for i in buf: System.Console.Write(i + " ") print print print myNum.n, "myNums created" main(("250", "1000"))=20

=20

=20

/* boo myNum version - Mandelbrot Benchmark by Bill Wood */ class myNum: =09public static n as int =09public i as double =09def constructor(j as int): =09=09i =3D j =09=09++n =09def constructor(y as double): =09=09i =3D y =09=09++n =09def constructor(x as myNum): =09=09i =3D x.i =09=09++n =09static def op_Multiply(x as myNum, j as int): =09=09return myNum(x.i * j) =09static def op_Multiply(x as myNum, y as myNum): =09=09return myNum(x.i * y.i) =09static def op_Division(x as myNum, y as myNum): =09=09return myNum(x.i / y.i) =09static def op_Addition(x as myNum, j as int): =09=09return myNum(x.i + j) =09static def op_Addition(x as myNum, y as myNum): =09=09return myNum(x.i + y.i) =09static def op_Subtraction(x as myNum, j as int): =09=09return myNum(x.i - j) =09static def op_Subtraction(x as myNum, y as myNum): =09=09return myNum(x.i - y.i) =09static def op_GreaterThan(x as myNum, y as myNum): =09=09return x.i > y.i =09static def op_LessThan(x as myNum, y as myNum): =09=09return x.i < y.i =09static def op_LessThanOrEqual(x as myNum, y as myNum): =09=09return x.i <=3D y.i =09def ToString(): =09=09return i.ToString() def mb_d(xl as myNum, xs as myNum, yl as myNum, ys as myNum, ix as myNum,\ =09iyl as myNum, iyh as myNum, r as myNum, iterations as myNum, buf as (myN= um)): bp =3D myNum(0) iy =3D iyl while iy < iyh: cx =3D xl + ix * xs ci =3D yl + iy * ys rx2 =3D ri2 =3D rx =3D ri =3D myNum(0) count =3D myNum(0) while ((rx2 + ri2) <=3D r) and (count < iterations): ri =3D (rx + rx) * ri + ci rx =3D rx2 - ri2 + cx rx2 =3D rx * rx ri2 =3D ri * ri count +=3D 1 if (rx2 + ri2 > r) and (count <=3D iterations): buf[bp.i] =3D count bp +=3D 1 else: buf[bp.i] =3D myNum(0) bp +=3D 1 iy +=3D 1 def main(argv as (string)): xl =3D myNum(-0.74526593488600) yl =3D myNum(0.11303858131900) xh =3D myNum(-0.74525997120900) yh =3D myNum(0.11304454499600) r =3D myNum(4.0) size =3D myNum(int.Parse(argv[0])) iterations =3D myNum(int.Parse(argv[1])) print ("size =3D $(size.i), iterations =3D $(iterations.i)") buf =3D array(myNum, size.i) xs =3D (xh - xl) / (size - 1) ys =3D (yh - yl) / (size - 1) start =3D date.Now ix =3D myNum(0) while ix < size: mb_d(xl, xs, yl, ys, ix, myNum(0), size, r, iterations, buf) ix +=3D 1 elapsed =3D date.Now.Subtract(start) print ("Boo elapsed time =3D $elapsed") for i in buf: System.Console.Write(i + " ") print print print myNum.n, "myNums created" main(("250", "1000"))=20

=20

=20

/* boo ducktyped version - Mandelbrot Benchmark by Bill Wood */ def mb_d(xl as duck, xs as duck, yl as duck, ys as duck, ix as duck, iyl as= duck, iyh as duck, r as duck, iterations as duck, buf as (duck)): bp as duck =3D 0 for iy as duck in range(iyl, iyh): cx as duck =3D xl + ix * xs ci as duck =3D yl + iy * ys # rx2 =3D ri2 =3D rx =3D ri =3D 0.0 rx2 as duck =3D 0.0; ri2 as duck =3D 0.0; rx as duck =3D 0.0; ri as= duck =3D 0.0 count as duck =3D 0 while ((rx2 + ri2) <=3D r) and (count < iterations): ri =3D (rx + rx) * ri + ci rx =3D rx2 - ri2 + cx rx2 =3D rx * rx ri2 =3D ri * ri count +=3D 1 if (rx2 + ri2 > r) and (count <=3D iterations): buf[bp] =3D count bp +=3D 1 else: buf[bp] =3D 0 bp +=3D 1 def main(argv as (string)): xl as duck =3D -0.74526593488600 yl as duck =3D 0.11303858131900 xh as duck =3D -0.74525997120900 yh as duck =3D 0.11304454499600 r as duck =3D 4.0 size as duck =3D int.Parse(argv[0]) iterations as duck =3D int.Parse(argv[1]) print ("size =3D $size, iterations =3D $iterations") buf =3D array(duck, cast(int, size)) xs as duck =3D (xh - xl) / (size - 1) ys as duck =3D (yh - yl) / (size - 1) start =3D date.Now for ix as duck in range(0, size): mb_d(xl, xs, yl, ys, ix, 0, size, r, iterations, buf) elapsed =3D date.Now.Subtract(start) print ("Boo elapsed time =3D $elapsed") for i as duck in buf: System.Console.Write(i + " ") main(("250", "1000"))=20

=20

=20

""" Python version - Mandelbrot benchmark by Bill Wood """ def mb_d(xl, xs, yl, ys, ix, iyl, iyh, r, iterations, buf): bp =3D 0 for iy in xrange(iyl, iyh): cx =3D xl + ix * xs ci =3D yl + iy * ys rx2 =3D ri2 =3D rx =3D ri =3D 0 count =3D 0 while ((rx2 + ri2) <=3D r) and (count < iterations): ri =3D (rx + rx) * ri + ci rx =3D rx2 - ri2 + cx rx2 =3D rx * rx ri2 =3D ri * ri count +=3D 1 if (rx2 + ri2 > r) and (count <=3D iterations): buf[bp] =3D count bp +=3D 1 else: buf[bp] =3D 0 bp +=3D 1 import sys, time def Main(): xl =3D -0.74526593488600 yl =3D 0.11303858131900 xh =3D -0.74525997120900 yh =3D 0.11304454499600 r =3D 4.0 size =3D 250 iterations =3D 1000 print "size =3D ", size, ", iterations =3D ", itera= tions buf =3D range(0, size) xs =3D (xh - xl) / (size - 1) ys =3D (yh - yl) / (size - 1) starttime =3D time.clock() for ix in xrange(0, size): mb_d(xl, xs, yl, ys, ix, 0, size, r, iterations, buf) print "Total time: ", time.clock() - starttime print buf Main()=20