MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Compiled function changes somehow.

  • To: mathgroup at smc.vnet.net
  • Subject: [mg78568] Compiled function changes somehow.
  • From: Nacho <ncc1701zzz at gmail.com>
  • Date: Wed, 4 Jul 2007 05:40:30 -0400 (EDT)

Hello.

I have been playing with Compile to accelerate some simple code and I
have found some differences that I cannot explain.

The standard/compiled code is as follows:

standard[max_] :=
  Table[n/m, {n, 1, max}, {m, 1, max}] // N // Flatten;

compiled =
  Compile[{{max, _Integer}},
   Table[n/m, {n, 1, max}, {m, 1, max}] // N // Flatten];

So I can do the same calculations with both codes:

In[19]:= standardresult = standard[1000]; // Timing

Out[19]= {2.969, Null}

In[20]:= compiledresult = compiled[1000]; // Timing

Out[20]= {0.422, Null}


The second is much faster, as expected. But are the results the same?
Apparently, yes:

In[21]:= standardresult == compiledresult

Out[21]= True

In[22]:= standardresult === compiledresult

Out[22]= True


But when I use Union in the lists to count the number of different
elements, I have this surprise:

Length[Union[standardresult]]
Length[Union[compiledresult]]

Out[23]= 608383
Out[24]= 634815

So they are not exactly the same...  I think that the correct answer
comes from the uncompiled version. It is the same if I remove the //N
so it can be compared symbolically.

Is this behaviour expected? Am I missing something?

Both seem to be Machine Precision, but obviously there are some little
differences. This happens with V5.2 and V6.0.

Any hint?

Thank you.



  • Prev by Date: Re: new behavior of Plot
  • Next by Date: Re: Working with factors of triangular numbers.
  • Previous by thread: Re: new behavior of Plot
  • Next by thread: Re: Compiled function changes somehow.