[erlang-questions] Efficiency of splitting binaries

Per Gustafsson per.gustafsson@REDACTED
Wed Oct 24 15:55:21 CEST 2007


Richard Andrews wrote:
> I'm looking for advice on efficiently splitting a binary into smaller chunks.
> 
> I currently use iolist_size() to check the size of the input binary then
> recursively break off a chunk from the front with something like, eg.
> 

Why are you using iolist_size()? Is it possiblethat your input is not a 
binary?


>     << ChunkData:30/binary-unit:8, RestData/binary >> = Data
> 

This should be efficient you are creating sub-binaries for both 
ChunkData and RestData so the cost should be independent of their 
respective sizes.

> ... then use RestData as input to the next cycle.
> 
> Is this sensible? Are there more efficient approaches?
> What effect does the binary-unit specifier have on performance? eg.
> 
>     ChunkData:1/binary-unit:240
> versus
>     ChunkData:240/binary
> 
> 

As long as Size*Unit remains the same this should not affect the runtime 
at all, but note that ChunkData:240/binary really means 
ChunkData:240/binary-unit:8

Per Gustafsson



More information about the erlang-questions mailing list