-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
65 lines (49 loc) · 1.98 KB
/
test.py
File metadata and controls
65 lines (49 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import ctypes
import random
import time
# Load the C library
c_lib = ctypes.CDLL('./libquicksort.so')
# C function prototypes
c_lib.quicksort.argtypes = [ctypes.POINTER(ctypes.c_int), ctypes.c_int, ctypes.c_int]
# Load the Rust library // Depending on the system built on this file name might change.
rust_lib = ctypes.CDLL('./quicksort/target/release/libquicksort.so')
# Rust function prototype
rust_lib.quicksort.argtypes = [ctypes.POINTER(ctypes.c_int), ctypes.c_int]
def test_quicksort():
# Test with a random list of 100000000 integers
size = 100000000
arr = [random.randint(0, 10000) for _ in range(size)]
# C QuickSort
c_arr = (ctypes.c_int * size)(*arr)
start_time = time.time()
c_lib.quicksort(c_arr, 0, size - 1)
c_time = time.time() - start_time
print(f"C QuickSort completed in {c_time:.6f} seconds")
# Rust QuickSort
rust_arr = (ctypes.c_int * size)(*arr)
start_time = time.time()
rust_lib.quicksort(rust_arr, size)
rust_time = time.time() - start_time
print(f"Rust QuickSort completed in {rust_time:.6f} seconds")
# C QuickSort
c_arr = (ctypes.c_int * size)(*arr)
start_time = time.time()
c_lib.quicksort(c_arr, 0, size - 1)
c_time = time.time() - start_time
print(f"C QuickSort completed in {c_time:.6f} seconds")
# Rust QuickSort
rust_arr = (ctypes.c_int * size)(*arr)
start_time = time.time()
rust_lib.quicksort(rust_arr, size)
rust_time = time.time() - start_time
print(f"Rust QuickSort completed in {rust_time:.6f} seconds")
# Check if both results are the same
start_time = time.time()
pythonsorttime = sorted(arr)
python_time = time.time() - start_time
print(f"Python sort completed in {python_time:.6f} seconds")
assert list(c_arr) == sorted(arr), "C QuickSort failed!"
assert list(rust_arr) == sorted(arr), "Rust QuickSort failed!"
print("Both QuickSort implementations worked correctly!")
if __name__ == '__main__':
test_quicksort()