Update README.

This commit is contained in:
ykiko 2024-06-19 12:32:58 +08:00
parent 59bbecdbee
commit d52cbec80c

View File

@ -91,39 +91,33 @@ python scripts/run_tests.py
### Example ### Example
```cpp ```cpp
#include "pocketpy.h" #include <pybind11/pybind11.h>
using namespace pkpy; namespace py = pybind11;
int main(){ int main() {
// Create a virtual machine // Start the interpreter
VM* vm = new VM(); py::scoped_interpreter guard{};
// Hello world! // Hello world!
vm->exec("print('Hello world!')"); py::exec("print('Hello world!')");
// Create a list // Create a list
vm->exec("a = [1, 2, 3]"); py::exec("a = [1, 2, 3]");
// Eval the sum of the list // Eval the sum of the list
PyVar result = vm->eval("sum(a)"); auto result = py::eval("sum(a)");
std::cout << "Sum of the list: "<< py_cast<int>(vm, result) << std::endl; // 6 std::cout << "Sum of the list: " << result.cast<int>() << std::endl; // 6
// Bindings // Bindings
vm->bind(vm->_main, "add(a: int, b: int)", auto m = py::module_::__main__();
[](VM* vm, ArgsView args){ m.def("add", [](int a, int b) {
int a = py_cast<int>(vm, args[0]); return a + b;
int b = py_cast<int>(vm, args[1]);
return py_var(vm, a + b);
}); });
// Call the function // Call the function
PyVar f_add = vm->_main->attr("add"); std::cout << "Sum of 2 variables: " << m.attr("add")(1, 2).cast<int>() << std::endl; // 10
result = vm->call(f_add, py_var(vm, 3), py_var(vm, 7));
std::cout << "Sum of 2 variables: "<< py_cast<int>(vm, result) << std::endl; // 10
// Dispose the virtual machine
delete vm;
return 0; return 0;
} }
``` ```
@ -166,7 +160,7 @@ See https://pocketpy.dev/performance/ for details.
And these are the results of the primes benchmark on Intel i5-12400F, WSL (Ubuntu 20.04 LTS), which *roughly* reflects the performance among c++, lua, pkpy and cpython. And these are the results of the primes benchmark on Intel i5-12400F, WSL (Ubuntu 20.04 LTS), which *roughly* reflects the performance among c++, lua, pkpy and cpython.
| name | version | time | file | | name | version | time | file |
| ---- | ---- | ---- | ---- | | ------- | ------- | ------------------------- | --------------------------------------------------------------------------------------------------------------------------------- |
| c++ | gnu++11 | `0.104s ■□□□□□□□□□□□□□□□` | [benchmarks/primes.cpp](https://github.com/pocketpy/pocketpy/blob/9481d653b60b81f4590a4d48f2be496f6962261e/benchmarks/primes.cpp) | | c++ | gnu++11 | `0.104s ■□□□□□□□□□□□□□□□` | [benchmarks/primes.cpp](https://github.com/pocketpy/pocketpy/blob/9481d653b60b81f4590a4d48f2be496f6962261e/benchmarks/primes.cpp) |
| lua | 5.3.3 | `1.576s ■■■■■■■■■□□□□□□□` | [benchmarks/primes.lua](https://github.com/pocketpy/pocketpy/blob/9481d653b60b81f4590a4d48f2be496f6962261e/benchmarks/primes.lua) | | lua | 5.3.3 | `1.576s ■■■■■■■■■□□□□□□□` | [benchmarks/primes.lua](https://github.com/pocketpy/pocketpy/blob/9481d653b60b81f4590a4d48f2be496f6962261e/benchmarks/primes.lua) |
| pkpy | 1.2.7 | `2.385s ■■■■■■■■■■■■■□□□` | [benchmarks/primes.py](https://github.com/pocketpy/pocketpy/blob/9481d653b60b81f4590a4d48f2be496f6962261e/benchmarks/primes.py) | | pkpy | 1.2.7 | `2.385s ■■■■■■■■■■■■■□□□` | [benchmarks/primes.py](https://github.com/pocketpy/pocketpy/blob/9481d653b60b81f4590a4d48f2be496f6962261e/benchmarks/primes.py) |
@ -175,7 +169,7 @@ And these are the results of the primes benchmark on Intel i5-12400F, WSL (Ubunt
## Used By ## Used By
| | Description | | | Description |
|-----------------------------------------------------------------|--------------------------------------------------------------------------| | --------------------------------------------------------------- | ------------------------------------------------------------------------ |
| [TIC-80](https://github.com/nesbox/TIC-80) | TIC-80 is a fantasy computer for making, playing and sharing tiny games. | | [TIC-80](https://github.com/nesbox/TIC-80) | TIC-80 is a fantasy computer for making, playing and sharing tiny games. |
| [MiniPythonIDE](https://github.com/CU-Production/MiniPythonIDE) | A python ide base on pocketpy | | [MiniPythonIDE](https://github.com/CU-Production/MiniPythonIDE) | A python ide base on pocketpy |
| [py-js](https://github.com/shakfu/py-js) | Python3 externals for Max / MSP | | [py-js](https://github.com/shakfu/py-js) | Python3 externals for Max / MSP |