[Shootout-list] Another benchmark

Sebastien Loisel Sebastien Loisel <sloisel@gmail.com>
Fri, 25 Mar 2005 08:19:35 -0800


On Fri, 25 Mar 2005 07:18:40 -0800 (PST), Isaac Gouy <igouy2@yahoo.com> wro=
te:
> > I'm not certain (I haven't tried yet) but it may also be
> > challenging to write versions in C, ocaml, python, lisp and so on,
> > which are similarly concise.
>=20
> It's maybe 160 lines without comments.
> Hmmm how big would it be in Java 1.4?

Hard to say, I'm no java expert. Using object-oriented instead of
templates, you can probably implement this stuff as follows. The basic
idea is that there's this "ad" class, which can be implemented using
doubles, floats, long doubles, or complex numbers (and of course, if
you have multiple types of complex numbers, then you get multiple ad
versions.) This is done in C++ with templates, some thinking would be
needed to say the equivalent things in Java. In this 160 lines of
code, the single ad implementation is used for floats, doubles, long
doubles, complex<float>s, complex<double>s and complex<long double>s.
If you do it the brute-force way, you'll have 6 implementations of ad.

Given those six implementations of ad, you'll need to implement the
newton root-finder so that it either works for all six types
(polymorphic) or you'll need to implement 6 newton root-finders.
Similarly, there's a trapezoid rule integrator, which is polymorphic
over the 6 types.

In many benchmarks, when people write these things they hard-code the
integrand into the integrator and optimize everything away. To defeat
this, the benchmark integrates three different functions. If you want
to hard-code them into the integrator, you'll need to have three
different integrators. So if you decide to optimize it that way you'll
end up with a gigantic program, which will show up on the benchmark
results as being a shitload of code.

In pure C, the more reasonable implementation would use unions and
lots of switches to implement the polymorphism. Assuming the C++
compilers are good, this will make the pure C implementation either
slower than C++ or it'll again have a zillion lines of code.

Cheers,

S=E9bastien Loisel