# Readbytes! is bugging me

**URL:** <https://discourse.julialang.org/t/readbytes-is-bugging-me/23396>\
**Category:** New to Julia\
**Tags:** binaryio, data\
**Created:** [April 22, 2019, 10:03am UTC](https://discourse.julialang.org/t/readbytes-is-bugging-me/23396 "2019-04-22T10:03:23Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [April 22, 2019, 2:17pm UTC](https://discourse.julialang.org/t/readbytes-is-bugging-me/23396/7 "2019-04-22T14:17:55Z")

</div>

I’ve done your suggestion 1 and can see some marginal improvement, so will keep it, but in this case your 2nd suggestion is slowing things down dramatically. I’ve made it as such:

```julia
#Preallocate an array depending on datatype and of chosen size
    arrayVal::Array{UInt8,1} = Vector{UInt8}(undef, size[1]*4)
    #readbytes!(fd, arrayVal,sizeof(arrayVal))
    read!(fd,arrayVal)
    #Close the open file
    close(fd)
    data = reinterpret(Int32,arrayVal)
    data .= ntoh.(data)
    return data

```

And now the results are:

```julia
@benchmark k = readVtkArray("PartAll",Idp)
BenchmarkTools.Trial:
  memory estimate: 477.90 MiB
  allocs estimate: 28706
  --------------
  minimum time: 534.921 ms (0.00% GC)
  median time: 556.051 ms (0.00% GC)
  mean time: 719.914 ms (22.26% GC)
  maximum time: 2.025 s (71.23% GC)
  --------------
  samples: 9
  evals/sample: 1

```

Which is a major decrease (if I’ve done it correctly). I am trying to make a minimal working example available in my other post ([How fast is binary reading capabilities in Julia compared with other languages? - #5 by Ahmed\_Salih](https://discourse.julialang.org/t/how-fast-is-binary-reading-capabilities-in-julia-compared-with-other-languages/23401/5)), I will try to post it as soon as possible.

Kind regards

---

_[View the full topic](https://discourse.julialang.org/t/readbytes-is-bugging-me/23396)._
