# Huge difference between passing a type, or using a hardcoded-type (in benchmarks)?

**URL:** <https://discourse.julialang.org/t/huge-difference-between-passing-a-type-or-using-a-hardcoded-type-in-benchmarks/38561>\
**Category:** Performance\
**Created:** [May 1, 2020, 3:04pm UTC](https://discourse.julialang.org/t/huge-difference-between-passing-a-type-or-using-a-hardcoded-type-in-benchmarks/38561 "2020-05-01T15:04:35Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [May 1, 2020, 3:11pm UTC](https://discourse.julialang.org/t/huge-difference-between-passing-a-type-or-using-a-hardcoded-type-in-benchmarks/38561/2 "2020-05-01T15:11:42Z")

</div>

Explicitly forcing specialization on `T` fixes the issue for me:

```julia
julia> function foo(io, ::Type{T}) where {T}
           out = Vector{T}()
           sizehint!(out, 100)
           for i in 1:100
               push!(out, read(io, T))
           end
           out
       end
foo (generic function with 2 methods)

```

And, by the way, you want to set `evals = 1` in your benchmark code to ensure that the function is only run once per `setup` call:

```julia
julia> @btime foo(x) setup=(x = IOBuffer(rand(UInt8, 1000))) evals=1;
  485.000 ns (2 allocations: 528 bytes)

julia> @btime foo(x, T) setup=(x = IOBuffer(rand(UInt8, 1000)); T=Int32) evals=1;
  486.000 ns (2 allocations: 528 bytes)

```

Otherwise the function will be evaluated many times per `setup` call, which is why you’re hitting EOF (ref: [BenchmarkTools setup isn't run between each iteration? - #6 by rdeits](https://discourse.julialang.org/t/benchmarktools-setup-isnt-run-between-each-iteration/36258/6)) .

---

_[View the full topic](https://discourse.julialang.org/t/huge-difference-between-passing-a-type-or-using-a-hardcoded-type-in-benchmarks/38561)._
