11
Commit ускоривший Python 2.7 на 30% и новое в Python 3.5 [email protected] @sapronovalex92 [email protected] vk.com/pynsk facebook.com/PyNskCom @py_nsk Александр Сапронов:

Commit ускоривший python 2.7.11 на 30% и новое в python 3.5

  • Upload
    pynsk

  • View
    579

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Commit ускоривший python 2.7.11 на 30% и новое в python 3.5

Commit ускоривший Python 2.7 на 30%

иновое в Python 3.5

[email protected]

@sapronovalex92

[email protected]

vk.com/pynsk

facebook.com/PyNskCom

@py_nsk

Александр Сапронов:

Page 2: Commit ускоривший python 2.7.11 на 30% и новое в python 3.5

Python 2.7.11Vamsi 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)

Page 3: Commit ускоривший python 2.7.11 на 30% и новое в python 3.5

Python 2.7.11Vamsi 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)

Page 4: Commit ускоривший python 2.7.11 на 30% и новое в python 3.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;

}

}

}

Page 5: Commit ускоривший python 2.7.11 на 30% и новое в python 3.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();

}

}

Page 6: Commit ускоривший python 2.7.11 на 30% и новое в python 3.5

computed gotoВ отличие от switch-case: CPU может лучше прогнозировать ветвления

Подробно про computed goto:

http://eli.thegreenplace.net/2012/07/12/computed-goto-for-efficient-dispatch-tables

Модуль предсказания переходов:устройство, входящее в состав микропроцессоров, имеющих

конвейерную архитектуру, предсказывающее, будет ли выполнен условный переход в исполняемой программе.

Page 7: Commit ускоривший python 2.7.11 на 30% и новое в python 3.5

Новое в 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

Page 8: Commit ускоривший python 2.7.11 на 30% и новое в python 3.5

PEP 448 - additional unpacking generalizations

>>> print(*[1], *[2], 3)1 2 3>>> dict(**{'x': 1}, y=2, **{'z': 3}){'x': 1, 'y': 2, 'z': 3}>>> *range(4), 4(0, 1, 2, 3, 4)>>> [*range(4), 4][0, 1, 2, 3, 4]

>>> {*range(4), 4}{0, 1, 2, 3, 4}>>> {'x': 1, **{'y': 2}}{'x': 1, 'y': 2}>>> {'x': 1, **{'x': 2}}{'x': 2}>>> {**{'x': 2}, 'x': 1}{'x': 1}

Page 9: Commit ускоривший python 2.7.11 на 30% и новое в python 3.5

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]: ...

Было

Стало

Page 10: Commit ускоривший python 2.7.11 на 30% и новое в python 3.5

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

Page 11: Commit ускоривший python 2.7.11 на 30% и новое в python 3.5

11

[email protected]

@sapronovalex92

[email protected]

vk.com/pynsk

facebook.com/PyNskCom

@py_nsk

PyNSK контакты: Мои контакты:

ru.linkedin.com/in/alexsapronov

Питоны кончились…Вопросы?