refactor: cleanup test code

This commit is contained in:
2025-04-03 13:45:37 +08:00
parent ef14e4851c
commit f03fc66c1e
10 changed files with 152 additions and 149 deletions

View File

@@ -0,0 +1,47 @@
import sys
import os
import random
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../src")))
from tpre import add, multiply, sm2p256v1
import time
# 生成元
g = (sm2p256v1.Gx, sm2p256v1.Gy)
def test_rust_vs_python_multiply():
mul_times = random.randint(0, sm2p256v1.N - 1)
# Rust实现
start_time = time.time()
for _ in range(10):
_ = multiply(g, mul_times, 1)
rust_time = time.time() - start_time
print(f"\nRust multiply 执行时间: {rust_time:.6f}")
# Python实现
start_time = time.time()
for _ in range(10):
_ = multiply(g, mul_times, 0)
python_time = time.time() - start_time
print(f"Python multiply 执行时间: {python_time:.6f}")
assert rust_time < python_time, "Rust实现应该比Python更快"
def test_rust_vs_python_add():
# Rust实现
start_time = time.time()
for _ in range(10):
_ = add(g, g, 1)
rust_time = time.time() - start_time
print(f"\nRust add 执行时间: {rust_time:.6f}")
# Python实现
start_time = time.time()
for _ in range(10):
_ = add(g, g, 0)
python_time = time.time() - start_time
print(f"Python add 执行时间: {python_time:.6f}")
assert rust_time < python_time, "Rust实现应该比Python更快"