Some of Sunday's edits have been lost. The edits from Saturday that were reverted have been restored. Site is now hosted on prgmr.com. Thank you for your patience. This notice will be removed one week from posting. --Michael Mol 18:12, 7 March 2010 (UTC)

Variable size/Get

From Rosetta Code

Jump to: navigation, search
Variable size/Get is a programming task. Visitors like you are encouraged to solve it according to the task description, using any language they may happen to know.
Add to BlogMarksAdd to del.icio.usAdd to diggAdd to NewsvineAdd to redditAdd to Slashdot
Demonstrate how to get the size of a variable.

Contents

[edit] Ada

Ada represents the size of a variable in bits, not bytes like many other languages.

Int_Bits : constant Integer := Integer'size;
Whole_Bytes : constant Integer := Int_Bits / Storage_Unit; -- Storage_Unit is the number of bits per storage element

[edit] AutoHotkey

VarSetCapacity(Var, 10240000)  ; allocate 10 megabytes
MsgBox % size := VarSetCapacity(Var) ; 10240000

[edit] C#

 
class Program
{
static void Main(string[] args)
{
int i = sizeof(int);
Console.WriteLine(i);
Console.ReadLine();
}
}
 

[edit] C

printf("An int contains %d bytes.\n", sizeof(int));

[edit] C++

Store the size of an int in bytes:

#include <cstdlib>
std::size_t intsize = sizeof(int);

Note: sizeof can be used without the header <cstdlib>; the latter is only needed for the type std::size_t, which is an alias for whatever type is used to store sizes for the given compiler.

Output the number of bits of an int:

#include <climits>
#include <cstdlib>
std::size_t intbits = CHAR_BITS*sizeof(int);

Note: the type char is always 1 byte (which, however, need not be 8 bits).

Get the size of a variable in bytes:

#include <cstdlib>
int a = 1;
std::size_t a_size = sizeof a;

Note: Parentheses are needed around types, but not around variables.

Get the size of an expression's type:

#include <cstdlib>
std::size_t size = sizeof (3*6 + 7.5);

[edit] Common Lisp

As with some of the other dynamic languages, we're not concerned with variable size, but rather the size of a value that a variable references. There's not a standard interface for this, but implementations may provide this functionality.

Works with: LispWorks

(let ((a (cons 1 2))
(b (make-array 10))
(c "a string"))
(list (hcl:find-object-size a)
(hcl:find-object-size b)
(hcl:find-object-size c)))

returns

(12 48 24)

However, note that interesting objects are generally composed of several levels of references, often including references to preexisting objects, so what the size should be considered as is often hard to define after the fact. A robust though non-automatic way to determine the “true” memory utilization of some data is to do something like this:

(let (items)
(gc) ; name varies by implementation
(room)
(dotimes (x 512)
(push (allocate-something-of-interest) items))
(gc)
(room))

room prints information about current memory usage, but in an implementation-defined format. Take the difference of the relevant numbers, divide by 512, and you have the amount of memory consumed by allocating one additional instance of whatever it is.

[edit] D

Every type and variable in D has a property sizeof, which give the size of the type in bytes. eg.

int i ;
writefln(i.sizeof) ; // print 4
int[13] ints1 ; // static integer array of length 13
writefln(ints1.sizeof) ; // print 52
int[] ints2 = new int[13] ; // dynamic integer array, variable length, currently 13
writefln(ints2.sizeof) ; // print 8, all dynamic array has this size
writefln(ints2.length) ; // print 13, length is the number of allocated element in aggregated type

[edit] Delphi

i := sizeof([any variable or structure]);

[edit] Fortran

Works with: Fortran version 90 and later The intrinsic functions bit_size and digits can be used to find the size of an integer. Bit_size returns the number of bits in an integer while digits returns the number of significant digits in the integer. Because of the use of signed intergers this will be one less than the bit size. Digits can be used on real variables where it returns the number of significant figures in the mantissa.

INTEGER, PARAMETER ::  i8 = SELECTED_INT_KIND(2)
INTEGER, PARAMETER :: i16 = SELECTED_INT_KIND(4)
INTEGER, PARAMETER :: i32 = SELECTED_INT_KIND(8)
INTEGER, PARAMETER :: i64 = SELECTED_INT_KIND(16)
INTEGER(i8) :: onebyte = 0
INTEGER(i16) :: twobytes = 0
INTEGER(i32) :: fourbytes = 0
INTEGER(i64) :: eightbytes = 0
 
