fy
2016-05-08 15:27:18 +08:00
首先是看看字节码,好像区别不大
>>> import dis
>>> dis.dis('a += 1')
1 0 LOAD_NAME 0 (a)
3 LOAD_CONST 0 (1)
6 INPLACE_ADD
7 STORE_NAME 0 (a)
10 LOAD_CONST 1 (None)
13 RETURN_VALUE
>>>
>>> dis.dis('a = a + 1')
1 0 LOAD_NAME 0 (a)
3 LOAD_CONST 0 (1)
6 BINARY_ADD
7 STORE_NAME 0 (a)
10 LOAD_CONST 1 (None)
13 RETURN_VALUE
>>>
然后去看字节码解析,貌似一毛一样
ceval.c
TARGET(BINARY_ADD) {
PyObject *right = POP();
PyObject *left = TOP();
PyObject *sum;
if (PyUnicode_CheckExact(left) &&
PyUnicode_CheckExact(right)) {
sum = unicode_concatenate(left, right, f, next_instr);
/* unicode_concatenate consumed the ref to v */
}
else {
sum = PyNumber_Add(left, right);
Py_DECREF(left);
}
Py_DECREF(right);
SET_TOP(sum);
if (sum == NULL)
goto error;
DISPATCH();
}
TARGET(INPLACE_ADD) {
PyObject *right = POP();
PyObject *left = TOP();
PyObject *sum;
if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
sum = unicode_concatenate(left, right, f, next_instr);
/* unicode_concatenate consumed the ref to v */
}
else {
sum = PyNumber_InPlaceAdd(left, right);
Py_DECREF(left);
}
Py_DECREF(right);
SET_TOP(sum);
if (sum == NULL)
goto error;
DISPATCH();
}
然后观察实现:
abstract.c
PyObject *
PyNumber_Add(PyObject *v, PyObject *w)
{
PyObject *result = binary_op1(v, w, NB_SLOT(nb_add));
if (result == Py_NotImplemented) {
PySequenceMethods *m = v->ob_type->tp_as_sequence;
Py_DECREF(result);
if (m && m->sq_concat) {
return (*m->sq_concat)(v, w);
}
result = binop_type_error(v, w, "+");
}
return result;
}
PyObject *
PyNumber_InPlaceAdd(PyObject *v, PyObject *w)
{
PyObject *result = binary_iop1(v, w, NB_SLOT(nb_inplace_add),
NB_SLOT(nb_add));
if (result == Py_NotImplemented) {
PySequenceMethods *m = v->ob_type->tp_as_sequence;
Py_DECREF(result);
if (m != NULL) {
binaryfunc f = NULL;
f = m->sq_inplace_concat;
if (f == NULL)
f = m->sq_concat;
if (f != NULL)
return (*f)(v, w);
}
result = binop_type_error(v, w, "+=");
}
return result;
}
于是我思考之后得出了结论:
他们有什么不同呢?最终调用的槽不同。
但是 python 代码来测试的结果,确实是 += 比 = + 快一点