Tag: python internals

为什么元组的内存空间less于列表?

一个tuple在Python中占用较less的内存空间: >>> a = (1,2,3) >>> a.__sizeof__() 48 而list s需要更多的内存空间: >>> b = [1,2,3] >>> b.__sizeof__() 64 Python内存pipe理的内部会发生什么?

在Python中hash(n)== n是什么时候?

我一直在玩Python的散列函数 。 对于小整数,它总是出现hash(n) == n 。 然而,这并没有扩大到大数目: >>> hash(2**100) == 2**100 False 我并不感到惊讶,我知道哈希值取值范围有限。 这个范围是什么? 我尝试使用二进制searchfind最小数字hash(n) != n >>> import codejamhelpers # pip install codejamhelpers >>> help(codejamhelpers.binary_search) Help on function binary_search in module codejamhelpers.binary_search: binary_search(f, t) Given an increasing function :math:`f`, find the greatest non-negative integer :math:`n` such that :math:`f(n) \le t`. If :math:`f(n) > t` […]

为什么tuple(set())== tuple(set())85%的时间哈希随机化启用?

给零比雷埃夫斯的另一个问题的答案 ,我们有这个 x = tuple(set([1, "a", "b", "c", "z", "f"])) y = tuple(set(["a", "b", "c", "z", "f", 1])) print(x == y) 大约85%的时间打印True 哈希随机化启用。 为什么85%?

Python是否优化了一个只用作返回值的variables?

以下两个代码片段之间有什么最终的区别? 第一个赋值给一个函数中的variables,然后返回该variables。 第二个函数直接返回值。 Python会把它们变成等效的字节码吗? 其中一个更快? 案例1 : def func(): a = 42 return a 案例2 : def func(): return 42

为什么Python 3.5中的str.translate比Python 3.4更快?

我试图从Python 3.4中使用text.translate()从给定的string中删除不需要的字符。 最小的代码是: import sys s = 'abcde12345@#@$#%$' mapper = dict.fromkeys(i for i in range(sys.maxunicode) if chr(i) in '@#$') print(s.translate(mapper)) 它按预期工作。 但是,在Python 3.4和Python 3.5中执行相同的程序会产生很大的差异。 计算时间的代码是 python3 -m timeit -s "import sys;s = 'abcde12345@#@$#%$'*1000 ; mapper = dict.fromkeys(i for i in range(sys.maxunicode) if chr(i) in '@#$'); " "s.translate(mapper)" Python 3.4程序需要1.3ms,而Python 3.5中的相同程序只需要26.4μs 。 在Python 3.5中有哪些改进,使得它比Python 3.4更快?

为什么迭代一个小string比一个小列表慢?

我正在玩timeit,注意到对一个小string做一个简单的列表理解要比在一个小单string列表上做同样的操作花费的时间要长。 任何解释? 这几乎是时间的1.35倍。 >>> from timeit import timeit >>> timeit("[x for x in 'abc']") 2.0691067844831528 >>> timeit("[x for x in ['a', 'b', 'c']]") 1.5286479570345861 什么发生在一个较低的水平,这是由此造成的?

为什么在Python 3中x ** 4.0比x ** 4更快?

为什么x**4.0比x**4快? 我正在使用CPython 3.5.2。 $ python -m timeit "for x in range(100):" " x**4.0" 10000 loops, best of 3: 24.2 usec per loop $ python -m timeit "for x in range(100):" " x**4" 10000 loops, best of 3: 30.6 usec per loop 我尝试改变我提出的权力,看看它是如何行动的,例如,如果我把x从10增加到16,那么从30增加到35,但是如果我以浮动的方式增加10.0 ,那么它只是移动在24.1〜4左右。 我想这与浮点数转换和2的幂可能有关,但我不知道。 我注意到,在这两种情况下,2的幂更快,我猜是因为这些计算对于解释器/计算机来说更为原生/容易。 但是,花车几乎不动。 2.0 => 24.1~4 & 128.0 => 24.1~4 但是 2 […]

为什么'x'在('x',)比'x'=='x'快?

>>> timeit.timeit("'x' in ('x',)") 0.04869917374131205 >>> timeit.timeit("'x' == 'x'") 0.06144205736110564 也适用于具有多个元素的元组,这两个版本似乎都是线性增长的: >>> timeit.timeit("'x' in ('x', 'y')") 0.04866674801541748 >>> timeit.timeit("'x' == 'x' or 'x' == 'y'") 0.06565782838087131 >>> timeit.timeit("'x' in ('y', 'x')") 0.08975995576448526 >>> timeit.timeit("'x' == 'y' or 'x' == 'y'") 0.12992391047427532 基于此,我认为我应该完全开始in各地使用in而不是== !

time.sleep – 睡觉线程或进程?

在* nix的Python中, time.sleep()阻塞线程或进程?

为什么一个类的主体在定义时间被执行?

与函数相反,类的主体在定义时执行: class A(object): print 'hello' date: hello 为什么是这样? 它与@classmethod / @staticmethod方法和类属性有关吗?