简体   繁体   English

为什么在 Python 中“a operator= b”比“a = a operator b”要快一点?

[英]Why is 'a operator= b' relatively a little faster than 'a = a operator b' in Python?

I have read this statement in a book called "Programming in Python 3", by Mark Summerfield.我在 Mark Summerfield 写的名为“Programming in Python 3”的书中读到了这句话。 This statement doesn't make a lot of sense to me.这句话对我来说没有多大意义。 So can you help me to resolve this little but important statement?那么你能帮我解决这个小而重要的声明吗?

The augmented version looks up a's value only once,so it is potentially faster.增强版本只查找 a 的值一次,因此它可能更快。

Chapter 1 "Python's Beautiful Heart", #6 Arithmetic Operators, p31, 4th last line第 1 章“Python 的美丽之心”,#6 算术运算符,第 31 页,倒数第 4 行

很好地解释了为什么分配i=i+1i+=1稍慢 基本上,它们在低级别对应于处理器的不同工作方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么“如果不是(a和b)”比“如果不是或不是b”更快? - Why is “if not (a and b)” faster than “if not a or not b”? 为什么“不”运算符比 python 中的常规条件更快? - Why “not” operator is faster than regular condition in python? Python 2:为什么楼层划分算子比普通划分算子快? - Python 2: Why is floor division operator faster than normal division operator? Python赋值运算符优先级 - (a, b) = a[b] = {}, 5 - Python Assignment Operator Precedence - (a, b) = a[b] = {}, 5 为什么is_(a,b)函数比python 2.7.3中的eq(a,b)工作得更快? - Why does the is_(a,b) function work faster than eq(a,b) in python 2.7.3? 元组相等运算符:'a', 'b' == ('a', 'b') - equality operator with tuple: 'a', 'b' == ('a', 'b') a * a//b 在 Python 中无法正常工作(// 运算符) - a * a//b doesn't work properly in Python (// Operator) python中的b / w!=和&lt;&gt;比较运算符有什么区别? - What is the difference b/w != and <> comparison operator in python? 为什么a.dot(b)比@b更快,尽管Numpy推荐@b - Why is a.dot(b) faster than a@b although Numpy recommends a@b 为什么 a / (b*c) 在 Python 中(以及一般情况下)比 a / b / c 慢? - Why is a / (b*c) slower than a / b / c in Python (and in general)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM