Questions tagged [cpython]

The reference implementation of the Python programming language. Use this tag for questions specific to this implementation, general Python questions should just be tagged with "python".

CPython is the default and most widely used implementation of the programming language. It is written in C.

In addition to CPython, there are several other production-quality Python implementations: , a JIT-compiler. , written in Java and , which is written for the Common Language Runtime. There are also several experimental implementations.

CPython is a bytecode interpreter. It has a foreign function interface with several languages including C, in which one must explicitly write bindings in a language other than Python.

See also

1358 questions
923
votes
3 answers

Why does Python code run faster in a function?

def main(): for i in xrange(10**8): pass main() This piece of code in Python runs in (Note: The timing is done with the time function in BASH in Linux.) real 0m1.841s user 0m1.828s sys 0m0.012s However, if the for loop isn't…
thedoctar
  • 8,943
  • 3
  • 20
  • 31
797
votes
12 answers

Why shouldn't I use PyPy over CPython if PyPy is 6.3 times faster?

I've been hearing a lot about the PyPy project. They claim it is 6.3 times faster than the CPython interpreter on their site. Whenever we talk about dynamic languages like Python, speed is one of the top issues. To solve this, they say PyPy is 6.3…
chhantyal
  • 11,874
  • 7
  • 51
  • 77
637
votes
11 answers

Python vs Cpython

What's all this fuss about Python and CPython (Jython,IronPython), I don't get it: python.org mentions that CPython is: The "traditional" implementation of Python (nicknamed CPython) yet another Stack Overflow question mentions that: CPython is…
K DawG
  • 13,287
  • 9
  • 35
  • 66
290
votes
2 answers

Why are some float < integer comparisons four times slower than others?

When comparing floats to integers, some pairs of values take much longer to be evaluated than other values of a similar magnitude. For example: >>> import timeit >>> timeit.timeit("562949953420000.7 < 562949953421000") # run 1 million…
Alex Riley
  • 169,130
  • 45
  • 262
  • 238
228
votes
6 answers

How is set() implemented?

I've seen people say that set objects in python have O(1) membership-checking. How are they implemented internally to allow this? What sort of data structure does it use? What other implications does that implementation have? Every answer here was…
Daenyth
  • 35,856
  • 13
  • 85
  • 124
148
votes
3 answers

What causes [*a] to overallocate?

Apparently list(a) doesn't overallocate, [x for x in a] overallocates at some points, and [*a] overallocates all the time? Here are sizes n from 0 to 12 and the resulting sizes in bytes for the three methods: 0 56 56 56 1 64 88 88 2 72 88 96 3 80…
Stefan Pochmann
  • 27,593
  • 8
  • 44
  • 107
133
votes
7 answers

Boolean identity == True vs is True

It is standard convention to use if foo is None rather than if foo == None to test if a value is specifically None. If you want to determine whether a value is exactly True (not just a true-like value), is there any reason to use if foo == True…
Uyghur Lives Matter
  • 18,820
  • 42
  • 108
  • 144
133
votes
3 answers

Why is it slower to iterate over a small string than a small list?

I was playing around with timeit and noticed that doing a simple list comprehension over a small string took longer than doing the same operation on a list of small single character strings. Any explanation? It's almost 1.35 times as much time. >>>…
Sunjay Varma
  • 5,007
  • 6
  • 34
  • 51
131
votes
4 answers

Why (0-6) is -6 = False?

I found something strange while debugging some code. Apparently, >>> (0-6) is -6 False but, >>> (0-5) is -5 True Why does this happen?
Dmitry Zagorulkin
  • 8,370
  • 4
  • 37
  • 60
100
votes
3 answers

Can we make 1 == 2 true?

Python ints are objects that encapsulate the actual number value. Can we mess with that value, for example setting the value of the object 1 to 2? So that 1 == 2 becomes True?
Kelly Bundy
  • 23,480
  • 7
  • 29
  • 65
98
votes
1 answer

Why does tuple(set([1,"a","b","c","z","f"])) == tuple(set(["a","b","c","z","f",1])) 85% of the time with hash randomization enabled?

Given Zero Piraeus' answer to another question, we have that x = tuple(set([1, "a", "b", "c", "z", "f"])) y = tuple(set(["a", "b", "c", "z", "f", 1])) print(x == y) Prints True about 85% of the time with hash randomization enabled. Why 85%?
Veedrac
  • 58,273
  • 15
  • 112
  • 169
94
votes
3 answers

What is python-dev package used for

I recently installed lxml. Before that, I had to install all the dependencies for that. So I tried to install liblxml2-dev, liblxslt1-dev and python-dev (google-searched for what packages are required for lxml) but even after that, I could not able…
Abhisek
  • 4,610
  • 3
  • 17
  • 27
93
votes
10 answers

IronPython vs. Python .NET

I want to access some .NET assemblies written in C# from Python code. A little research showed I have two choices: IronPython with .NET interface capability/support built-in Python with the Python .NET package What are the trade-offs between both…
cschol
  • 12,799
  • 11
  • 66
  • 80
82
votes
2 answers

list() uses slightly more memory than list comprehension

So i was playing with list objects and found little strange thing that if list is created with list() it uses more memory, than list comprehension? I'm using Python 3.5.2 In [1]: import sys In [2]: a = list(range(100)) In [3]:…
vishes_shell
  • 22,409
  • 6
  • 71
  • 81
79
votes
2 answers

Why is code using intermediate variables faster than code without?

I have encountered this weird behavior and failed to explain it. These are the benchmarks: py -3 -m timeit "tuple(range(2000)) == tuple(range(2000))" 10000 loops, best of 3: 97.7 usec per loop py -3 -m timeit "a = tuple(range(2000)); b =…
Bharel
  • 23,672
  • 5
  • 40
  • 80
1
2 3
90 91