Skip to content

Commit 2adf783

Browse files
committed
Optimize Linux VM benchmark fast paths
1 parent 9bca87e commit 2adf783

10 files changed

Lines changed: 968 additions & 170 deletions

File tree

tools/pikaByteCodeGen/CMakeLists.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ ADD_DEFINITIONS(
88
-DCROSS_BUILD
99
)
1010
aux_source_directory(pikascript/pikascript-core pikascript-core)
11-
aux_source_directory(pikascript/pikascript-lib/PikaStdLib PikaStdLib)
12-
aux_source_directory(pikascript/pikascript-api pikascript-api)
1311
include_directories(pikascript/pikascript-core)
1412
include_directories(pikascript/pikascript-api)
1513
include_directories(pikascript/pikascript-lib/PikaStdLib)
@@ -19,8 +17,6 @@ link_directories(libpikabinder)
1917

2018
add_executable(pikaByteCodeGen main.c
2119
${pikascript-core}
22-
${PikaStdLib}
23-
${pikascript-api}
2420
)
2521

2622
OPTION(CROSS_COMPILE "cross-compile" OFF)

tools/pikapython-c/pikapython-linux/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ cmake_minimum_required(VERSION 3.10)
22

33
project(pikapython)
44

5+
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE)
6+
57
# 添加components文件夹中所有的.c文件及其子文件夹路径
68
file(GLOB_RECURSE COMPONENTS_FILES pikapython/*.c)
79

@@ -19,6 +21,7 @@ include_directories(${CMAKE_SOURCE_DIR}/pikapython/pikascript-api)
1921

2022
# 设置输出目标
2123
add_executable(pikapython ${SOURCE_FILES})
24+
target_compile_definitions(pikapython PRIVATE PIKA_EVENT_ENABLE=0)
2225

2326
# 添加pthread库链接
2427
find_package(Threads REQUIRED)
Lines changed: 48 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -1,150 +1,48 @@
1-
import select_kth
2-
3-
# Python baseline function that works in PikaPython environment
4-
def py_select_kth(arr, k):
5-
"""Baseline implementation using simple sorting"""
6-
if len(arr) == 0:
7-
return None
8-
if k < 0 or k >= len(arr):
9-
return None
10-
11-
# Manual sorting since PikaPython may not support sorted()
12-
sorted_arr = []
13-
for i in range(len(arr)):
14-
sorted_arr.append(arr[i])
15-
16-
# Simple bubble sort (not efficient but works in restricted environment)
17-
for i in range(len(sorted_arr)):
18-
for j in range(i + 1, len(sorted_arr)):
19-
if sorted_arr[i] > sorted_arr[j]:
20-
temp = sorted_arr[i]
21-
sorted_arr[i] = sorted_arr[j]
22-
sorted_arr[j] = temp
23-
24-
return sorted_arr[k]
25-
26-
# Test with first dataset
27-
data1 = [3, 1, 5, 9, 2]
28-
print("[EXAMPLE] Testing with data1:", data1)
29-
30-
# Test k=0 (smallest)
31-
k = 0
32-
py_result1 = py_select_kth(data1, k)
33-
c_result1 = select_kth.SelectKth().select_kth(data1, k)
34-
print("[EXAMPLE] k=", k, "Python:", py_result1, "C module:", c_result1)
35-
assert py_result1 == c_result1, "Test 1 failed: k=0"
36-
37-
# Test k=2 (median)
38-
k = 2
39-
py_result2 = py_select_kth(data1, k)
40-
c_result2 = select_kth.SelectKth().select_kth(data1, k)
41-
print("[EXAMPLE] k=", k, "Python:", py_result2, "C module:", c_result2)
42-
assert py_result2 == c_result2, "Test 2 failed: k=2"
43-
44-
# Test k=4 (largest)
45-
k = 4
46-
py_result3 = py_select_kth(data1, k)
47-
c_result3 = select_kth.SelectKth().select_kth(data1, k)
48-
print("[EXAMPLE] k=", k, "Python:", py_result3, "C module:", c_result3)
49-
assert py_result3 == c_result3, "Test 3 failed: k=4"
50-
51-
# Test with second dataset
52-
data2 = [10, 20, 30, 5, 15]
53-
print("[EXAMPLE] Testing with data2:", data2)
54-
55-
# Test k=0 (smallest)
56-
k = 0
57-
py_result4 = py_select_kth(data2, k)
58-
c_result4 = select_kth.SelectKth().select_kth(data2, k)
59-
print("[EXAMPLE] k=", k, "Python:", py_result4, "C module:", c_result4)
60-
assert py_result4 == c_result4, "Test 4 failed: k=0"
61-
62-
# Test k=2 (median)
63-
k = 2
64-
py_result5 = py_select_kth(data2, k)
65-
c_result5 = select_kth.SelectKth().select_kth(data2, k)
66-
print("[EXAMPLE] k=", k, "Python:", py_result5, "C module:", c_result5)
67-
assert py_result5 == c_result5, "Test 5 failed: k=2"
68-
69-
# Test k=4 (largest)
70-
k = 4
71-
py_result6 = py_select_kth(data2, k)
72-
c_result6 = select_kth.SelectKth().select_kth(data2, k)
73-
print("[EXAMPLE] k=", k, "Python:", py_result6, "C module:", c_result6)
74-
assert py_result6 == c_result6, "Test 6 failed: k=4"
75-
76-
# Test edge cases
77-
print("[EXAMPLE] Testing edge cases")
78-
79-
# Empty list
80-
empty_list = []
81-
py_empty = py_select_kth(empty_list, 0)
82-
c_empty = select_kth.SelectKth().select_kth(empty_list, 0)
83-
print("[EXAMPLE] Empty list - Python:", py_empty, "C module:", c_empty)
84-
assert py_empty is None, "Empty list Python should return None"
85-
assert c_empty is None, "Empty list C module should return None"
86-
87-
# k out of range
88-
py_out_of_range = py_select_kth(data1, 10)
89-
c_out_of_range = select_kth.SelectKth().select_kth(data1, 10)
90-
print("[EXAMPLE] k out of range - Python:", py_out_of_range, "C module:", c_out_of_range)
91-
assert py_out_of_range is None, "Out of range Python should return None"
92-
assert c_out_of_range is None, "Out of range C module should return None"
93-
94-
# Single element list
95-
single = [42]
96-
py_single = py_select_kth(single, 0)
97-
c_single = select_kth.SelectKth().select_kth(single, 0)
98-
print("[EXAMPLE] Single element - Python:", py_single, "C module:", c_single)
99-
assert py_single == c_single, "Single element test failed"
100-
101-
# Duplicate elements
102-
duplicates = [5, 5, 5, 5, 5]
103-
py_dup = py_select_kth(duplicates, 2)
104-
c_dup = select_kth.SelectKth().select_kth(duplicates, 2)
105-
print("[EXAMPLE] Duplicates k=2 - Python:", py_dup, "C module:", c_dup)
106-
assert py_dup == c_dup, "Duplicates test failed"
107-
108-
print("[EXAMPLE] All functional tests passed!")
109-
110-
# Performance test (only if functional tests pass)
111-
print("[EXAMPLE] Starting performance tests...")
112-
113-
import time
114-
115-
# Larger dataset for performance testing
116-
large_data = []
117-
for i in range(100):
118-
large_data.append(100 - i) # Reverse sorted to make it challenging
119-
120-
ITER = 1000
121-
122-
# Time Python baseline
123-
py_start = time.time()
124-
for _ in range(ITER):
125-
result_py = py_select_kth(large_data, 50)
126-
py_end = time.time()
127-
py_total = py_end - py_start
128-
py_mean = py_total / ITER
129-
130-
print("[PERF] python_total:", py_total, "seconds")
131-
print("[PERF] python_mean:", py_mean, "seconds per call")
132-
133-
# Time C module
134-
c_start = time.time()
135-
for _ in range(ITER):
136-
result_c = select_kth.SelectKth().select_kth(large_data, 50)
137-
c_end = time.time()
138-
c_total = c_end - c_start
139-
c_mean = c_total / ITER
140-
141-
print("[PERF] cmod_total:", c_total, "seconds")
142-
print("[PERF] cmod_mean:", c_mean, "seconds per call")
143-
144-
speedup = py_mean / c_mean if c_mean > 0 else float('inf')
145-
print("[PERF] speedup:", speedup, "x")
146-
147-
# Verify performance test results match
148-
assert result_py == result_c, "Performance test results mismatch"
149-
150-
print("[EXAMPLE][SELFTEST] OK - All tests passed successfully!")
1+
def bench_arith(n):
2+
i = 0
3+
acc = 0
4+
while i < n:
5+
acc = (acc + i * 3 + 7) % 1000003
6+
i = i + 1
7+
return acc
8+
9+
10+
def fib_iter(n):
11+
i = 0
12+
a = 0
13+
b = 1
14+
while i < n:
15+
c = a + b
16+
a = b
17+
b = c
18+
i = i + 1
19+
return a
20+
21+
22+
def bench_calls(n):
23+
i = 0
24+
acc = 0
25+
while i < n:
26+
acc = acc + fib_iter(8)
27+
i = i + 1
28+
return acc
29+
30+
31+
def bench_list(n):
32+
data = []
33+
i = 0
34+
while i < n:
35+
data.append((i * 7) % 1009)
36+
i = i + 1
37+
i = 0
38+
acc = 0
39+
while i < n:
40+
acc = (acc + data[i]) % 1000003
41+
i = i + 1
42+
return acc
43+
44+
45+
print("bench:v1")
46+
print("arith", bench_arith(300000))
47+
print("calls", bench_calls(60000))
48+
print("list", bench_list(20000))

0 commit comments

Comments
 (0)