# Difference between \`take!(IOBuffer(test))\` and \`Vector{UInt8}(test)\`

**URL:** https://discourse.julialang.org/t/difference-between-take-iobuffer-test-and-vector-uint8-test/98664
**Category:** Internals & Design
**Tags:** question
**Created:** [May 11, 2023, 7:25am UTC](https://discourse.julialang.org/t/difference-between-take-iobuffer-test-and-vector-uint8-test/98664 "2023-05-11T07:25:29Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)
#### Post date: [May 11, 2023, 7:25am UTC](https://discourse.julialang.org/t/difference-between-take-iobuffer-test-and-vector-uint8-test/98664/1 "2023-05-11T07:25:29Z")

</div>

Hi all 🙂

there are often more than one way to do one thing in programming. My question is, why is the on thing faster than the other. Should we change it?

```julia
julia> length(test)
1000000

julia> @btime take!(IOBuffer(test));
  27.200 μs (4 allocations: 976.80 KiB)

julia> @btime Vector{UInt8}(test);
  333.500 μs (2 allocations: 976.67 KiB)

julia> length(test)
1

julia> @btime take!(IOBuffer(test));
  32.998 ns (3 allocations: 192 bytes)

julia> @btime Vector{UInt8}(test);
  124.556 ns (1 allocation: 64 bytes)

```

refs:

> <https://github.com/JuliaLang/julia/blob/0b5ec1f57eb38102235568de0a5f6a8a3ae92ace/base/strings/io.jl#L308>

> <https://github.com/JuliaLang/julia/blob/0b5ec1f57eb38102235568de0a5f6a8a3ae92ace/base/strings/string.jl#L104>

tested on:

```julia
julia> versioninfo()
Julia Version 1.8.5
Commit 17cfb8e65e (2023-01-08 06:45 UTC)
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: 8 × 11th Gen Intel(R) Core(TM) i7-11800H @ 2.30GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-13.0.1 (ORCJIT, tigerlake)
  Threads: 1 on 8 virtual cores
Environment:
  JULIA_PKG_USE_CLI_GIT = true

```

Cheers

---

<div class="post-metadata">

### Author: ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)
#### Post date: [May 12, 2023, 9:06pm UTC](https://discourse.julialang.org/t/difference-between-take-iobuffer-test-and-vector-uint8-test/98664/2 "2023-05-12T21:06:36Z")

</div>

How did you define your `test` variable? I got similar timings for the two (using Julia 1.9.0):

```julia
julia> using BenchmarkTools

julia> const test = fill(UInt8(1), 1000000);

julia> @btime take!(IOBuffer(test));
  32.839 μs (3 allocations: 976.73 KiB)

julia> @btime Vector{UInt8}(test);
  31.311 μs (2 allocations: 976.67 KiB)

```
