What is the equivalent of this in nim:
from sys import getsizeof
greeting = "hello world"
print(getsizeof(greeting))
60
I tried sizeof but I think that for a string type, that gives me the size of the pointer not the underlying string:
let greeting = "Hello World"
echo(sizeof(greeting))
8That gives me the length of the string. However, I am interested in the space it takes in the memory, i.e. including it's internal length field.
From the docs: > However, strings in Nim are both zero-terminated and have a length field. One can retrieve the length with the builtin len procedure; the length never counts the terminating zero.
I came across a toy example in Python that "compresses" a sequence of DNA to a bitfield. They start by showing the space the original string takes in memory by using sys.getsizeof, so I was just trying to replicate the same in Nim.
So while I don't care about the actual internal structure of the string it'd be nice to be able to introspect the space it takes in the memory.
If that's not possible, I'll use ascii string + some loose change to approximate.
Kind of, yes.
That said, this looks very useful even beyond my immediate concern with strings. Thank you for sharing!