Would concatenation be better...?
#[10 11 12 13],#[21 22 23 24 ]
Question
I want to join two instance of ByteArray in Squeak, Cuis or Pharo Smalltalk
b := #[10 11 12 13] asOrderedCollection.
b addAll: #[21 22 23 24].
b asByteArray
Are there more efficient ways to do this?
La solution
Would concatenation be better...?
#[10 11 12 13],#[21 22 23 24 ]
Autres conseils
Yes. Using an OrderedCollection will involve several unnecessary object allocations and redundant copying. You should create a new byte array and copy the contents of your source arrays into it:
a := #[10 11 12 13].
b := #[21 22 23 24].
c := ByteArray new: (a size + b size).
c replaceFrom: 1 to: a size with: a startingAt: 1.
c replaceFrom: a size + 1 to: c size with: b startingAt: 1.
This only allocates the new ByteArray and does the copying with primitives, so it's quite fast. It'll work in Squeak, Cuis and Pharo, and very likely other Smalltalks as well.