+ +
+
+
+
+ + +
+
+
处理中...
+
+ + +
+
+
+

日志目录: {{ LOG_DIR }}

+ +
+
+

请点击"获取日志"按钮开始扫描...

+
+
+
+ + +
+
+
+

文件目录: {{ FILE_DIR }}

+ +
+
+

请点击"刷新文件"按钮开始扫描...

+
+
+
+ + + + \ No newline at end of file diff --git a/tools/pikapython-c/test_example.py b/tools/pikapython-c/test_example.py new file mode 100644 index 000000000..d4fa60fa7 --- /dev/null +++ b/tools/pikapython-c/test_example.py @@ -0,0 +1,60 @@ +"""示例脚本:与 --module 注入配套使用。 + +严格按 REQUIRE.md: +运行命令: + python run_pika.py --module test_module_example test_example.py + +脚本自身不负责放置模块文件;模块目录需预先存在于根目录: + ./test_module_example/ + test_module_example.pyi + test_module_example_Test.c + +run_pika.py 在本次运行期间会: +1. 临时复制 .pyi 与 C 到 pikapython-linux/pikapython/ 下 +2. 强制(若需要)重新 cmake+预编译+编译 +3. 运行后清理临时复制的文件与目录 +""" +import time +import test_module_example + +# Python baseline implementation (for performance comparison) +def py_add(a: int, b: int) -> int: + return a + b + +obj = test_module_example.Test() + +# Functional checks +print('[EXAMPLE] add(7, 35)=', obj.add(7, 35)) +print('[EXAMPLE] greet:"', obj.greet('demo'), '"', sep='') +assert obj.add(1, 2) == 3 +assert obj.greet('x').startswith('Hello,') + +# Performance comparison (simplified for Pika runtime): run both versions 10000 times +ITER = 10000 +py_total = 0.0 +c_total = 0.0 +for _ in range(ITER): + t0 = time.time() + py_add(123, 456) + py_total += (time.time() - t0) + t1 = time.time() + obj.add(123, 456) + c_total += (time.time() - t1) + +if ITER > 0: + py_mean = py_total / ITER + c_mean = c_total / ITER +else: + py_mean = 0.0 + c_mean = 0.0 +if c_mean > 0: + ratio = py_mean / c_mean +else: + ratio = 0.0 +py_mean_us = py_mean * 1000000.0 +c_mean_us = c_mean * 1000000.0 +print('[PERF] python_total=' + ('%.6f' % py_total) + 's mean=' + ('%.2f' % py_mean_us) + 'us') +print('[PERF] cmod_total=' + ('%.6f' % c_total) + 's mean=' + ('%.2f' % c_mean_us) + 'us') +print('[PERF] speedup(py_mean/c_mean)= ' + ('%.2f' % ratio) + 'x') + +print('[EXAMPLE][SELFTEST] test_module_example OK') diff --git a/tools/pikapython-c/test_module_example/test_module_example.pyi b/tools/pikapython-c/test_module_example/test_module_example.pyi new file mode 100644 index 000000000..0f02c1104 --- /dev/null +++ b/tools/pikapython-c/test_module_example/test_module_example.pyi @@ -0,0 +1,5 @@ +# 注入示例 .pyi:被 run_pika.py --module test_module_example 直接复制到 pikapython/ 下 +# 单一路径:不自动生成、不回退、无需多模式 +class Test: + def add(self, a: int, b: int) -> int: ... + def greet(self, name: str) -> str: ... diff --git a/tools/pikapython-c/test_module_example/test_module_example_Test.c b/tools/pikapython-c/test_module_example/test_module_example_Test.c new file mode 100644 index 000000000..5fa6d4a58 --- /dev/null +++ b/tools/pikapython-c/test_module_example/test_module_example_Test.c @@ -0,0 +1,11 @@ +#include "pikaScript.h" +// 注入示例 C 源: run_pika.py --module test_module_example 复制使用 (无自动生成) +int test_module_example_Test_add(PikaObj* self, int a, int b){ + return a + b; +} +char* test_module_example_Test_greet(PikaObj* self, char* name){ + char buf[64]; + int n = snprintf(buf, sizeof(buf), "Hello,%s", name); + (void)n; + return obj_cacheStr(self, buf); +}