WRITE (*,*) BIT_SIZE(onebyte), DIGITS(onebyte) ! prints 8 and 7
WRITE (*,*) BIT_SIZE(twobytes), DIGITS(twobytes) ! prints 16 and 15
WRITE (*,*) BIT_SIZE(fourbytes), DIGITS(fourbytes) ! prints 32 and 31
WRITE (*,*) BIT_SIZE(eightbytes), DIGITS(eightbytes) ! prints 64 and 63
WRITE (*,*) DIGITS(0.0), DIGITS(0d0) ! prints 24 and 53

[edit] Haskell

only works with types that instance Storable:

import Foreign
 
sizeOf (undefined :: Int) -- size of Int in bytes (4 on mine)
sizeOf (undefined :: Double) -- size of Double in bytes (8 on mine)
sizeOf (undefined :: Bool) -- size of Bool in bytes (4 on mine)
sizeOf (undefined :: Ptr a) -- size of Ptr in bytes (4 on mine)

[edit] IDL

IDL is array based, so its size() function is geared towards that:

arr = intarr(3,4)
print,size(arr)
;=> prints this:
2 3 4 2 12

The result means: 2 dimensions in the array, the first dimension has extent 3, the second has extent 4, the elements of the array are 2-byte integers (IDL's default for an "int"), there's a total of 12 elements in the array.

[edit] J

In J, the function 7!:5 is analogous to sizeof in C. For example:

some_variable =: 42
7!:5<'some_variable'

An advantage of 7!:5 is that it can be used on any name, including functions, operators, etc (i.e. it's not just restricted to variables):

some_function =: +/ % #
7!:5<'some_function'

[edit] Modula-3

BITSIZE and BYTESIZE are built in functions.

MODULE Size EXPORTS Main;
 
FROM IO IMPORT Put;
FROM Fmt IMPORT Int;
 
BEGIN
Put("Integer in bits: " & Int(BITSIZE(INTEGER)) & "\n");
Put("Integer in bytes: " & Int(BYTESIZE(INTEGER)) & "\n");
END Size.

Output:

Integer in bits: 32
Integer in bytes: 4

[edit] OCaml

(** The result is the size given in word.
The word size in octet can be found with (Sys.word_size / 8).
(The size of all the datas in OCaml is at least one word, even chars and bools.)
*)

let sizeof v =
let rec rec_size d r =
if List.memq r d then (1, d) else
if not(Obj.is_block r) then (1, r::d) else
if (Obj.tag r) = (Obj.double_tag) then (2, r::d) else
if (Obj.tag r) = (Obj.string_tag) then (Obj.size r, r::d) else
if (Obj.tag r) = (Obj.object_tag) ||
(Obj.tag r) = (Obj.closure_tag)
then invalid_arg "please only provide datas"
else
let len = Obj.size r in
let rec aux d sum i =
if i >= len then (sum, r::d) else
let this = Obj.field r i in
let this_size, d = rec_size d this in
aux d (sum + this_size) (i+1)
in
aux d (1) 0
in
fst(rec_size [] (Obj.repr v))
;;

testing in the toplevel:

# sizeof 234 ;;
- : int = 1
 
# sizeof 23.4 ;;
- : int = 2
 
# sizeof (1,2);;
- : int = 3
 
# sizeof (2, 3.4) ;;
- : int = 4
 
# sizeof (1,2,3,4,5) ;;
- : int = 6
 
# sizeof [| 1;2;3;4;5 |] ;;
- : int = 6
 
# sizeof [1;2;3;4;5] ;;
- : int = 11
 
(* because a list is equivalent to *)
 
# sizeof (1,(2,(3,(4,(5,0))))) ;;
- : int = 11
 
# type foo = A | B of int | C of int * int ;;
type foo = A | B of int | C of int * int
 
# sizeof A ;;
- : int = 1
 
# sizeof (B 3) ;;
- : int = 2
 
# sizeof (C(1,2)) ;;
- : int = 3
 
# sizeof true ;;
- : int = 1
 
# sizeof 'A' ;;
- : int = 1
 
# sizeof `some_pvar ;;
- : int = 1
 
# sizeof "" ;;
- : int = 1
 
# sizeof "Hello!" ;;
- : int = 2
(* remember the size is given in words
(so 4 octets on 32 bits machines) *)

 
# for i=0 to 16 do
Printf.printf "%d -> %d\n" i (sizeof(String.create i))
done;;
0 -> 1
1 -> 1
2 -> 1
3 -> 1
4 -> 2
5 -> 2
6 -> 2
7 -> 2
8 -> 3
9 -> 3
10 -> 3
11 -> 3
12 -> 4
13 -> 4
14 -> 4
15 -> 4
16 -> 5
- : unit = ()
 
# sizeof(Array.create 10 0) ;;
- : int = 11
 
# sizeof(Array.create 10 (String.create 20)) ;;
- : int = 16
 
# sizeof(Array.init 10 (fun _ -> String.create 20)) ;;
- : int = 61

[edit] Perl

Works with: Perl version 5.x Library: Devel::Size

use Devel::Size;
 
my $var = 9384752;
my @arr = (1, 2, 3, 4, 5, 6);
print size($var);
print total_size(@arr);

[edit] PL/I

 
put skip list (SIZE(x)); /* gives the number of bytes occupied by X */
/* whatever data type or structure it is. */
 
put skip list (CURRENTSIZE(x));
/* gives the current number of bytes of X */
/* actually used by such things as a */
/* varying-length string, including its */
/* length field. */
 

[edit] PureBasic

Define a
Debug SizeOf(a)
; This also works for structured variables

[edit] Python

This information is only easily available for the array type:

>>> from array import array
>>> argslist = [('l', []), ('c', 'hello world'), ('u', u'hello \u2641'),
('l', [1, 2, 3, 4, 5]), ('d', [1.0, 2.0, 3.14])]
>>> for typecode, initializer in argslist:
a = array(typecode, initializer)
print a, '\tSize =', a.buffer_info()[1] * a.itemsize
del a
 
 
array('l') Size = 0
array('c', 'hello world') Size = 11
array('u', u'hello \u2641') Size = 14
array('l', [1, 2, 3, 4, 5]) Size = 20
array('d', [1.0, 2.0, 3.1400000000000001]) Size = 24
>>>

Also: Works with: Python version 2.6+

import sys
sys.getsizeof(obj)

[edit] Pop11

From abstract point of view Pop11 variables are bindings between identifiers and values. In concrete terms Pop11 variables store references to values in the heap. Each reference takes one machine word (4 bytes on 32-bit machines and 8 bytes on 64-bit machines). Pop11 identifiers take 3 machine words, but are only needed for "permanent" variables (lexical variables do not need identifiers after compilation). Additionally variable names (words) need space (4 machine for word + space for string corresponding to the word). The bottom line is: variable needs one machine word plus some overhead due to introspection.

Form user point of view more important is space taken by values (size of values referenced by a single variable typically varies during program execution). The datasize function gives amount (in machine words) of space directly used by given value:

;;; Prints 0 because small integers need no heap storage
datasize(12) =>
;;; Prints 3: 3 character fits into single machine word, 1 word
;;; for tag, 1 for length
datasize('str') =>
;;; 3 element vector takes 5 words: 3 for values, 1 for tag, 1 for
;;; length
datasize({1 2 3}) =>
;;; Prints 3 because only first node counts
datasize([1 2 3]) =>

Note that large amount of data my be referenced from given value, but this data is potentially shared, so there is no canonical way to assign it to a single value or variable.

[edit] R

object.size gives an estimate of the amount of memory used to store the variable, in (kilo/Mega/Giga) bytes. See also dim, length and nchar for determining the extent of mulitdimensional variables (such as matrices, lists, etc).

# Results are system dependent
num <- c(1, 3, 6, 10)
object.size(num) # e.g. 56 bytes
 
#Allocating vectors using ':' results in less memory being (reportedly) used
num2 <- 1:4
object.size(num2) # e.g. 40 bytes
 
#Memory shared by objects isn't always counted
l <- list(a=c(1, 3, 6, 10), b=1:4)
object.size(l) # e.g. 280 bytes
 
l2 <- list(num, num2)
object.size(l2) # e.g. 128 bytes

[edit] Tcl

Since all variables are ultimately strings in Tcl, this is easy:

string bytelength $var

There is additional overhead per value and per variable, which depends on the architecture that Tcl was built for and the version of Tcl. In 8.5 on a ILP-32 architecture, local variables have an overhead of 8 bytes (without traces) and values have a minimum overhead of 24 bytes (this minimum is achieved for integers that fit in a signed 64-bit integer type or a double-precision float).

[edit] Toka

There are two primary data types in Toka, cells and characters. The size of these can be obtained easily:

char-size .
cell-size .

If you load the floating point support, you can also obtain the size of a floating point number:

needs floats
float-size .

All sizes are returned in bytes.

[edit] Ursala

The virtual machine represents all code and data as binary trees of cells. The number of cells required for any object can be computed by the built in weight function (or by an equivalent user-defined function), which takes an argument of any type and returns a natural number. Host memory usage for any given object is worst case linear in the weight, but may be considerably less due to sharing (i.e., copying something by copying only a reference to it, which is done automatically and invisibly to the programmer).

An additional facility exists for arbitrary precision floating point numbers, which are based on the mpfr library. The library function mpfr..prec applies to a number in mpfr format and returns the number of bits of precision in the mantissa. Host memory usage is linear plus a small constant.

#import std
 
#cast %nL
 
examples = <weight 'hello',mpfr..prec 1.0E+0>

output:

<40,160>
Personal tools
Google AdSense