Код: Выделить всё
cdef class BookL1:
cdef readonly str exchange
cdef readonly str symbol
cdef readonly double bid
cdef readonly double ask
cdef readonly double bid_size
cdef readonly double ask_size
def __init__(self, str exchange, str symbol, double bid, double ask, double bid_size, double ask_size):
self.exchange = exchange
self.symbol = symbol
self.bid = bid
self.ask = ask
self.bid_size = bid_size
self.ask_size = ask_size
Код: Выделить всё
from dataclasses import dataclass
@dataclass(slots=True)
class BookL1:
exchange: str
symbol: str
bid: float
ask: float
bid_size: float
ask_size: float
Код: Выделить всё
from trade_types import BookL1 as CBookL1
from utils import BookL1
import timeit
def create_book_l1():
return BookL1("binance", "BTC/USDT", 30000.0, 30001.0, 1.5, 2.0)
def create_cbook_l1():
return CBookL1("binance", "BTC/USDT", 30000.0, 30001.0, 1.5, 2.0)
def access_book_l1(book):
return (book.exchange, book.symbol, book.bid, book.ask, book.bid_size, book.ask_size)
def run_benchmark(class_type, create_func, iterations=1000000):
creation_time = timeit.timeit(create_func, number=iterations)
instance = create_func()
access_time = timeit.timeit(lambda: access_book_l1(instance), number=iterations)
print(f"{class_type} Benchmark Results:")
print(f"Creation time: {creation_time:.6f} seconds")
print(f"Access time: {access_time:.6f} seconds")
print()
if __name__ == "__main__":
run_benchmark("Python BookL1", create_book_l1)
run_benchmark("Cython CBookL1", create_cbook_l1)
Код: Выделить всё
Python BookL1 Benchmark Results:
Creation time: 0.200142 seconds
Access time: 0.105723 seconds
Cython CBookL1 Benchmark Results:
Creation time: 0.064651 seconds
Access time: 0.169590 seconds
Подробнее здесь: https://stackoverflow.com/questions/790 ... -dataclass
Мобильная версия