Building an equation I've developed a function in the Julia language, using the SymEngine library, which builds an equation. This function works quite quickly, returning a lambdified equation.

The problem is, calculation of the value of this equation in the point takes too long time! And as the equation becomes longer, the calculation time also increases.

Is there a way to optimize the equation, generated by the function?

function func()

  @vars x y z myu

  longitude=atan(y/x)+pi*sign(y)*(1-sign(x))/2
  r=(x^2+y^2+z^2)^(1/2)

  dUx=0
  dUy=0
  dUz=0
  for i=2:degree
      for j=0:degree

          index=1+j; for ll=2:i-1 index+=ll+1; end

          P_i=(myu^2-1)^i
          for k=1:i+j P_i=diff(P_i,myu) end

          if(i&gt;20) F=factorial(Int128(i)); else F=factorial(i); end
          P_ij=(((1-myu^2)^(j/2))/(F*2^i))*P_i

          if(P_ij!=0)
            CS_exp=CS[index,3]*cos(j*longitude)+CS[index,4]*sin(j*longitude)
            CS_diff_x= j*CS[index,4]*cos(j*longitude)*(-y/(x^2+y^2)) - j*CS[index,3]*sin(j*longitude)*(-y/(x^2+y^2))
            CS_diff_y= j*CS[index,4]*cos(j*longitude)*( x/(x^2+y^2)) - j*CS[index,3]*sin(j*longitude)*( x/(x^2+y^2))
            CS_diff_z= 0

            L=P_ij(z/r)

            dUx+= GMe*(diff((L*(Req/r)^i)/r,x)*CS_exp + CS_diff_x*(L*(Req/r)^i)/r)
            dUy+= GMe*(diff((L*(Req/r)^i)/r,y)*CS_exp + CS_diff_y*(L*(Req/r)^i)/r)
            dUz+= GMe*(diff((L*(Req/r)^i)/r,z)*CS_exp + CS_diff_z*(L*(Req/r)^i)/r)

          end
       end
  end

  return lambdify(dUx, [x,y,z], cse=true),lambdify(dUy, [x,y,z], cse=true),lambdify(dUz, [x,y,z], cse=true)

end

dU=func();

dy= [
       dU[1](y[1],y[2],y[3]),
       dU[2](y[1],y[2],y[3]),
       dU[3](y[1],y[2],y[3])
    ]


The calculation of dy takes too long. Any ideas?
I've also commented on the julia slack, but just if anyone else is reading, as I see you've not updated the question according to my comments. 

You can see Julia's general performance tips here. I suggest you read this. You are falling into at least two of the performance gotchas: 


You are initializing dUx, dUy and dUz as Ints first, but change their type to Float64s in the first assignment. That trips the compiler. The solution is to initialize them to 0.
You are using degree and CS that appear to be captured global variables, which makes it impossible for the compiler to specialize. The solution is to pass them as arguments to the function.


Not that the second issue also means that your code is not runnable, because we don't have access to degree and CS. If your code is not runnable it's very hard to comment on. 

Here's what you can do yourself to fix this:


Use the Traceur.jl package to check your code, using @trace func(). That will check your code for the common performance gotchas.
Use the BenchmarkTools package (with @btime) to evaluate any improvements.
Once you've done that, use the Profile module (or even better, Juno's inbuilt profiler to see which lines in your code are slow.


Once you've done that, if you still have problems, please comment here with the results from the profiler, and update your code to be runnable, and we'll be happy to take a look again.