# How costly is it to store content in a box register?

I wonder how costly it is in terms of compilations speed and also other terms like memory etc., to store some content in a box and use this box then afterwards only once (or maybe twice). Is the difference meaningless or significant if used often in a document?

{\sbox\mybox{<some content>}% or \setbox\mybox\hbox{..}
<calculated something depended on the box dimension>
\box\mybox
% or:
% \usebox\mybox
}


Also: how big is the difference between using \box and \usebox (which requires to copy the box)? In some scenarios I can't be sure that the box isn't required again and can't use \box in general.

I'm coding several macros similar to \raisebox which store their content into a box and allow the user to access the dimensions using the macros \height, \width and others. During the normal use these macros might often be cascaded and each of it would simply re-save the saved box of the inner macro (\sbox\mybox{\sbox\mybox{..} .. \usebox\mybox} .. \usebox\mybox). So I wonder if I should program these macros in a way so that they can reuse the box register of the inner macro in such cases or if this would be overkill?

-
Saying \copy\x or \box\x costs the same, the former is less efficient for memory because it doesn't free the register, which is cleared anyway when the group ends. One has to pay attention to a peculiarity of box registers: \setbox0=\hbox{x}{\box0} results in an empty box register 0, because \box clears the contents of the most recent instance of the register, which in this case happens to live at an upper level. –  egreg Jul 10 '11 at 20:19
@egreg: Thanks! That's a very important point I wanted to investigate anyway. –  Martin Scharrer Jul 10 '11 at 20:32
@egreg: Ok, I looked at this a little and figured that a \setbox0=\copy0 copies the existing box 0 to a local assigned box 0, so \box0 clears the local assignment while the one in the parent scope is unchanged. This is nice if you want to \copy the box but e.g. with adjusted height and depth. –  Martin Scharrer Jul 10 '11 at 20:39

Saying \copy\x or \box\x costs the same, the former is less efficient for memory because it doesn't free the register, which is cleared anyway when the group ends.

One has to pay attention to a peculiarity of box registers: \setbox0=\hbox{x}{\box0} results in an empty box register 0, because \box clears the contents of the most recent instance of the register, which in this case happens to live at an upper level.

As Martin remarks,

\begingroup
\setbox0=\copy0
<code>
\endgroup


creates a local copy of \box0 and inside the group one can do many things to \box0 without worrying about the box register at the upper level.

If I compile a file consisting of 10000 copies of a box built simply with \hbox I get

real 0m0.524s
user 0m0.458s
sys 0m0.054s

If I say instead \setbox0=\hbox{...}\box0, I get

real 0m0.529s
user 0m0.460s
sys 0m0.054s

Not a big difference.

-
Thanks for the answer. What would be the overhead of using \setbox to store the content and then use \box to typeset it versus typesetting it directly? –  Martin Scharrer Jul 16 '11 at 20:34
I repeated this now with 1.000.000 copies but using \sbox with and without xcolor to get the overhead of the color stack into account. Results (real): hbox 1m36s, sbox 1m46s, sbox with xcolor 1m49s, averaged over 3 runs each on a ramdisk to avoid HD caching etc. impacts. The file size is indentical in the first two cases (12480175 byte) but 2.5% larger with the color support (12798580 byte). Conclusions: There is an impact, especially with colors, but for normal documents it should be negligent. I accept the answer now. Thanks for the update. –  Martin Scharrer Jul 17 '11 at 12:03