Автор доклада: Александр Сапронов.
Обзорный мини-доклад. В докладе вы найдете причину релиза 2.7.11 и новинки Python 3.5
Доклад сделан на 2 встрече сообщества PyNSK
Direct Style Effect Systems -The Print[A] Example- A Comprehension Aid
Commit ускоривший python 2.7.11 на 30% и новое в python 3.5
1. Commit ускоривший
Python 2.7 на 30%
и
новое в Python 3.5
sapronov.alexander92@gmail.com
@sapronovalex92
pynsk.nsk@gmail.com
vk.com/pynsk
facebook.com/PyNskCom
@py_nsk
Александр Сапронов:
2. Python 2.7.11
Vamsi Parasa of the Server Scripting Languages Optimization team at Intel posted
a patch to change the switch statement that executes Python bytecode in the
CPython interpreter to use computed gotos instead.
(http://article.gmane.org/gmane.comp.python.devel/153401)
3. Python 2.7.11
Vamsi Parasa of the Server Scripting Languages Optimization team at Intel posted
a patch to change the switch statement that executes Python bytecode in the
CPython interpreter to use computed gotos instead.
(http://article.gmane.org/gmane.comp.python.devel/153401)
4. computed goto
В отличие от switch-case: Не производит граничных проверок
#define OP_HALT 0x0
#define OP_INC 0x1
int interp_switch(unsigned char* code, int initval) {
int pc = 0;
int val = initval;
while (1) {
switch (code[pc++]) {
case OP_HALT:
return val;
case OP_INC:
val++;
break;
default:
return val;
}
}
}
5. computed goto
В отличие от switch-case: Не производит граничных проверок
#define OP_HALT 0x0
#define OP_INC 0x1
int interp_switch(unsigned char* code, int initval) {
int pc = 0;
int val = initval;
while (1) {
switch (code[pc++]) {
case OP_HALT:
return val;
case OP_INC:
val++;
break;
default:
return val;
}
}
}
int interp_cgoto(unsigned char* code, int initval) {
static void* dispatch_table[] = {&&do_halt, &&do_inc};
#define DISPATCH() goto *dispatch_table[code[pc++]]
int pc = 0;
int val = initval;
DISPATCH();
while (1) {
do_halt:
return val;
do_inc:
val++;
DISPATCH();
}
}
6. computed goto
В отличие от switch-case: CPU может лучше прогнозировать ветвления
Подробно про computed goto:
http://eli.thegreenplace.net/2012/07/12/computed-goto-for-efficient-dispatch-tables
Модуль предсказания переходов:
устройство, входящее в состав микропроцессоров, имеющих
конвейерную архитектуру, предсказывающее, будет ли
выполнен условный переход в исполняемой программе.
7. Новое в Python 3.5 - PEP 478
● PEP 441 , improved Python zip application support
● PEP 448 , additional unpacking generalizations
● PEP 461 , "%-formatting" for bytes and bytearray objects
● PEP 465 , a new operator ("@") for matrix multiplication
● PEP 471 , os.scandir(), a fast new directory traversal function
● PEP 475 , adding support for automatic retries of interrupted system calls
● PEP 479 , change StopIteration handling inside generators
● PEP 484 , the typing module, a new standard for type annotations
● PEP 485 , math.isclose(), a function for testing approximate equality
● PEP 486 , making the Widnows Python launcher aware of virtual environments
● PEP 488 , eliminating .pyo files
● PEP 489 , a new and improved mechanism for loading extension modules
● PEP 492 , coroutines with async and await syntax
9. PEP 484 - the typing module, a new standard
for type annotations
def greeting(name: str) -> str:
return 'Hello ' + name
from typing import *
T = TypeVar('T')
def filter(function: Optional[Callable[[T], Any]],
iterable: Iterable[T]) -> Iterator[T]:
...
Было
Стало
10. PEP 492 - coroutines with async and await syntax
async def read_data(db):
pass
async def read_data(db):
data = await db.fetch('SELECT ...')
Новые ключевые слова:
async и await