Difficult-Rocket/try/c/dll_load.py

87 lines
2.0 KiB
Python
Raw Normal View History

2022-04-30 13:56:57 +08:00
# -------------------------------
# Difficult Rocket
# Copyright © 2021-2022 by shenjackyuanjie
# All rights reserved
# -------------------------------
2022-05-11 11:11:39 +08:00
import os
import sys
import time
import psutil
2022-04-30 13:56:57 +08:00
import ctypes
2022-05-11 11:11:39 +08:00
import random
import cProfile
2022-04-30 13:56:57 +08:00
2022-05-03 13:59:15 +08:00
2022-05-11 11:11:39 +08:00
class PartData_C(ctypes.Structure):
_fields_ = [("x", ctypes.c_double),
("y", ctypes.c_double),
("vx", ctypes.c_double),
("vy", ctypes.c_double)]
2022-05-03 13:59:15 +08:00
2022-05-11 11:11:39 +08:00
class PartData_Py:
def __init__(self, x, y, vx, vy):
self.x = x
self.y = y
self.vx = vx
self.vy = vy
2022-05-03 13:59:15 +08:00
2022-05-11 11:11:39 +08:00
def count_time(func):
def int_time():
start_time = time.perf_counter()
func()
over_time = time.perf_counter()
total_time = over_time - start_time
print("程序运行了%s" % total_time)
return int_time
test_times = 1000
def count_info(func):
def float_info():
pid = os.getpid()
p = psutil.Process(pid)
info_start = p.memory_full_info().uss / 1024
func()
info_end = p.memory_full_info().uss / 1024
print("程序占用了内存" + str(info_end - info_start) + "KB")
print(info_start, info_end)
return float_info
@count_time
@count_info
def c_class_test():
class_list = [PartData_C(x=random.random(), y=random.random(), vx=random.random(), vy=random.random()) for x in range(test_times)]
for x in class_list:
x.x = random.random()
x.y = random.random()
x.vx = random.random()
x.vy = random.random()
print(sys.getsizeof(class_list, 1), '\n')
@count_time
@count_info
def py_class_test():
class_list = [PartData_Py(x=random.random(), y=random.random(), vx=random.random(), vy=random.random()) for x in range(test_times)]
for x in class_list:
x.x = random.random()
x.y = random.random()
x.vx = random.random()
x.vy = random.random()
print(sys.getsizeof(class_list, 1), '\n')
# gc.disable()
cProfile.run('c_class_test()', sort='calls')
# time.sleep(1)
cProfile.run('py_class_test()', sort='calls')