cpython

Why is code using intermediate variables faster than code without?

扶醉桌前 提交于 2019-11-28 15:54:57
问题 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 = tuple(range(2000)); a==b" 10000 loops, best of 3: 70.7 usec per loop How come comparison with variable assignment is faster than using a one liner with temporary variables by more than 27%? By the Python docs, garbage collection is disabled during timeit so

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?

蹲街弑〆低调 提交于 2019-11-28 15:44:52
问题 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%? 回答1: I'm going to assume any readers of this question to have read both: Zero Piraeus' answer and My explanation of CPython's dictionaries. The first thing to note is that hash randomization is decided on interpreter start-up. The hash of each letter will

IronPython vs. Python .NET

妖精的绣舞 提交于 2019-11-28 15:29:36
问题 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 solutions? 回答1: If you want to mainly base your code on the .NET framework, I'd highly recommend IronPython vs Python.NET. IronPython is pretty much native .NET - so it just works great when integrating with other .NET langauges. Python.NET is good

How exactly is Python Bytecode Run in CPython?

守給你的承諾、 提交于 2019-11-28 15:19:31
I am trying to understand how Python works (because I use it all the time!). To my understanding, when you run something like python script.py, the script is converted to bytecode and then the interpreter/VM/CPython–really just a C Program–reads in the python bytecode and executes the program accordingly. How is this bytecode read in? Is it similar to how a text file is read in C? I am unsure how the Python code is converted to machine code. Is it the case that the Python interpreter (the python command in the CLI) is really just a precompiled C program that is already converted to machine

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

℡╲_俬逩灬. 提交于 2019-11-28 13:26:42
问题 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. >>> from timeit import timeit >>> timeit("[x for x in 'abc']") 2.0691067844831528 >>> timeit("[x for x in ['a', 'b', 'c']]") 1.5286479570345861 What's happening on a lower level that's causing this? 回答1: TL;DR The actual speed difference is closer to

Storing Python objects in a Python list vs. a fixed-length Numpy array

寵の児 提交于 2019-11-28 13:25:01
In doing some bioinformatics work, I've been pondering the ramifications of storing object instances in a Numpy array rather than a Python list, but in all the testing I've done the performance was worse in every instance. I am using CPython. Does anyone know the reason why? Specifically: What are the performance impacts of using a fixed-length array numpy.ndarray(dtype=object) vs. a regular Python list? Initial tests I performed showed that accessing the Numpy array elements was slower than iteration through the Python list, especially when using object methods. Why is it faster to

Python的C/C++扩展——Python的C语言接口

 ̄綄美尐妖づ 提交于 2019-11-28 10:34:07
文章首发于我的技术博客:你可以在上面看到更多的 Python教程 和 python爬虫 Python语言最初是用C语言实现的一种脚本语言,后来被称为CPython,是因为后来又有其它语言实现的Python,比如Python实现的Python——PyPy,Java语言实现的Python——Jython,.Net实现的Python——IronPython。 CPython具有优良的开放性和可扩展性,并提供了方便灵活的应用程序接口(API),从而使得C/C++程序员能够在各个级别上对Python解释器的功能进行扩展。 Python的C语言接口很适合封装C语言实现的各种函数,如果要封装C++的类,使用boost_python或者SWIG更方便和合适。 1 模块封装 假设我们有一个C函数: /* 文件名: mylib.c */ int addone(int a) { return a+1; } 如果想在Python解释器中调用该函数,则应该首先将其实现为Python中的一个模块,这需要编写相应的封装接口,如下所示: /* wrap_mylib.c */ #include <Python.h> #include "mylib.h" PyObject* wrap_addone(PyObject* self, PyObject* args) { int n, result; if (! PyArg

Comparing None with built-in types using arithmetic operators?

你。 提交于 2019-11-28 08:58:15
Python 2.7.2 (default, Jun 12 2011, 14:24:46) [MSC v.1500 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> None > 0 False >>> None == 0 False >>> None < 0 True Is comparing None using arithmetic operators well defined for built-in types (integers in this case)? Is the difference between the first two and the third comparison part of language specification (Python's specification - you must be kidding :)) or is it CPython's implementation detail? The only meaningful comparison you can really use with None is if obj is None: (or if obj is not

can you recover from reassigning __builtins__ in python?

徘徊边缘 提交于 2019-11-28 08:07:43
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 broken There is another variable __package__ still accessible, but I don't know if/how it can be used.

OrderedDict comprehensions

淺唱寂寞╮ 提交于 2019-11-28 07:54:57
Can I extend syntax in python for dict comprehensions for other dicts, like the OrderedDict in collections module or my own types which inherit from dict ? Just rebinding the dict name obviously doesn't work, the {key: value} comprehension syntax still gives you a plain old dict for comprehensions and literals. >>> from collections import OrderedDict >>> olddict, dict = dict, OrderedDict >>> {i: i*i for i in range(3)}.__class__ <type 'dict'> So, if it's possible how would I go about doing that? It's OK if it only works in CPython. For syntax I guess I would try it with a O{k: v} prefix like we