# Function with initialized data

**URL:** https://discourse.julialang.org/t/function-with-initialized-data/3908
**Category:** New to Julia
**Created:** [May 24, 2017, 12:13pm UTC](https://discourse.julialang.org/t/function-with-initialized-data/3908 "2017-05-24T12:13:21Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Andreas](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@Andreas](https://discourse.julialang.org/u/Andreas)
#### Post date: [May 24, 2017, 12:13pm UTC](https://discourse.julialang.org/t/function-with-initialized-data/3908/1 "2017-05-24T12:13:21Z")

</div>

Hi all,

I have a function using data which has to be initialized at run-time. Here is a basic example:

```julia
type Test{T<:Number}
       x::T
       a::Array{T, 1}
end

function f(t::Test, x)
       i = floor(Integer, x - t.x)
       return x*t.a[i]
end

(t::Test)(x) = f(t, x) #functor definition

t = Test(3., rand(100)); #initialize data
```

if I map this function on an array, I get quite different performance whether I use the function f or the functor-call:

```julia
#init
rsi	= rand(10)*100 + 4;
@timed map(x -> f(t, x), rsi);
map(t, rsi);			

#test	
rs	= rand(10^7)*100 + 4;	
@time map(x -> f(t, x), rs);  
#1.349930 seconds (20.03 M allocations: 382.873 MB, 26.17% gc time)

@time map(t, rs);
# 0.290798 seconds (7 allocations: 76.294 MB, 0.83% gc time)
```

The difference is even bigger when I use vectorized functions:

```julia
#init
ft(x) = f(t, x)
ft.(rsi);
t.(rsi);
#test
@time ft.(rs);
#2.698942 seconds (30.00 M allocations: 534.059 MB, 8.62% gc time)
@time t.(rs);
#0.305188 seconds (7 allocations: 76.294 MB, 1.57% gc time)
```

So far so good, if I want to map the function I simply have to use the functor. However, what am I going to do if I have additional functions which need the same initialized data? I could of course define a type for each function, but that does not seem very elegant.

Any suggestions? Am I missing something?

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [May 24, 2017, 12:21pm UTC](https://discourse.julialang.org/t/function-with-initialized-data/3908/2 "2017-05-24T12:21:08Z")

</div>

Don’t benchmark in global scope: put the variable definitions and the benchmarking code inside a function.

---

<div class="post-metadata">

### Author: ![Andreas](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@Andreas](https://discourse.julialang.org/u/Andreas)
#### Post date: [May 24, 2017, 1:02pm UTC](https://discourse.julialang.org/t/function-with-initialized-data/3908/3 "2017-05-24T13:02:06Z")

</div>

As simple as that 🙂

Thanks a lot!

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [May 24, 2017, 1:04pm UTC](https://discourse.julialang.org/t/function-with-initialized-data/3908/4 "2017-05-24T13:04:34Z")

</div>

Alternatively (and preferably), use the BenchmarkTooks.jl package.
