Re: Mathematica 20x slower than Java at arithmetic/special functions, is

• To: mathgroup at smc.vnet.net
• Subject: [mg115889] Re: Mathematica 20x slower than Java at arithmetic/special functions, is
• From: Oliver Ruebenkoenig <ruebenko at wolfram.com>
• Date: Mon, 24 Jan 2011 05:23:38 -0500 (EST)

```On Sun, 23 Jan 2011, Leo Alekseyev wrote:

> I was playing around with JLink the other day, and noticed that Java
> seems to outperform Mathematica by ~20x  in an area where I'd expect
> Mathematica to be rather well optimized -- arithmetic involving special
> functions.  In my particular example, I am simply evaluating a sum of
> Bessel functions.  I understand that much depends on the underlying
> implementation, but I just want to run this by Mathgroup to see if
> this is to be expected, or maybe if I'm doing something suboptimal in
> Mathematica.  Here's the code that I'm running:
>
> grid1dc[x_,
>  y_] = (With[{d = 0.1, NN = 50},
>    Sum[Re[N[
>        d BesselJ[1, 2 Pi d Sqrt[m^2 + n^2]]/
>          Sqrt[m^2 + n^2 + 10^-7]] Exp[
>        I 2.0 Pi (m x + n y)]], {m, -NN, NN, 1}, {n, -NN, NN, 1}]]) //
>   N
>
> and
>
> gridres1da =
> With[{delta = 0.5, xlim = 2.5, ylim = 2.5},
>  Table[{x, y, grid1dc[x, y]}, {x, -xlim, xlim, delta}, {y, -ylim,
>    ylim, delta}]]
>
>
> Java implementation uses Colt and Apache common math libraries for the
> Bessels and complex numbers, uses a double for loop, and consistently
> runs 15-20 times faster.
>
> --Leo
>
>

Leo,

here is a speedup of about 100 and if you have multiple CPUs then there is

tmp = (With[{d = 0.1, NN = 50},
Sum[Re[N[
d BesselJ[1, 2 Pi d Sqrt[m^2 + n^2]]/
Sqrt[m^2 + n^2 + 10^-7]] Exp[
I 2.0 Pi (m x + n y)]], {m, -NN, NN, 1}, {n, -NN, NN,
1}]]) // N // Chop;

(* since tmp is a large expr, compilation to C takes a long time and does
not pay *)
cfGrid1D =
With[{code = tmp},
Compile[{{x, _Real, 0}, {y, _Real, 0}}, code,
RuntimeAttributes -> Listable, Parallelization -> True]];

gridData =
Transpose[
Flatten[With[{delta = 0.5, xlim = 2.5, ylim = 2.5},
Developer`ToPackedArray@
Table[{x, y}, {x, -xlim, xlim, delta}, {y, -ylim, ylim,
delta}]], 1]];

AbsoluteTiming[re = cfGrid1D @@ gridData;]

Norm[ Flatten[gridres1da, 1][[All, -1]] - re]
1.88052*10^-13

Hope this helps,

Oliver

```

• Prev by Date: Re: Wavelet and histogram
• Next by Date: Re: Mathematica 20x slower than Java at arithmetic/special functions, is
• Previous by thread: Re: Mathematica 20x slower than Java at arithmetic/special functions, is
• Next by thread: Re: Mathematica 20x slower than Java at arithmetic/special functions, is
{"ft_lang_label":"__label__en","ft_lang_prob":0.8076068,"math_prob":0.9800716,"size":2237,"snap":"2020-24-2020-29","text_gpt3_token_len":773,"char_repetition_ratio":0.083296016,"word_repetition_ratio":0.16873449,"special_character_ratio":0.38310236,"punctuation_ratio":0.216,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99927,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14],"im_url_duplicate_count":[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-06-01T09:16:16Z\",\"WARC-Record-ID\":\"<urn:uuid:0471685a-2c6c-440b-8ed9-66ae38e2b4cf>\",\"Content-Length\":\"45851\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:a706285a-3162-48d3-992a-38a91fdd85d9>\",\"WARC-Concurrent-To\":\"<urn:uuid:1b5681f0-acce-4426-92a1-ce132efd63fc>\",\"WARC-IP-Address\":\"140.177.205.73\",\"WARC-Target-URI\":\"http://forums.wolfram.com/mathgroup/archive/2011/Jan/msg00768.html\",\"WARC-Payload-Digest\":\"sha1:X6B2V3JQYDDTO3LYO2UKNFMFHD6XGDGC\",\"WARC-Block-Digest\":\"sha1:EBTAFPP735I7H6B5W32V4UOHP64ILLEW\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-24/CC-MAIN-2020-24_segments_1590347415315.43_warc_CC-MAIN-20200601071242-20200601101242-00536.warc.gz\"}"}