简体   繁体   中英

Understanding object.size() versus nchar()

Consider the following vectors x and y

x <- "a"
y <- deparse(x)

From ?nchar

nchar takes a character vector as an argument and returns a vector whose elements contain the sizes of the corresponding elements of x.

And from ?object.size

The calculation is of the size of the object, and excludes the space needed to store its name in the symbol table.

With these two definitions, shouldn't the object size of y in memory be larger than that of x ?

> nchar(x) == nchar(y)
[1] FALSE
> object.size(x) == object.size(y)
[1] TRUE

Machine info:

> version[[1]]
# [1] "x86_64-pc-linux-gnu"

Not necessarily. You typically don't set aside the exact amount of memory you need for a string since that it makes it difficult to skip around and find the one you need in memory, so allocations will happen in chunks. Observe

rv<-1:100
os<-sapply(rv, function(x) object.size(paste0(rep("a",x), collapse="")))
plot(rv,os, xlab="string length", ylab="object size")

在此处输入图片说明

Here we create strings "a","aa","aaa","aaaa", etc and see the object size. Note the size of the object does not increase with each additional character, but when you pass a threshold, you get a larger chunk assigned to you.

So object size doesn't tell you how many bytes the data is actually taking up, it just tells you how many bytes have been set aside. There are not really the same thing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM