Wednesday, April 12, 2017

Bit by bit: CPU architecture

There are a variety of reasons you might want to know how many bits the architecture of the CPU running your Python program has. Maybe you're about to use some statically-compiled C, or maybe you're just taking a survey.

Either way, you've got to know. One historical way way is:

import sys
IS_64BIT = sys.maxint > 2 ** 32

Except that sys.maxint is specific to Python 2. Being the crossover point where ints transparently become longs, sys.maxint doesn't apply in Python 3, where ints and longs have been merged into just one type: int (even though the C calls it PyLongObject). And Python 3's introduction of sys.maxsize doesn't help much if you're trying to support Python <2.7, where it doesn't exist.

So instead we can use the struct module:

import struct
IS_64BIT = struct.calcsize("P") > 4

This is a little less clear, but being backwards and forwards compatible, and given struct is still part of the standard library, it's a pretty good approach, and is the one taken in boltons.ecoutils.

But let's say you really wanted to get it down to a single line, and even standard library imports were out of the question, for some reason. You could do something like this:

IS_64BIT = tuple.__itemsize__ > 4

While not extensively documented, a lot of built-in types have __itemsize__ and __basicsize__ attributes, which describes the memory requirement of the underlying structure. For tuples, each item requires a pointer. Pointer size * 8 = bits in the architecture. 4 * 8 = 32-bit architecture, and 8 * 8 = 64-bit architecture.

Even though documentation isn't great, the __itemsize__ approach works back to at least Python 2.6 and forward to Python 3.7. Memory profilers like pympler use __itemsize__ and it might work for you, too!


  1. Once the conceptual design is approved by the client, the design is then progressed into more detail and shared with other parties such as structural and MEP engineers.Zaha Hadid

  2. i’m for the first time here. I found this board and I find It really useful & it helped me out a lot. psychology assignment helpI hope to give something back and help others like you helped me.

  3. What sets us apart from other security companies is that we put lay security driver
    a lot of emphasis on personal protection. We have the latest security gadgets, arms, tracking devices, and all accessories that are necessary to detect threats and protect our clients. This is perhaps the leading reason why we are the first choice for many high-end clients who need protection around the clock.

  4. The time period additionally changes as per the degree level, for example, for dissertation help service doctorate or phd level, the greatest time limit permitted is eight years in any case the degree is denied and the understudy's enrollment is likewise dropped.

  5. Python is an object-oriented, significant-level programming language.
    Python is generally utilized for creating sites and programming, task computerization, information examination, and information perception. Since it's moderately simple to learn, Python has been taken on by numerous non-software engineers like bookkeepers and researchers for an assortment of regular undertakings, such as getting sorted out funds. Best PhD writing service in UK

  6. Online dissertation help is such a blessing. I had to submit a report about how Python should be added in normal curriculum and I got the report done in just two days. It is simply amazing. I love their service and how humble they are. Very professional and helpful. I would recommend the website to all of my friends.

  7. This comment has been removed by the author.

  8. I am glad you take pride in what you write. This makes you stand way out from many other https://assignment help expert online
    writers that push poorly written content.