# Build\_function performance issues

**URL:** <https://discourse.julialang.org/t/build-function-performance-issues/116834>\
**Category:** Specific Domains\
**Tags:** symbolics\
**Created:** [July 9, 2024, 3:36pm UTC](https://discourse.julialang.org/t/build-function-performance-issues/116834 "2024-07-09T15:36:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![JosephChoi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josephchoi/32/221042_2.png) [@JosephChoi](https://discourse.julialang.org/u/JosephChoi)\
**Post date:** [July 9, 2024, 3:36pm UTC](https://discourse.julialang.org/t/build-function-performance-issues/116834/1 "2024-07-09T15:36:48Z")

</div>

When using functions generated with `build_function` with `expression = Val{false}`, I’m getting performance issues compared to manually written functions. Am I using `build_function` incorrectly or are there some additional settings I should be using?

```julia
using Symbolics

@syms x y z a b c

expr = a*x^2 + b*y + c - z

# build_function generated function with expression = Val{true}
clipboard(build_function(expr, x, y, z, a, b, c, expression = Val{true}))
# Paste function code and give function name
function build_fun_valtrue(x, y, z, a, b, c)
    (+)((+)((+)(c, (*)(-1, z)), (*)(b, y)), (*)(a, (^)(x, 2)))
end

# build_function generated function with expression = Val{false}
build_fun_valfalse = build_function(expr, x, y, z, a, b, c, expression = Val{false})

#manually written function
function manual_fun(x,y,z,a,b,c)
    return a*x^2 + b*y + c - z
end

# Running build_fun_valfalse() takes much longer and allocates memory
@time for _ in 1:1e6 build_fun_valtrue(3,2,1,1,2,3) end
@time for _ in 1:1e6 build_fun_valfalse(3,2,1,1,2,3) end
@time for _ in 1:1e6 manual_fun(3,2,1,1,2,3) end
# When running the tests again however, it will no longer allocate memory and
# take less time, but still take much longer to evaluate than the other functions

```

Using the returned function code from `build_function(expression = Val{true})` gives near identical results to using the manually written function, but using the `build_function(expression = Val{false})` functions take ~3 orders of magnitude longer to execute 1e6 times.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [July 10, 2024, 3:23am UTC](https://discourse.julialang.org/t/build-function-performance-issues/116834/2 "2024-07-10T03:23:09Z")

</div>

This is just a benchmarking artifact. Generic functions are always set as `const` by defualt. Variables in Julia are generally not const. So only one thing here is not a constant global. So this is simply an artifact of benchmarking in the global scope that would go away in any function scope. To see this, just mark the one as const:

```julia
using Symbolics

@syms x y z a b c

expr = a*x^2 + b*y + c - z

# build_function generated function with expression = Val{true}
clipboard(build_function(expr, x, y, z, a, b, c, expression = Val{true}))
# Paste function code and give function name
function build_fun_valtrue(x, y, z, a, b, c)
    (+)((+)((+)(c, (*)(-1, z)), (*)(b, y)), (*)(a, (^)(x, 2)))
end

# build_function generated function with expression = Val{false}
const build_fun_valfalse = build_function(expr, x, y, z, a, b, c, expression = Val{false})

#manually written function
function manual_fun(x,y,z,a,b,c)
    return a*x^2 + b*y + c - z
end

# Running build_fun_valfalse() takes much longer and allocates memory
@time for _ in 1:1e6 build_fun_valtrue(3,2,1,1,2,3) end
@time for _ in 1:1e6 build_fun_valfalse(3,2,1,1,2,3) end
@time for _ in 1:1e6 manual_fun(3,2,1,1,2,3) end

```

and now it goes away. Or use `@btime` with `$`.
