Python Catania - Stefano Borzì
Python extendibility
Python extendibility
print('Hello World!')#include <iostream>
using namespace std;
int main() {
cout << "Hello World!" << endl;
return 0;
}
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}打印 你好世界Python extendibility
" The ratings are based on the number of skilled engineers world-wide, courses and third party vendors "
Python extendibility
" In 2024, Python overtook JavaScript as the most popular language on GitHub "
Python extendibility
Python extendibility
counts = {}
for x in data:
counts[x] = counts.get(x, 0) + 1
from collections import Counter
counts = Counter(data)
Python extendibility
import time
from collections import Counter
data = [i % 10 for i in range(10_000_000)]
# Manual loop
t0 = time.time()
counts = {}
for x in data:
counts[x] = counts.get(x, 0) + 1
t1 = time.time()
print("Manual loop:", t1 - t0)
# Counter
t0 = time.time()
counts2 = Counter(data)
t1 = time.time()
print("Counter:", t1 - t0)
Manual loop: 0.727339506149292
Counter:
0.31299853324890137
Python extendibility
Python extendibility
#define PY_SSIZE_T_CLEAN
#include <Python.h>
// Define the C function that implements "add"
static PyObject* myext_add(PyObject* self, PyObject* args) {
double a, b;
if (!PyArg_ParseTuple(args, "dd", &a, &b)) {
return NULL; // invalid arguments
}
double result = a + b;
return Py_BuildValue("d", result); // return a Python float
}
// Method definitions
static PyMethodDef MyExtMethods[] = {
{"add", myext_add, METH_VARARGS, "Add two numbers"},
{NULL, NULL, 0, NULL}
};
// Module definition
static struct PyModuleDef myextmodule = {
PyModuleDef_HEAD_INIT,
"myext", // module name
NULL, // optional docstring
-1, // size of per-interpreter state or -1
MyExtMethods
};
// Module initialization
PyMODINIT_FUNC PyInit_myext(void) {
return PyModule_Create(&myextmodule);
}
myext.c
Python extendibility
from setuptools import setup, Extension
module = Extension("myext", sources=["myext.c"])
setup(
name="myext",
version="1.0",
description="Simple example of Python C extension",
ext_modules=[module],
)
$ pip install .
setup.py
import myext
print(myext.add(3.5, 4.2))
Python extendibility
Python Catania - Stefano Borzì
Python multithreading
from time import sleep
arr = range(100)
for x in arr:
print(x)
sleep(1)
Python multithreading
from time import sleep
from tqdm import tqdm
arr = range(100)
for x in tqdm(arr):
# print(x)
sleep(1)
Python multithreading
from time import sleep
import pathos as pa
arr = range(100)
def parallel():
sleep(1)
ncpu = pa.helpers.cpu_count()
with pa.multiprocessing.ProcessingPool(ncpu) as p:
p.map(parallel, arr), total=len(arr)Python multithreading
from time import sleep
from tqdm import tqdm
import pathos as pa
arr = range(100)
def parallel():
sleep(1)
ncpu = pa.helpers.cpu_count()
with pa.multiprocessing.ProcessingPool(ncpu) as p:
list(tqdm(p.imap(parallel, arr), total=len(arr)))Python multithreading
Python multithreading
Python multithreading
Python has a built-in mechanism that limits how threads are executed, and it’s called the Global Interpreter Lock, or GIL.
Python multithreading
Python multithreading
Python extendibility