title: Julia symbolic and numeric performance vs Python
body: <p>I just translated a set of scientific calculations involving matrices which elements are symbolic expressions which are differentiated and combined with various other mathematical expressions then numerically integrated. The pieces of code below constitute a minimal example for the sake of reproducing the performance gap I am experiencing. I understand that differentiating symbolically then integrating numerically does not make sense, but again, the point is about performance gap. It's important to note that importing libraries do not represent much time and do not explain the performance gap.</p>
  <p>Julia code:</p>
  <pre><code>using Symbolics, QuadGK
  
  @variables x
  
  m = [i * 10*x^3 + 1/i * sin(x) + 5*i*x^3 * cos(x) - 8i*x^2 + 2/sin(i*3.0)*x + exp(1/(x+10)) for i in 1:500]
  
  m_d = expand_derivatives.(Differential(x).(m))
  m_d_expr = build_function(m, x)
  m_d_f = eval(m_d_expr[1])
  v = quadgk(m_d_f, 0, 1)
  print(v[1])
  </code></pre>
  <p>Python Code:</p>
  <pre><code>import pandas as pd
  import numpy as np
  from sympy import sin, diff, pi, lambdify, integrate, cos, exp
  from sympy.abc import x
  from sympy.matrices import Matrix
  from scipy.integrate import quad
  
  def integrate_matrix(m, v, a, b):
      mi = np.zeros((m.rows, m.cols))
      for i in range(m.rows):
          for j in range(m.cols):
              f = lambdify(v, m[i, j])
              integral_value = quad(f, a, b)[0]
              mi[i, j] = integral_value
              
      return mi
  
  
  m = Matrix([i * 10*x**3 + 1/i * sin(x) + 5*i*x**3 * cos(x) - 8*i*x**2 + 2/sin(i*3.0)*x + exp(1/(x+10)) for i in range(1, 501)])
  
  v = integrate_matrix(m, x, 0, 1)
  print(v)
  </code></pre>
  <p>My question: Is there a way to improve the Julia code to match Python code performance. Each time I try to impress my piers about Julia performance, I get embarrassed. I am still a Julia noob, but I really do not see what to do.</p>
  <p>Approx timing:
  Python : 6 seconds
  Julia : 30+ seconds</p>
  <p>Julia version 1.6
  Python 3.7</p>
  <p>Note: I am posting this due to the big gap. And no, the CAS does not explain it all. Moreover, we are doing a symbolic diff not integration, not to mention sympy is known to be slow. I could add code to precisely time, then what? The original scientific code I faced the problem with was 6 seconds Python vs 75 seconds Julia. What a shame.</p>