简体   繁体   中英

How to determine the size of a set variable in Python?

I have code as below where table data is captured in set(cur) . Is there any way to find the space occupied by this variable in linux/unix? (Memory or buffer space)

cur.execute("select A , B , C from DeptTable")
dept_entries = set(cur)

cur.execute("select A , B , C from EmployeeTable where EmplName in ('A','B')") 
for empl in cur:
  if empl in dept_entries:
    print(empl, 'Yes')
  else:
    print(empl, 'No')

I would recommend using sys.getsizeof :

>>> import sys
>>> sys.getsizeof(dept_entries)
12345
>>> sys.getsizeof(set([1,2,3]))
224
>>> sys.getsizeof(set([1,2,3,4,5]))
736

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM