🔴 三步走一遍
🔴 这一节的 Python 题会**在运行时调 gcc 现编一个 .so,再用 ctypes 加载进来**——真的跨语言调用,不是假装。判题机需要 gcc、一个可写的临时目录,以及允许 Python 起子进程。⚠️ 这条路线**一个答案都不是耗时**。耗时跟机器、跟负载、跟编译器都有关,换台机器数字就变了。**能当答案的只有"走了多少步"和"两边结果一不一样"。**
import ctypes, os, subprocess, tempfile
def build_c(src, name="m"):
"""把一段 C 现场编成 .so,交回加载好的库。
判题机需要 gcc 和一个可写的临时目录。"""
d = tempfile.mkdtemp()
c_path = os.path.join(d, name + ".c")
so_path = os.path.join(d, name + ".so")
with open(c_path, "w") as f:
f.write(src)
subprocess.run(["gcc", "-shared", "-fPIC", "-O2", "-o", so_path, c_path],
check=True, capture_output=True)
return ctypes.CDLL(so_path)
C_SRC = r"""
long sum_sq(const int *a, int n) {
long s = 0;
for (int i = 0; i < n; i++) s += (long)a[i] * a[i];
return s;
}
"""
data = list(range(1, 11))
# ① 构建
built = 0
lib = None
try:
lib = build_c(C_SRC)
built = 1
except Exception:
built = 0
# ② 调用
lib.sum_sq.argtypes = [ctypes.POINTER(ctypes.c_int), ctypes.c_int]
lib.sum_sq.restype = ctypes.c_long
Arr = ctypes.c_int * len(data)
c_result = lib.sum_sq(Arr(*data), len(data))
# ③ 验证:拿纯 Python 那份当基准,两边必须一模一样
py_result = sum(x * x for x in data)
same = int(c_result == py_result)
print(f"{built}/{c_result}/{py_result}/{same}")
全部评论