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

Hi all :slight_smile:

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> 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:

tested on:

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

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

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)