So far, I have mixed feelings about the new compiler in Mathematica 12. Sometimes my results are a little bit faster, sometimes they are considerable slower than those obtained with the old compiler with the option CompilationTarget->"C", and anyway it is much more typing. Now I met a simple example that I cannot get compiled.

With the old compiler in Mathematica the following works excellent:

fuc=Compile[{{lst, _Real, 1}}, Module[{s=0.},
   Do[s=s+z, {z, lst}]; s], CompilationTarget->"C"]

It returns a compiled function that is many hundreds times faster than the uncompiled function.

With the new compiler, I expected the following to work:

fun=FunctionCompile @  
  Function[Typed[lst,TypeSpecifier["PackedArray"]["Real64", 1]],
     Module[{s=0.}, Do[s=s+z, {z, lst}]; s]]

The result is a failure with a (for me) not very helpful message: Function Native`CreateIterator: no satisfiable candidate of the type {Integer64, PackedArray[Real64, 1:Integer64], Integer64}->Handle[MIteratorWork[v13092, v13092, v13092, Integer64]] was found in the alternative list <<324>>, and tag TypeError.

I tried to improve my attempt by indicating the type of s and trying to indicate the type of z, without success.

I cannot imagine that the new compiler cannot compile the list form of Do. Any hint on what am I doing wrong and how should it be done is highly appreciated.

  • 2
    $\begingroup$ Looks like a I guess it is an error in the internal type coercion. This is not overly surprising as this technology is pretty new. Best course of action would be to tell Wolfram Support about it. For the moment, fun = FunctionCompile@Function[ Typed[lst, TypeSpecifier["PackedArray"]["Real64", 1]], Module[{s = 0.}, Do[s += lst[[i]], {i, 1, Length[lst]}]; s ] ] will do... $\endgroup$ Commented May 5, 2019 at 15:49
  • $\begingroup$ Still, fuc will beat fun by over an order of magnitude and Total will be another order of magnitude faster. Either I did not get how to use it or FunctionCompile is really no reason to be excited. $\endgroup$ Commented May 5, 2019 at 15:54
  • 1
    $\begingroup$ my experience regarding speed is the same. Check out, mathematica.stackexchange.com/questions/195435/… $\endgroup$
    – Asim
    Commented May 5, 2019 at 18:27

1 Answer 1


The CreateIterator error suggests that only integer iterators are available. (I do not know whether they are the only ones or not; nor do I know whether to consider this a bug. But it is worth reporting and see what WRI says.)

fun = FunctionCompile@
  Function[Typed[lst, TypeSpecifier["PackedArray"]["Real64", 1]], 
   Module[{s = 0.}, Do[s = s + lst[[z]], {z, Length@lst}]; s]]

[I just noticed @Henrik posted the same thing in a comment while I was typing this up.]

  • 4
    $\begingroup$ Following your and @Henrik 's suggestion I reported both my observation that the new compiler cannot compile the list form of Do, and the speed difference between fuc and fun to WRI. They filed reports about both points, and remarked that FunctionCompile at the moment is experimental, so it might undergo substantial expansions and improvements in future versions of Mathematica. The old Compile[] functionality will remain in the Wolfram Language for the foreseeable future. $\endgroup$ Commented May 7, 2019 at 7:37
  • 1
    $\begingroup$ Thank you, Fred, for reporting! $\endgroup$ Commented May 17, 2019 at 7:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.