# Understanding the performance costs of generating functions on the fly

**URL:** <https://discourse.julialang.org/t/understanding-the-performance-costs-of-generating-functions-on-the-fly/47990>\
**Category:** Performance\
**Created:** [October 8, 2020, 2:58pm UTC](https://discourse.julialang.org/t/understanding-the-performance-costs-of-generating-functions-on-the-fly/47990 "2020-10-08T14:58:43Z")\
**Posts on this page:** 1\
**Showing post:** 10

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [October 8, 2020, 6:20pm UTC](https://discourse.julialang.org/t/understanding-the-performance-costs-of-generating-functions-on-the-fly/47990/10 "2020-10-08T18:20:06Z")

</div>

I just ran a similar benchmark, and it confirms the logic.

```julia
julia> df = DataFrame(a = rand(1_000_000), b = rand(1_000_000));

julia> a = df.a; b = df.b;

julia> function comp(a, b)
       ma = mean(a)
       mb = mean(b)
       
       c = cor(a, b)
       
       z = (a .- mb ./ std(a))
       
       return b .+ mb .- z .* c
       end;

julia> function make_fun_2(df)
       pn = propertynames(df)
       if :a in pn && :b in pn 
           [:a, :b] => function(a, b)       
              ma = mean(a)
              mb = mean(b)
              
              c = cor(a, b)
              
              z = (a .- mb ./ std(a))
              
              return b .+ mb .- z .* c => :c
              end => :c
       else
           [:a, :b] => ((a, b) -> fill("hello", length(a))) => :c
       end
       end;

julia> @btime comp(a, b);
  5.794 ms (4 allocations: 15.26 MiB)

julia> @btime transform!(df, [:a, :b] => comp => :c);
  6.067 ms (88 allocations: 15.26 MiB) # within a margin of error from the above

julia> @btime transform!(df, make_fun_2(df));
  8.109 ms (97 allocations: 30.52 MiB)

```

---

_[View the full topic](https://discourse.julialang.org/t/understanding-the-performance-costs-of-generating-functions-on-the-fly/47990)._
