# How to alias a function to have maximum speed

**URL:** <https://discourse.julialang.org/t/how-to-alias-a-function-to-have-maximum-speed/92918>\
**Category:** New to Julia\
**Created:** [January 13, 2023, 12:42pm UTC](https://discourse.julialang.org/t/how-to-alias-a-function-to-have-maximum-speed/92918 "2023-01-13T12:42:07Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![baizhan](https://avatars.discourse-cdn.com/v4/letter/b/ecae2f/32.png) [@baizhan](https://discourse.julialang.org/u/baizhan)\
**Post date:** [January 13, 2023, 12:42pm UTC](https://discourse.julialang.org/t/how-to-alias-a-function-to-have-maximum-speed/92918/1 "2023-01-13T12:42:07Z")

</div>

I have a function, say `f(x,y,z)=x+y+z`  
In many situations, I would like to fix one of the parameters, say letting `z=10`, and give it another name.  
To do this, I can think of several different ways.

```julia
g1(x,y)=f(x,y,10)
g2=f

function rename()
  g(x,y)=f(x,y,10)
  return g
end

g3=rename()

```

Then, calling `g1(x,y)`, `g2(x,y,10)` and `g3(x,y)` should give the same result.

However, the time consuming for these three functions are very different.  
A simple test suggest that `g1` works far more faster than `g2` and `g3`.

Why is that?  
If I have to alias `f` using `g2` or `g3` way, how can I make it fast?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 13, 2023, 1:07pm UTC](https://discourse.julialang.org/t/how-to-alias-a-function-to-have-maximum-speed/92918/2 "2023-01-13T13:07:42Z")

</div>

> [@baizhan](#):
>
> However, the time consuming for these three functions are very different.  
> A simple test suggest that `g1` works far more faster than `g2` and `g3`.

[Don’t benchmark in global scope](https://docs.julialang.org/en/v1/manual/performance-tips/#Performance-critical-code-should-be-inside-a-function).

Inside a function (i.e. in local scope), they should all be basically the same in performance, and all fast, but the simplest and most idiomatic thing is generally to use an anonymous function `(x,y) -> f(x,y,10)`.

---

<div class="post-metadata">

**Author:** ![baizhan](https://avatars.discourse-cdn.com/v4/letter/b/ecae2f/32.png) [@baizhan](https://discourse.julialang.org/u/baizhan)\
**Post date:** [January 13, 2023, 1:17pm UTC](https://discourse.julialang.org/t/how-to-alias-a-function-to-have-maximum-speed/92918/3 "2023-01-13T13:17:24Z")

</div>

This is my test code

```julia
function test1()
	for i=1:1000
		g1(i,i+1)
	end
end

function test2()
	for i=1:1000
		g2(i,i+1,10)
	end
end

function test3()
	for i=1:1000
		g3(i,i+1)
	end
end

@time test1()
@time test2()
@time test3()

```

and the result is:

0.000001 seconds  
0.006006 seconds (1.75 k allocations: 28.250 KiB, 98.73% compilation time)  
0.004733 seconds (3.35 k allocations: 124.493 KiB, 97.85% compilation time)

---

<div class="post-metadata">

**Author:** ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)\
**Post date:** [January 13, 2023, 1:31pm UTC](https://discourse.julialang.org/t/how-to-alias-a-function-to-have-maximum-speed/92918/4 "2023-01-13T13:31:24Z")

</div>

The problem is that `g2` and `g3` are variables in global scope. You either need to define them in the respective functions (`test2` and `test3`), or declare them constant (`const g2=f`, `const g3=rename()`).

Also, to time your functions, consider using `@btime` from `BenchmarkTools` instead of `@time`, which includes the compilation time.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [January 13, 2023, 3:11pm UTC](https://discourse.julialang.org/t/how-to-alias-a-function-to-have-maximum-speed/92918/5 "2023-01-13T15:11:04Z")

</div>

> [@baizhan](#):
>
> ```julia
> function test1()
> for i=1:1000
> g1(i,i+1)
> end
> end
> 
> ```

Benchmarking can be difficult, and here the compiler tricks you.

```julia
julia> using BenchmarkTools

julia> @btime test1()
  1.200 ns (0 allocations: 0 bytes)

```

That’s unrealistically fast. What if we run the loop a _trillion_ times?

```julia
function test1t()
	for i=1:10^12
		g1(i,i+1)
	end
end

julia> @btime test1t()
  1.200 ns (0 allocations: 0 bytes)

```

Still 1.2ns. The compiler realizes that your test function returns nothing, and has no observable effects, and can therefore be replaced with `test1() = nothing`.

Running a lot of iterations of a loop, for benchmarking purposes, will just trick you. Leave this kind of looping to BenchmarkTools. And do make sure that your benchmarked function returns something that can be observed, otherwise the compiler will go behind your back and replace the function with nothing at all.

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [January 13, 2023, 4:17pm UTC](https://discourse.julialang.org/t/how-to-alias-a-function-to-have-maximum-speed/92918/6 "2023-01-13T16:17:20Z")

</div>

Why do you want another function name at all? You can use default arguments for that. And if you find it difficult to remember argument names/order, use optional named arguments in this case `f(; x=1, y=2, z=3)`.

```julia
julia> f(x=1, y=2, z=3) = x + y + z;

julia> f()
6

julia> f(1)
6

julia> f(1, 2)
6

julia> f(1, 2, 3)
6

```

Another option is to define multiple methods for `f`:

```julia
julia> f(x, y, z) = x + y + z
f (generic function with 1 method)

julia> f(x, y) = f(x, y, 3)
f (generic function with 2 methods)

julia> f(x) = f(x, 2, 3)
f (generic function with 3 methods)

julia> f() = f(1, 2, 3)
f (generic function with 4 methods)

julia> f()
6

julia> f(1)
6

julia> f(1,2)
6

julia> f(1,2,3)
6

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 13, 2023, 4:28pm UTC](https://discourse.julialang.org/t/how-to-alias-a-function-to-have-maximum-speed/92918/7 "2023-01-13T16:28:39Z")

</div>

> [@Seif\_Shebl](#):
>
> Why do you want another function name at all? You can use default arguments for that.

It’s quite common to want to fix certain arguments at the call site when a function is passed to some higher-order function (e.g. optimization, `map`, etcetera), rather than at the function-definition site (which you may not even control).

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [January 13, 2023, 4:41pm UTC](https://discourse.julialang.org/t/how-to-alias-a-function-to-have-maximum-speed/92918/8 "2023-01-13T16:41:42Z")

</div>

Makes sense. In this case, using an anonymous function `(x,y) -> f(x,y,10)` as you suggested would be perfect. But the OP didn’t explicitly specify their real use case.

---

<div class="post-metadata">

**Author:** ![baizhan](https://avatars.discourse-cdn.com/v4/letter/b/ecae2f/32.png) [@baizhan](https://discourse.julialang.org/u/baizhan)\
**Post date:** [January 14, 2023, 1:33am UTC](https://discourse.julialang.org/t/how-to-alias-a-function-to-have-maximum-speed/92918/9 "2023-01-14T01:33:07Z")

</div>

Thank you for your advice!

After adding the `const` keyword, `g2` and `g3` do get as fast as `g1`.
