Advertisement
wagner-cipriano

Memory size of the matrix object

Jan 20th, 2019
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.25 KB | None | 0 0
  1. """
  2. Calculate memory size to object matrix in python
  3. Wagner Cipriano
  4. """
  5. from sys import getsizeof
  6. import pandas as pd
  7. import numpy as np
  8.  
  9. #modules, python 3 x python 2
  10. if ('xrange' in dir(__builtins__)):
  11.    xrange = range
  12. randn = np.random.randn
  13. randint = np.random.randint
  14.  
  15. def humanbytes(obj=None, numB=None):
  16.    """Return the lenght of the object in the memory, in bytes as a human friendly KB, MB, GB, or TB string"""
  17.    if(obj is not None):
  18.       B = getsizeof(obj)
  19.    elif(numB):
  20.       B = numB
  21.    else:
  22.       raise Exception('Err param humanbytes')
  23.    #return B #@TESTE
  24.    KB = float(1024)
  25.    MB = float(KB ** 2) # 1,048,576
  26.    GB = float(KB ** 3) # 1,073,741,824
  27.    TB = float(KB ** 4) # 1,099,511,627,776
  28.  
  29.    if B < KB:
  30.       return '{0} {1}'.format(B,'Bytes' if 0 == B > 1 else 'Byte')
  31.    elif KB <= B < MB:
  32.       return '{0:.2f} KB'.format(B/KB)
  33.    elif MB <= B < GB:
  34.       return '{0:.2f} MB'.format(B/MB)
  35.    elif GB <= B < TB:
  36.       return '{0:.2f} GB'.format(B/GB)
  37.    elif TB <= B:
  38.       return '{0:.2f} TB'.format(B/TB)
  39.  
  40. #vars
  41. n = 1000000000
  42. mult = 10
  43. dim = 1
  44.  
  45. #INT >>> test with Matrix from randint
  46. print '#INT ' * 5
  47. while dim <= n:
  48.    try:
  49.       Matrix = randint(0, 2, dim)
  50.    except MemoryError as Err:
  51.       print '>>> MemoryError Exception in dim %s' %(dim)
  52.       break;
  53.    leng = humanbytes(Matrix)
  54.    del Matrix
  55.    print('int  Matrix {0}x{0} - {1}'.format(dim, leng))
  56.    dim = dim * mult
  57.  
  58. #FLOAT >>> test with Matrix from randn
  59. print '\n', '#FLOAT ' * 5
  60. dim = 1
  61. while dim <= n:
  62.    try:
  63.       Matrix = randn(dim, dim)
  64.    except MemoryError as Err:
  65.       print '>>> MemoryError Exception in dim %s' %(dim)
  66.       break;
  67.    leng = humanbytes(Matrix)
  68.    del Matrix
  69.    print('float Matrix {0}x{0} - {1}'.format(dim, leng))
  70.    dim = dim * mult
  71.  
  72.  
  73. #INT CALC
  74. print '\n', '#INT Calc  ' * 5
  75. lg = 104
  76. i = 0
  77. dim = 10
  78. while dim <= n:
  79.    lg = lg + 72 * 10**i
  80.    leng = humanbytes(numB=lg)
  81.    print('int  Matrix {0}x{0} - {1}'.format(dim, leng))
  82.    dim = dim * mult
  83.    i += 1
  84.  
  85. #FLOAT CALC
  86. print '\n', '#FLOAT Calc  ' * 5
  87. lg = 120
  88. i = 0
  89. dim = 10
  90. while dim <= n:
  91.    lg = lg + 792 * 10**i
  92.    leng = humanbytes(numB=lg)
  93.    print('int  Matrix {0}x{0} - {1}'.format(dim, leng))
  94.    dim = dim * mult
  95.    i += 2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement