Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Calculate memory size to object matrix in python
- Wagner Cipriano
- """
- from sys import getsizeof
- import pandas as pd
- import numpy as np
- #modules, python 3 x python 2
- if ('xrange' in dir(__builtins__)):
- xrange = range
- randn = np.random.randn
- randint = np.random.randint
- def humanbytes(obj=None, numB=None):
- """Return the lenght of the object in the memory, in bytes as a human friendly KB, MB, GB, or TB string"""
- if(obj is not None):
- B = getsizeof(obj)
- elif(numB):
- B = numB
- else:
- raise Exception('Err param humanbytes')
- #return B #@TESTE
- KB = float(1024)
- MB = float(KB ** 2) # 1,048,576
- GB = float(KB ** 3) # 1,073,741,824
- TB = float(KB ** 4) # 1,099,511,627,776
- if B < KB:
- return '{0} {1}'.format(B,'Bytes' if 0 == B > 1 else 'Byte')
- elif KB <= B < MB:
- return '{0:.2f} KB'.format(B/KB)
- elif MB <= B < GB:
- return '{0:.2f} MB'.format(B/MB)
- elif GB <= B < TB:
- return '{0:.2f} GB'.format(B/GB)
- elif TB <= B:
- return '{0:.2f} TB'.format(B/TB)
- #vars
- n = 1000000000
- mult = 10
- dim = 1
- #INT >>> test with Matrix from randint
- print '#INT ' * 5
- while dim <= n:
- try:
- Matrix = randint(0, 2, dim)
- except MemoryError as Err:
- print '>>> MemoryError Exception in dim %s' %(dim)
- break;
- leng = humanbytes(Matrix)
- del Matrix
- print('int Matrix {0}x{0} - {1}'.format(dim, leng))
- dim = dim * mult
- #FLOAT >>> test with Matrix from randn
- print '\n', '#FLOAT ' * 5
- dim = 1
- while dim <= n:
- try:
- Matrix = randn(dim, dim)
- except MemoryError as Err:
- print '>>> MemoryError Exception in dim %s' %(dim)
- break;
- leng = humanbytes(Matrix)
- del Matrix
- print('float Matrix {0}x{0} - {1}'.format(dim, leng))
- dim = dim * mult
- #INT CALC
- print '\n', '#INT Calc ' * 5
- lg = 104
- i = 0
- dim = 10
- while dim <= n:
- lg = lg + 72 * 10**i
- leng = humanbytes(numB=lg)
- print('int Matrix {0}x{0} - {1}'.format(dim, leng))
- dim = dim * mult
- i += 1
- #FLOAT CALC
- print '\n', '#FLOAT Calc ' * 5
- lg = 120
- i = 0
- dim = 10
- while dim <= n:
- lg = lg + 792 * 10**i
- leng = humanbytes(numB=lg)
- print('int Matrix {0}x{0} - {1}'.format(dim, leng))
- dim = dim * mult
- i += 2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement