Discussion:
space comparison of strings and symbols
(too old to reply)
Nelson Alexandra
2020-05-20 18:51:32 UTC
Permalink
How much more memory does a symbol need compared to the name of
the symbol?
Kaz Kylheku
2020-05-20 20:12:09 UTC
Permalink
Post by Nelson Alexandra
How much more memory does a symbol need compared to the name of
the symbol?
That depends entirely on the implementation.

Also on some details, like is the symbol interned or not.

Interning may require an entry to be allocated in some associative
structure, which an uninterned symbol won't need.

In a Common Lisp, the smallest symol we can make is likely with
make-symbol, because the symbol is uninterned, yet the name is
externally supplied and so we can discount it.

You can use profiling tools to investigate how much consing is being
done by a given expression.

CLISP (32 bit):

1]> (time (make-symbol "foo"))
Real time: 1.1E-5 sec.
Run time: 9.0E-6 sec.
Space: 48 Bytes
#:|foo|
[2]> (time (intern "foo"))
Real time: 5.1E-5 sec.
Run time: 5.0E-5 sec.
Space: 48 Bytes
|foo| ;
NIL
[3]> (time (gensym "foo"))
Real time: 1.3E-5 sec.
Run time: 1.1E-5 sec.
Space: 112 Bytes
#:|foo3345|

TXR Lisp (non-ANSI, 32 bit):

2> (pprof (make-sym "foo"))
malloc bytes: 0
gc heap bytes: 16
total: 16
milliseconds: 0
#:foo
3> (pprof (intern "foo"))
malloc bytes: 0
gc heap bytes: 48
total: 48
milliseconds: 0
foo
4> (pprof (gensym "foo"))
malloc bytes: 664
gc heap bytes: 48
total: 712
milliseconds: 0
#:foo0009

Some of this may be misleading because the allocations are not
necessarily permanent. If creating the object involves a bit of garbage,
that garbage shouldn't count as part of the object's representation
contributing to its size.
--
TXR Programming Lanuage: http://nongnu.org/txr
Music DIY Mailing List: http://www.kylheku.com/diy
ADA MP-1 Mailing List: http://www.kylheku.com/mp1
Nelson Alexandra
2020-05-20 22:18:15 UTC
Permalink
Post by Kaz Kylheku
[...]
That depends entirely on the implementation.
Also on some details, like is the symbol interned or not.
Interning may require an entry to be allocated in some associative
structure, which an uninterned symbol won't need.
In a Common Lisp, the smallest symol we can make is likely with
make-symbol, because the symbol is uninterned, yet the name is
externally supplied and so we can discount it.
You can use profiling tools to investigate how much consing is being
done by a given expression.
1]> (time (make-symbol "foo"))
Real time: 1.1E-5 sec.
Run time: 9.0E-6 sec.
Space: 48 Bytes
#:|foo|
[2]> (time (intern "foo"))
Real time: 5.1E-5 sec.
Run time: 5.0E-5 sec.
Space: 48 Bytes
|foo| ;
NIL
[3]> (time (gensym "foo"))
Real time: 1.3E-5 sec.
Run time: 1.1E-5 sec.
Space: 112 Bytes
#:|foo3345|
2> (pprof (make-sym "foo"))
malloc bytes: 0
gc heap bytes: 16
total: 16
milliseconds: 0
#:foo
3> (pprof (intern "foo"))
malloc bytes: 0
gc heap bytes: 48
total: 48
milliseconds: 0
foo
4> (pprof (gensym "foo"))
malloc bytes: 664
gc heap bytes: 48
total: 712
milliseconds: 0
#:foo0009
Some of this may be misleading because the allocations are not
necessarily permanent. If creating the object involves a bit of
garbage, that garbage shouldn't count as part of the object's
representation contributing to its size.
Thank you!

Loading...