Differencebetween 'Size' and 'Length' Methods

Difference between size and length methods?

size() is a method specified in java.util.Collection, which is then inherited by every data structure in the standard library. length is a field on any array (arrays are objects, you just don't see the class normally), and length() is a method on java.lang.String, which is just a thin wrapper on a char[] anyway.

Perhaps by design, Strings are immutable, and all of the top-level Collection subclasses are mutable. So where you see "length" you know that's constant, and where you see "size" it isn't.

What is the difference between `size` and `length` methods

Summary

In Ruby, methods can be overridden, so there are classes where there are multiple methods that lead to the same results so that behavior can be easily overridden in one method without affecting the other. Some classes do this using separate methods, while other classes implement this behavior as aliases.

Which is which, and why, is often a language implementation decision that can't be answered canonically without asking the Ruby Core team members who implemented the code. As such, that portion of the question is out of scope for Stack Overflow. Assuming that aliased methods are not expected to be monkey-patched as often as work-alike methods is a reasonable assumption, but it is only that: an assumption.

If you need a truly canonical answer, you will have to dig through the SVN source, search the bug tracker discussions, or ask the Core Team directly. However, I provide a pragmatic analysis below.

String Class: Different Methods

For example, the Ruby String#size and String#length methods are actually separate methods, but internally Ruby calls the same C source code to implement them both:

rb_str_length(VALUE str)
{
return LONG2NUM(str_strlen(str, NULL));
}

This is purely an implementation detail. From the Ruby VM's point of view, they are really separate methods that just happen to share an underlying C implementation for speed. You should be able to redefine #size or #length on a String object without changing the behavior of both, although doing so often interferes with a REPL such as Pry or IRB.

Array Class: Aliased Methods

On the other hand, some classes implement #size and #length as aliases. For example, Array#size is explicitly defined as an alias for Array#length. As a result, this creates a copy of the original method name as #size, so you should be able to redefine the aliased version without changing the behavior of the original #length method.

Parting Thoughts

This issue is really a difference of implementation, not behavior. In practice, it would appear the only meaningful distinction lies in which Ruby component implements the work-alike behavior. There may be legacy reasons, performance reasons, or it may simply be a bug that no one has cared enough about to file or fix.

Since the behavior is sane, and doesn't really violate the Principle of Least Surprise, I'd treat it as a minor language quirk. However, anyone who feels more strongly about it should definitely file a bug.

What's the difference between length and length()?

.length;

directly accesses a field member.

.length();

invokes a method (i.e. an accessor) to access a field member.

in the case of String, this is to be expected since it's immutable.

count vs length vs size in a collection

Length() tends to refer to contiguous elements - a string has a length for example.

Count() tends to refer to the number of elements in a looser collection.

Size() tends to refer to the size of the collection, often this can be different from the length in cases like vectors (or strings), there may be 10 characters in a string, but storage is reserved for 20. It also may refer to number of elements - check source/documentation.

Capacity() - used to specifically refer to allocated space in collection and not number of valid elements in it. If type has both "capacity" and "size" defined then "size" usually refers to number of actual elements.

I think the main point is down to human language and idioms, the size of a string doesn't seem very obvious, whilst the length of a set is equally confusing even though they might be used to refer to the same thing (number of elements) in a collection of data.

Is there a technical difference between the terms length and size (in programming, of course)?

Ideally, count would be the number of items, and size would be the amount of storage taken up (as in sizeof).

In practice, all three (including length, which is the most ambiguous) are muddled up in many widely-used libraries, so there's no point trying to impose a pattern on them at this stage.

Scala - What is the difference between size and length of a Seq?

Nothing. In the Seq doc, at the size method it is clearly stated: "The size of this sequence, equivalent to length.".

Difference between len and size

size comes from numpy (on which pandas is based).

It gives you the total number of elements in the array. However, you can also query the sizes of specific axes with np.size (see below).

In contrast, len gives the length of the first dimension.

For example, let's create an array with 36 elements shaped into three dimensions.

In [1]: import numpy as np                                                      

In [2]: a = np.arange(36).reshape(2, 3, -1)

In [3]: a
Out[3]:
array([[[ 0, 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10, 11],
[12, 13, 14, 15, 16, 17]],

[[18, 19, 20, 21, 22, 23],
[24, 25, 26, 27, 28, 29],
[30, 31, 32, 33, 34, 35]]])

In [4]: a.shape
Out[4]: (2, 3, 6)

size

size will give you the total number of elements.

In [5]: a.size                                                        
Out[5]: 36

len

len will give you the number of 'elements' of the first dimension.

In [6]: len(a)                                                                  
Out[6]: 2

This is because, in this case, each 'element' stands for a 2-dimensional array.

In [14]: a[0]                                                                   
Out[14]:
array([[ 0, 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10, 11],
[12, 13, 14, 15, 16, 17]])

In [15]: a[1]
Out[15]:
array([[18, 19, 20, 21, 22, 23],
[24, 25, 26, 27, 28, 29],
[30, 31, 32, 33, 34, 35]])

These arrays, in turn, have their own shape and size.

In [16]: a[0].shape                                                             
Out[16]: (3, 6)

In [17]: len(a[0])
Out[17]: 3

np.size

You can use size more specifically with np.size.

For example you can reproduce len by specifying the first ('0') dimension.

In [11]: np.size(a, 0)                                                          
Out[11]: 2

And you can also query the sizes of the other dimensions.

In [10]: np.size(a, 1)                                                          
Out[10]: 3

In [12]: np.size(a, 2)
Out[12]: 6

Basically, you reproduce the values of shape.

Difference between length() and capacity() methods in StringBuilder

StringBuilder is for building up text. Internally, it uses an array of characters to hold the text you add to it. capacity is the size of the array. length is how much of that array is currently filled by text that should be used. So with:

StringBuilder sb = new StringBuilder(1000);
sb.append("testing");

capacity() is 1000 (there's room for 1000 characters before the internal array needs to be replaced with a larger one), and length() is 7 (there are seven meaningful characters in the array).

The capacity is important because if you try to add more text to the StringBuilder than it has capacity for, it has to allocate a new, larger buffer and copy the content to it, which has memory use and performance implications*. For instance, the default capacity of a StringBuilder is currently 16 characters (it isn't documented and could change), so:

StringBuilder sb = new StringBuilder();
sb.append("Singing:");
sb.append("I am the very model of a modern Major General");

...creates a StringBuilder with a char[16], copies "Singing:" into that array, and then has to create a new array and copy the contents to it before it can add the second string, because it doesn't have enough room to add the second string.


* (whether either matters depends on what the code is doing)

std::string length() and size() member functions

As per the documentation, these are just synonyms. size() is there to be consistent with other STL containers (like vector, map, etc.) and length() is to be consistent with most peoples' intuitive notion of character strings. People usually talk about a word, sentence or paragraph's length, not its size, so length() is there to make things more readable.



Related Topics



Leave a reply



Submit