File : Details

# varspace()

Memory allocated to specified variables, to containers components, or to results of expressions
Details
Version
1.0
Author
Samuel Gougeon
Maintainer
Samuel Gougeon
Categories
Supported Scilab Version
5.5
Creation Date
July 13, 2015 Description
```            varspace() returns the number of bytes allocated to each variable specified by
its name or directly provided. When an heterogenous container like a list, a
cells array or a structure is provided, the vector or matrix of numbers of bytes
allocated to their components is returned.

varspace() will be useful to optimize algorithms using a lot of intermediate
memory, or simply to get aware about memory consumption w.r.t. to data types and
sizes.

Help page, examples and results follow.

--> varspace

Memory space taken by specified variables, litterals, or containers components

CALLING SEQUENCES
-----------------
varspace              // displays this help
varspace var1 var2 ...
vm = varspace("var1", "var2", ...)
Vm = varspace(VarNames)
Vm = varspace(obj#1)
VM = varspace(obj#1, obj#2, ...)

PARAMETERS
----------
var1, var2,.. :
Words = names of variables that's memory space must be
returned or displayed.
VarNames :
Vector or matrix of non-empty words = variables names
vm :   Row of decimal numbers = memory space allocated to associated
variables, in bytes.

Vm :   Vector or matrix of decimal numbers, having the size of
VarNames or obj#1.
Vm(i)   = memory allocated to the variable named VarNames(i)
or to the component obj#1(i).
Vm(i,j) = memory allocated to the variable named VarNames(i,j)
or to the component obj#1(i,j).

obj#1, obj#2,.. :
Literal objects, or variables of simple data type, or
heterogeneous containers (Lists, Cells arrays, Structures
arrays). If a component C is itself a container, varspace()
is not recursively applied to its components: Only its whole
varspace is returned at the C position.

VM :   List of vectors or matrices of decimal numbers.
VM(i) has the size of obj#i. It provides the vector or matrix
of numbers of bytes allocated to the components of obj#i:
- literal objects or expressions: the object or the result
of the expression is first assigned to a temporary variable.
The memory space taken by this one is returned.
- words or texts (scalar, vector or matrix): are considered
as objects in their own, not as names of variables to be
byte-sized.

DESCRIPTION
-----------
For a series of scilab variables, varspace() returns the respective
amounts of allocated memory, in bytes.
If a queried variable or a component of a list or stucture does
not exist, a memory amount == -1 is returned.
NOTE: Only local variables are seen. To make a global variable locally
visible, please apply global() to it before calling varspace().
NOTE: If two variables with the same name are defined in the local
and the global spaces and are queried, only the local one is
considered.

EXAMPLES
--------
clear d
a = 1, b = [1 2], c = [1 2 3], e = [], es = '', s1 = 'a', s2 = 'ab'
ec = cell(); ec22 = cell(2,2); ec44 = cell(4,4); ec88 = cell(8,8);

// Specifying names of variables
// -----------------------------
varspace ec ec22 ec44 ec88 // inline syntax (then without coma)
varspace('a,b ,c, d, e,es, s1,s2, ec22 ec88, a')
varspace(['a' 'b' 'c' ; 'd' 'ec' 'a']) // matrix of variables names

// Specifying objects
// ------------------
//  Matrix of texts considered as an object (because '' can't be the name
//   of a variable) :
varspace(['a' 'b' 'c' ; 'd' 'e' ''])

// Containers like a cells array: the memory allocated to each array's
//  component is returned:
c = makecell([2,3], %t,%pi,%z, list('hello',rand(3,5)),'',rand(1,3)<0.5)

// The memory allocated to the cell is greater than the sum of
//  memory allocated to its components:
varspace(c)
sum(varspace(c)), varspace("c")

// This is also the case for lists, that can have undefined components:
m = rand(100,100);
L = list(m, m+%i*m, , m<0.5, int8(m), int16(m), int32(m));
varspace('m', L)  // Since L is an object, 'm' is considered
//  as a simple string (another object),
//  instead of a variable's name
// To get the varspace of a litteral string, you may embed it in a list:
varspace(list("a,b ,c, d, e,es, s1,s2, ec22 ec88, a"))
// .. or priorly assign it to a variable:
s = "a,b ,c, d, e,es, s1,s2, ec22 ec88, a";
varspace s

* who : http://help.scilab.org/docs/current/en_US/who.html
* http://wiki.scilab.org/Memory%20representation%20of%20variables

RESULTS OF EXAMPLES
===================
--> clear d    // d does no longer exist
--> a = 1; b = [1 2]; c = [1 2 3]; e = []; es = ''; s1 = 'a'; s2 = 'ab';
--> ec = cell(); ec22 = cell(2,2); ec44 = cell(4,4); ec88 = cell(8,8);
-->
--> // Specifying names of variables
--> // -----------------------------
--> varspace ec ec22 ec44 ec88
ans  =
160.    240.    480.    1440.

--> varspace('a,b ,c, d, e,es, s1,s2, ec22 ec88, a')
ans  =
16.    32.    40.  - 1.    16.    32.    32.    32.    240.    1440.    16.

--> varspace(['a' 'b' 'c' ; 'd' 'ec' 'a'])
ans  =
16.    32.     40.
- 1.     160.    16.

-->// Specifying objects
-->// ------------------

--> varspace(['a' 'b' 'c' ; 'd' 'e' ''])
ans  =
64.

--> c=makecell([2,3],%t,%pi,%z,list('hello',rand(3,5)),'',rand(1,3)<0.5)
c  =
!%t        3.1415927  z           !
!                                 !
!{2 list}  ""         [%f,%t,%f]  !

--> varspace(c)
ans  =
24.     24.    56.
208.    32.    24.

--> sum(varspace(c)), varspace("c")
ans  =
368.
ans  =
552.

--> m = rand(100,100);
--> L = list(m, m+%i*m, , m<0.5, int8(m), int16(m), int32(m));
--> varspace('m', L)
ans  =
ans(1)
32.
ans(2)
80016.    160016.  - 1.    40016.    10024.    20024.    40024.
``` Files (1) Miscellaneous file
```File defining the function varspace()

* MAKE IT AVAILABLE in EVERY SESSION: Click there to see indications

* Get help: Enter "varspace" without parameters

``` News (0) Comments (0) Leave a comment
You must register and log in before leaving a comment. Email notifications
Send me email when this toolbox has changes, new files or a new release.
{"ft_lang_label":"__label__en","ft_lang_prob":0.5745086,"math_prob":0.98068196,"size":5179,"snap":"2020-45-2020-50","text_gpt3_token_len":1584,"char_repetition_ratio":0.16193236,"word_repetition_ratio":0.094797686,"special_character_ratio":0.38868508,"punctuation_ratio":0.2298951,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9797572,"pos_list":[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],"im_url_duplicate_count":[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-11-25T13:53:27Z\",\"WARC-Record-ID\":\"<urn:uuid:83ab7139-25c3-4955-8a46-9604d3a3e4bc>\",\"Content-Length\":\"18973\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:2d91c6b5-6c5b-43dc-aa30-b46345d2412a>\",\"WARC-Concurrent-To\":\"<urn:uuid:a5bf83e2-b801-42a8-90d8-ab33f2cd2398>\",\"WARC-IP-Address\":\"176.9.3.186\",\"WARC-Target-URI\":\"https://fileexchange.scilab.org/toolboxes/364000\",\"WARC-Payload-Digest\":\"sha1:7KCO57K72U5YDMECM262E6VRF5CGOYLX\",\"WARC-Block-Digest\":\"sha1:DXPLIEVUMZALKPJGDJHLV7UIG4JYVCOX\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-50/CC-MAIN-2020-50_segments_1606141182794.28_warc_CC-MAIN-20201125125427-20201125155427-00259.warc.gz\"}"}