# Readbytes! or reading into an existing data block

**URL:** https://discourse.julialang.org/t/readbytes-or-reading-into-an-existing-data-block/5505
**Category:** General Usage
**Created:** [August 22, 2017, 3:03pm UTC](https://discourse.julialang.org/t/readbytes-or-reading-into-an-existing-data-block/5505 "2017-08-22T15:03:00Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![lwabeke](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lwabeke/32/4005_2.png) [@lwabeke](https://discourse.julialang.org/u/lwabeke)
#### Post date: [August 22, 2017, 3:03pm UTC](https://discourse.julialang.org/t/readbytes-or-reading-into-an-existing-data-block/5505/1 "2017-08-22T15:03:00Z")

</div>

This must have been asked before, but I couldn’t find it.

How do I use readbytes! to read into a preallocated matrix? Or is there a better function?

Currently I’m using this:

```julia
    data = Array{Complex{Int16}}(10, 1000, 3, 1 ) # Allocate correct size, done once

    dataBytes = reshape(data, prod(size(data)) )
    dataBytes = reinterpret(UInt8, dataBytes) # dataBytes is now an alias of data reinterpreted as byte array

# do this in a processing loop many times
    bytesRead = readbytes!(fid, dataBytes, sizeof(dataBytes) )
 

```

but isn’t there a more direct way to use data, instead of creating dataBytes as an alias to the data?

---

<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: [August 22, 2017, 4:53pm UTC](https://discourse.julialang.org/t/readbytes-or-reading-into-an-existing-data-block/5505/2 "2017-08-22T16:53:57Z")

</div>

> [@lwabeke](#):
>
> isn’t there a more direct way to use data, instead of creating dataBytes as an alias to the data?

Not that I know of (short of re-implementing `readbytes!` via low-level `ccall`s). What’s wrong with using `reinterpret`?

(Note that reading bytes directly into `Int16` values in this way is endian-dependent, i.e. the binary files may not be portable to other architectures, although in practice right now almost everyone uses little-endian machines.)

---

<div class="post-metadata">

### Author: ![lwabeke](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lwabeke/32/4005_2.png) [@lwabeke](https://discourse.julialang.org/u/lwabeke)
#### Post date: [August 23, 2017, 7:57am UTC](https://discourse.julialang.org/t/readbytes-or-reading-into-an-existing-data-block/5505/3 "2017-08-23T07:57:44Z")

</div>

In principle nothing is wrong with using reinterpret, it just is annoying that I need to reshape and reinterpret as separate steps.

Thus far on the problems I am mostly working with, trying to get high performance out of Julia in that particular domain, I have developed the hypothesis: that the key to performance is I need to be very careful of creating temporary variables. By now I’m a bit Obsessive Compulsive about any temporary variables no matter how big. At times my Julia coding is starting to feel like a fight to keep the GC away: Create temporary variables and reuse the the whole time (unless the infrequent occurrence of them changing size). Here I’m torn between wrapping functions in a let block to make the temporaries static or passing them in and cluttering the calling interface.

Originally the function performing the readbytes! was taking in the Complex{Int16} array (user interface) and then creating the Vector{UInt8} internally, this meant such a temporary variable every time this function is called. I have now settled with letting the user do the conversion outside once and have my function take it in as a Vector{UInt8}.

I keep feeling I want to be able to manually allocate a block of memory and then be able to at various times instantiate different types of arrays to utilise that as their data storage block. I’m not sure how one who do it, but I guess something like that would be possible in order to be able to interface to C function calls.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [August 23, 2017, 8:33am UTC](https://discourse.julialang.org/t/readbytes-or-reading-into-an-existing-data-block/5505/4 "2017-08-23T08:33:53Z")

</div>

> [@lwabeke](#):
>
> the key to performance is I need to be very careful of creating temporary variables

Among other things. No need to form hypotheses, see the [performance tips](https://docs.julialang.org/en/latest/manual/performance-tips/#man-performance-tips-1). Preallocating outputs is there, but other things are equally important. Profiling and benchmarking will give you specific information for particular cases.

---

<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: [August 23, 2017, 2:55pm UTC](https://discourse.julialang.org/t/readbytes-or-reading-into-an-existing-data-block/5505/5 "2017-08-23T14:55:39Z")

</div>

> [@lwabeke](#):
>
> By now I’m a bit Obsessive Compulsive about any temporary variables no matter how big.

In general, this is a mistake. For operations on a large (length n \>\> 1) array, then any O(1) costs (e.g. creating a few small heap-allocated temporaries like `reinterpret` wrappers) will often be negligible. This is especially true if you are doing O(n) I/O as with `readbytes!`.

Of course, ultimately you have to do profiling and benchmarking to be certain of where your performance is going, but as a general rule I wouldn’t worry about small allocations outside of innermost loops.

Hoare’s [famous quote about premature optimization and “small efficiencies”](http://ubiquity.acm.org/article.cfm?id=1513451) comes to mind.
