Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
size of an int
#1
i have a big int. i want to know how many bytes is needed to store it. that is,what is the minimum value i need to give in argument 1 of its .to_bytes() method. my first thought was to do this by "brute force". that is, increasing powers of 256 until the first number larger.

is there a better way?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
This will do the trick:

import sys

x = 9999999999999999

sys.getsizeof(x)
Reply
#3
that is not giving me expected values. for example an int with a value anywhere from 0 to 255 should have a size of 1. from 256 to 65535 should have a size of 2. it appears that sys.getsizeof is returning memory usage information, which is bot what i want.

256**n-1 should always give n and is the highest value to do so. 256**n should always give n+1.

i have found a different method. i can repeat an integer divide (// in Python3) on the number until it reaches 0. the count is the answer. but divide is slower than multiply so i think this is actually worse. another possibility is to change the multiply search to make increasingly faster steps by squaring (doubles the power) the when this exceeds the number in question, do a binary search of powers of 256 to narrow down the correct answer. IMHO, although Python is intended for tasks where ultimate speed of run time is not a requirement, use of best [known] algorithms is always a good thing for any application in any language. choice of application is part of the design step, although i have, for many projects, looped back to design.

i guess i misled by saying "store it". i meant to store just the numeric value itself in a sequence of bytes (not how much memory some engine uses). this would be the bytes you get from the .to_bytes() method. the 1st argument is how many bytes you want which must be at least as many needed to store the numeric value (it raises OverflowError if the chosen amount is too small)

perhaps shift operations can do this better.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
sys.getsize should return the size of the complete object, not just only the consumed memory of the value inside the integer object.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
so i have to figure it out on my own.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  size of set vs size of dict zweb 0 2,141 Oct-11-2019, 01:32 AM
Last Post: zweb
  CSV file created is huge in size. How to reduce the size? pramoddsrb 0 10,478 Apr-26-2018, 12:38 AM
Last Post: pramoddsrb

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020