Performance of length and spawn

Craig Dickson <>
Fri Jan 22 15:12:59 CET 1999

A couple of questions:

Is the performance of length/1 O(1) or O(n)? (I.e. is a count of the number
of items in the list stored, or is the length determined by walking the list
every time?)

When spawning a new process, is it necessary to duplicate in memory every
argument passed to it?

The specific case is that I'm writing a server that spawns new processes to
handle most of the requests sent to it, in the hopes of maximizing the
server's availability to accept requests. If a request basically amounts to
"How long is a certain list?", then I can see a few possibilities:

(1) If length/1 is O(n) and lists don't have to be copied for spawn/3: the
list might be quite long, so perhaps it's worthwhile to spawn a process to
get the length and send it back to the caller.

(2) If length/1 is O(1): it probably is fast enough that it isn't worth the
trouble to use spawn/3 even if copying would not be necessary.

(3) If arguments have to be copied for spawn/3: since copying a list
probably takes longer than counting its elements, spawn is probably taking
more of the server's time than just calling length/1 directly.

Another question: Is there any intention to add features to Erlang's module
system, such as the ability to make certain modules private to others? In
practice, I suppose just not documenting certain modules (or documenting
them as not for public use) is enough to keep people out of them, but I'd
like for the language itself to understand that some modules are for
internal use only within a definable group of modules. I seem to recall that
Java's "package" concept supplies such a capability (then again, I haven't
done any Java in a couple of years).



More information about the erlang-questions mailing list