# Generate functions inside a function from string

**URL:** <https://discourse.julialang.org/t/generate-functions-inside-a-function-from-string/12331>\
**Category:** Performance\
**Tags:** question\
**Created:** [July 12, 2018, 1:15pm UTC](https://discourse.julialang.org/t/generate-functions-inside-a-function-from-string/12331 "2018-07-12T13:15:13Z")\
**Posts on this page:** 1\
**Showing post:** 10

<div class="post-metadata">

**Author:** ![wuhuagumu](https://avatars.discourse-cdn.com/v4/letter/w/7ab992/32.png) [@wuhuagumu](https://discourse.julialang.org/u/wuhuagumu)\
**Post date:** [July 13, 2018, 2:38am UTC](https://discourse.julialang.org/t/generate-functions-inside-a-function-from-string/12331/10 "2018-07-13T02:38:38Z")

</div>

Well, I am working with `SymPy`. I would like to build a symbolic matrix and then numerically calculate it. However I found `SymPy` cannot do the calculation fast enough, e.g.

```
using SymPy

@vars x

expr=integrate(sin(x))
f=eval(parse("x->$(repr(expr))"))
g=lambdify(expr,[x])

@time subs(expr,x=>1.0)
@time f(1.0)
@time g(1.0)

# timing:
# 0.000365 seconds (80 allocations: 2.453 KiB)
# 0.000003 seconds (5 allocations: 176 bytes)
# 0.000016 seconds (9 allocations: 240 bytes)

```

I found `eval` is the fastest. As I have to diagonalize a rather large matrix many times (e.g. ten thousand of 400\*400 matrices), I want to make the performance better.

Besides, I thought building something from string should not be a very uncommon thing…

---

_[View the full topic](https://discourse.julialang.org/t/generate-functions-inside-a-function-from-string/12331)._
