i don’t know why this old topic just popped up, but i was looking at SerialPort and string building perofmrance, so…
for what it’s worth:
- Strings in SuperCollider are raw character arrays. they are fast unless you are constantly reallocating and garbage colelcting them. so use
.format to build them:
// 10000 pairs of random integers formatted as strings
~values = Array.fill(10000, { [1024.rand, 1024.rand] });
{
~strings = ~values.collect({|v| "set % %".format(v[0], v[1]) });
}.bench;
// time to run: 0.042151639998337 seconds.
(i’m too lazy to benchmark the equivalent C code: exercise for reader.)
if you build strings in some other way, like adding one character at a time, it could get hella slow because you are really allocating new strings and having the GC pick up after you.
and anyways, you don’t striclty need to build strings at all; you can use pre-allocated raw bytes just like serialosc does, and send them straight to SerialPort.
but i agree with the other answers that it doesn’t seem particularly fruitful to rebuild serialosc in supercollider. IMHO it will be comparable in speed (string handling will be a little slower; OTOH it doesn’t have to go through a UDP port or whatever.) if you particularly want your SC app to be totally self-contained, and also want to learn the monome serial protocol really well, and generally want to learn how to do low-level things efficiently in SC, then it could be a fun exercise.