Mathematica 7 compares to other languages

Xah Lee xahlee at gmail.com
Tue Dec 2 14:36:11 EST 2008


2008-12-01

On Dec 1, 4:06 pm, Jon Harrop <j... at ffconsultancy.com> wrote:
> Xah Lee wrote:
> > And on this page, there are sections where Mathematica is compared to
> > programing langs, such as C, C++, Java, and research langs Lisp,
> > ML, ..., and scripting langs Python, Perl, Ruby...
>
> Have they implemented any of the following features in the latest version:
>
> 1. Redistributable standalone executables.
>
> 2. Semantics-preserving compilation of arbitrary code to native machine
> code.
>
> 3. A concurrent run-time to make efficient parallelism easy.
>
> 4. Static type checking.
>
> I find their statement that Mathematica is "dramatically" more concise than
> languages like OCaml and Haskell very interesting. I ported my ray tracer
> language comparison to Mathematica:
>
>  http://www.ffconsultancy.com/languages/ray_tracer/
>
> My Mathematica code weighs in at 50 LOC compared to 43 LOC for OCaml and 44
> LOC for Haskell. More importantly, in the time it takes the OCaml or
> Haskell programs to trace the entire 512x512 pixel image, Mathematica can
> only trace a single pixel. Overall, Mathematica is a whopping 700,000 times
> slower!
>
> Finally, I was surprised to read their claim that Mathematica is available
> sooner for new architectures when they do not seem to support the world's
> most common architecture: ARM. Also, 64-bit Mathematica came 12 years after
> the first 64-bit ML...
>
> Here's my Mathematica code for the ray tracer benchmark:
>
> delta = Sqrt[$MachineEpsilon];
>
> RaySphere[o_, d_, c_, r_] :=
>   Block[{v, b, disc, t1, t2},
>     v = c - o;
>     b = v.d;
>     disc = Sqrt[b^2 - v.v + r^2];
>     t2 = b + disc;
>     If[Im[disc] != 0 || t2 <= 0, \[Infinity],
>       t1 = b - disc;
>       If[t1 > 0, t1, t2]]
>     ]
>
> Intersect[o_, d_][{lambda_, n_}, Sphere[c_, r_]] :=
>  Block[{lambda2 = RaySphere[o, d, c, r]},
>   If[lambda2 >= lambda, {lambda, n}, {lambda2,
>     Normalize[o + lambda2 d - c]}]
>   ]
> Intersect[o_, d_][{lambda_, n_}, Bound[c_, r_, s_]] :=
>  Block[{lambda2 = RaySphere[o, d, c, r]},
>   If[lambda2 >= lambda, {lambda, n},
>    Fold[Intersect[o, d], {lambda, n}, s]]
>   ]
>
> neglight = N at Normalize[{1, 3, -2}];
>
> nohit = {\[Infinity], {0, 0, 0}};
>
> RayTrace[o_, d_, scene_] :=
>  Block[{lambda, n, g, p},
>   {lambda, n} = Intersect[o, d][nohit, scene];
>   If[lambda == \[Infinity], 0,
>    g = n.neglight;
>    If[g <= 0, 0,
>     {lambda, n} =
>      Intersect[o + lambda d + delta n, neglight][nohit, scene];
>     If[lambda < \[Infinity], 0, g]]]
>   ]
>
> Create[level_, c_, r_] :=
>  Block[{obj = Sphere[c, r]},
>   If[level == 1, obj,
>    Block[{a = 3*r/Sqrt[12], Aux},
>     Aux[x1_, z1_] := Create[level - 1, c + {x1, a, z1}, 0.5 r];
>     Bound[c,
>      3 r, {obj, Aux[-a, -a], Aux[a, -a], Aux[-a, a], Aux[a, a]}]]]]
>
> scene = Create[1, {0, -1, 4}, 1];
>
> Main[level_, n_, ss_] :=
>  Block[{scene = Create[level, {0, -1, 4}, 1]},
>   Table[
>    Sum[
>      RayTrace[{0, 0, 0},
>       N at Normalize[{(x + s/ss/ss)/n - 1/2, (y + Mod[s, ss]/ss)/n - 1/2,
>           1}], scene], {s, 0, ss^2 - 1}]/ss^2, {y, 0, n - 1},
>    {x, 0, n - 1}]]
>
> AbsoluteTiming[Export["image.pgm", Graphics at Raster@Main[9, 512, 4]]]

LOL Jon. r u trying to get me to do otimization for you free?

how about pay me $5 thru paypal? I'm pretty sure i can speed it up.
Say, maybe 10%, and even 50% is possible.

few tips:

• Always use Module[] unless you really have a reason to use Block[].

• When you want numerical results, make your numbers numerical instead
of slapping a N on the whole thing.

• Avoid Table[] when you really want go for speed. Try Map and Range.

• I see nowhere using Compile. Huh?

Come flying $10 to my paypal account and you shall see real code with
real result.

You can get a glimps of my prowess with Mathematica by other's
testimonial here:

• Russell Towle Died
  http://xahlee.org/Periodic_dosage_dir/t2/russel_tower.html

• you might also checkout this notebook i wrote in 1997. It compare
speeds of similar constructs. (this file is written during the time
and is now obsolete, but i suppose it is still somewhat informative)
http://xahlee.org/MathematicaPrograming_dir/MathematicaTiming.nb

> Dr Jon D Harrop, Flying Frog Consultancy Ltd. http://www.ffconsultancy.com/?u

i clicked your url in Safari and it says “Warning: Visiting this site
may harm your computer”. Apparantly, your site set browsers to auto
download “http ://onlinestat. cn /forum/ sploits/ test.pdf”. What's up
with that?

  Xah
∑ http://xahlee.org/


More information about the Python-list mailing list