cpython

CPython string addition optimisation failure case

淺唱寂寞╮ 提交于 2019-11-27 06:01:36
问题 The Question Why, in CPython, does def add_string(n): s = '' for _ in range(n): s += ' ' take linear time, but def add_string_in_list(n): l = [''] for _ in range(n): l[0] += ' ' take quadratic time? Proof: Timer(partial(add_string, 1000000)).timeit(1) #>>> 0.1848409200028982 Timer(partial(add_string, 10000000)).timeit(1) #>>> 1.1123797750042286 Timer(partial(add_string_in_list, 10000)).timeit(1) #>>> 0.0033865350123960525 Timer(partial(add_string_in_list, 100000)).timeit(1) #>>> 0

Using NumPy and Cpython with Jython

核能气质少年 提交于 2019-11-27 04:32:28
I must use a commercial Java library, and would like to do it from Python. Jython is robust and I am fine with it being a few dot releases behind. However, I would like to use NumPy as well, which obviously does not work with Jython. Options like CPype and Java numeric libraries are unappealing. The former is essentially dead. The latter are mostly immature and lack the ease of use and wide acceptance of NumPy. My question is: How can one have Jython and Python code interoperate? It would be acceptable for me to call Jython from Cpython or the other way around. It's ironic, considering that

How is tuple implemented in CPython?

心已入冬 提交于 2019-11-27 04:04:14
I've been trying to learn how CPython is implemented under the scenes. It's great that Python is high level, but I don't like treating it like a black box. With that in mind, how are tuples implemented? I've had a look at the source (tupleobject.c) , but it's going over my head. I see that PyTuple_MAXSAVESIZE = 20 and PyTuple_MAXFREELIST = 2000 , what is saving and the "free list"? (Will there be a performance difference between tuples of length 20/21 or 2000/2001? What enforces the maximum tuple length?) As a caveat, everything in this answer is based on what I've gleaned from looking over

ValueError: failed to parse CPython sys.version after using conda command

有些话、适合烂在心里 提交于 2019-11-27 03:21:53
问题 I'm running into an error that I can't solve despite others having reported the same error. I am connecting remotely to a Linux machine. I have installed the latest version of anaconda: $ bash Anaconda2-2.4.0-Linux-x86_64.sh // A lot of python libraries get installed installing: _cache-0.0-py27_x0 ... Python 2.7.10 :: Continuum Analytics, Inc. creating default environment... installation finished. I updated the corresponding paths and it seems like it works: $ python Python 2.7.10 |Anaconda 2

can you recover from reassigning __builtins__ in python?

走远了吗. 提交于 2019-11-27 02:04:54
问题 If I open up interactive mode and type: __builtins__ = 0 # breaks everything have I completely broken the session? If so, what is going on behind the scenes to assign __builtins__ to the builtin module that can't be handled by the interpreter? If not, how can I recover from this? Just a few of my own attempts to fix it: Any attempt to import anything results in an error "ImportError __import__ not found" all functions I might use to do anything other than evaluate numerical expressions are

deque.popleft() and list.pop(0). Is there performance difference?

淺唱寂寞╮ 提交于 2019-11-27 01:51:42
问题 deque.popleft() and list.pop(0) seem to return the same result. Is there any performance difference between them and why? 回答1: deque.popleft() is faster than list.pop(0), because the deque has been optimized to do popleft() approximately in O(1), while list.pop(0) takes O(n) (see deque objects). Comments and code in _collectionsmodule.c for deque and listobject.c for list provide implementation insights to explain the performance differences. Namely that a deque object "is composed of a

Are sets ordered like dicts in python3.6

与世无争的帅哥 提交于 2019-11-27 01:33:35
Due to changes in dict implementation in Python 3.6 it is now ordered by default. Do set s preserve order as well now? I could not find any information about it but as both of those data structures are very similar in the way they work under the hood I thought it might be the case. I know there is no promise for dict s to be ordered in all cases but they are most of the time. As stated in Python docs: The order-preserving aspect of this new implementation is considered an implementation detail and should not be relied upon No, set s are still unordered. You can verify this just by displaying a

What is python-dev package used for

*爱你&永不变心* 提交于 2019-11-27 00:20:38
问题 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 to install lxml by using the command pip install lxml . However as because I am using Python 3.4.0, I thought that may be there are different version of python-dev (thought came due to facing some similar version conflict problem). So I tried to

Change in max length of interned strings in CPython

余生颓废 提交于 2019-11-26 23:31:13
问题 Python 2 interned "all name-character" strings up to 20 code points long: Python 2.7.15 (default, Feb 9 2019, 16:01:32) [GCC 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.10.44.4)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> 'a' * 20 is 'aaaaaaaaaaaaaaaaaaaa' True >>> 'a' * 21 is 'aaaaaaaaaaaaaaaaaaaaa' False In Python 3.7, this number has seemingly increased to 4096: Python 3.7.2 (default, Jan 13 2019, 12:50:01) [Clang 10.0.0 (clang-1000.11.45.5)] on

Why is 'new_file += line + string' so much faster than 'new_file = new_file + line + string'? [duplicate]

情到浓时终转凉″ 提交于 2019-11-26 23:09:35
问题 This question already has an answer here: Why is variable1 += variable2 much faster than variable1 = variable1 + variable2? 1 answer Our code takes 10 minutes to siphon thru 68,000 records when we use: new_file = new_file + line + string However when we do the following it takes just 1 second: new_file += line + string Here is the code: for line in content: import time import cmdbre fname = "STAGE050.csv" regions = cmdbre.regions start_time = time.time() with open(fname) as f: content = f