14

I am using a mutable Buffer and need to find out how many elements it has.

Both size and length methods are defined, inherited from separate traits.

Is there any actual performance difference, or can they be considered exact synonyms?

1
  • 2
    I personally am a semantic nit-picker, so I only use length for things that are linear or sequential and size otherwise. Feb 6, 2013 at 17:44

4 Answers 4

16

They are synonyms, mostly a result of Java's decision of having size for collections and length for Array and String. One will always be defined in terms of the other, and you can easily see which is which by looking at the source code, the link for which is provided on scaladoc. Just find the defining trait, open the source code, and search for def size or def length.

1
  • Thanks! I didn't know the source code was accessible online through the scaladoc. This is quite nice!
    – Eduardo
    Feb 6, 2013 at 17:11
5

In this case, they can be considered synonyms. You may want to watch out with some other cases such as Array - whilst length and size will always return the same result, in versions prior to Scala 2.10 there may be a boxing overhead for calling size (which is provided by a Scala wrapper around the Array), whereas length is provided by the underlying Java Array.

In Scala 2.10, this overhead has been removed by use of a value class providing the size method, so you should feel free to use whichever method you like.

1
  • I just profiled Scala 2.11 code, and calling size on an array creates an object. You should still avoid it in performance-critical code.
    – mattg
    Jan 21, 2016 at 20:03
4

As of Scala-2.11, these methods may have different performance. For example, consider this code:

val bigArray = Array.fill(1000000)(0)
val beginTime = System.nanoTime()
var i = 0
while (i < 2000000000) {
  i += 1
  bigArray.length
}
val endTime = System.nanoTime()
println(endTime - beginTime)
sys.exit(-1)

Running this on my amd64 computer gives about 2423834 nanos time (varies from time to time).

Now, if I change the length method to size, it will become about 70764719 nanos time.

This is more than 20x slower.

Why does it happen? I didn't dig it through, I don't know. But there are scenarios where length and size perform drastically different.

3

They are synonyms, as the scaladoc for Buffer.size states:

The size of this buffer, equivalent to length.

The scaladoc for Buffer.length is explicit too:

The length of the buffer. Note: xs.length and xs.size yield the same result.

Simple advice: refer to the scaladoc before asking a question.

UPDATE: Just saw your edit adding mention of performance. As Daniel C. Sobral aid, one is normally always implemented in term of the other, so they have the same performance.

1
  • I was actually wondering if there was any performance difference? For example, one stored as an actual attribute in the data structure, versus the other one having to loop through all values (like with a List) to return the same value.
    – Eduardo
    Feb 6, 2013 at 16:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.