[erlang-questions] Efficiency of splitting binaries

Bjorn Gustavsson bjorn@REDACTED
Wed Oct 24 16:06:33 CEST 2007


Richard Andrews <bbmaj7@REDACTED> writes:

> 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.
> 
>     << ChunkData:30/binary-unit:8, RestData/binary >> = Data

unit:8 is default for binaries, so you don't have to specify it.

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

None if both the size and unit are known at compile-time.

If the size is variable, the size and unit values will be multiplied at run-time.
The run-time system may optimize common sizes such as 1 and 8.

I recommend that you don't change the unit size for binaries, but use the
default 8.

/Bjorn
-- 
Björn Gustavsson, Erlang/OTP, Ericsson AB



More information about the erlang-questions mailing list