Comments (3)
@WillPCowan it's a good idea for the custom handler, @carlosmiei how do you think we migrate this map struct into exchange base?
from ccxt.
For reference, I got ChatGPT to do a little test on this and there is a lot of variance, but putting the map into an instance variable is 2-5x faster:
With the adjusted parameters of 100,000 events and 100 trials, the average time taken for each event handler is:
• With event-to-handler map as an instance variable: 0.0179 seconds
• Without event-to-handler map as an instance variable (constructed in each call): 0.0525 seconds
import timeit
import random
# Define both classes
class EventHandlerWithMap:
def __init__(self):
self.event_to_handler = {
"event_1": self.handle_event_1,
"event_2": self.handle_event_2,
"event_3": self.handle_event_3
}
def handle_event(self, event):
handler = self.event_to_handler.get(event)
if handler:
handler()
else:
pass
def handle_event_1(self):
pass
def handle_event_2(self):
pass
def handle_event_3(self):
pass
class EventHandlerWithoutMap:
def __init__(self):
pass
def handle_event(self, event):
event_to_handler = {
"event_1": self.handle_event_1,
"event_2": self.handle_event_2,
"event_3": self.handle_event_3
}
handler = event_to_handler.get(event)
if handler:
handler()
else:
pass
def handle_event_1(self):
pass
def handle_event_2(self):
pass
def handle_event_3(self):
pass
# Prepare random events
num_events = 100000
events = ["event_1", "event_2", "event_3"]
random_events = [random.choice(events) for _ in range(num_events)]
# Define the test functions
def test_with_map():
handler = EventHandlerWithMap()
for event in random_events:
handler.handle_event(event)
def test_without_map():
handler = EventHandlerWithoutMap()
for event in random_events:
handler.handle_event(event)
# Measure the time using timeit
num_trials = 100
time_with_map = timeit.timeit(test_with_map, number=num_trials)
time_without_map = timeit.timeit(test_without_map, number=num_trials)
avg_time_with_map = time_with_map / num_trials
avg_time_without_map = time_without_map / num_trials
avg_time_with_map, avg_time_without_map
from ccxt.
if there is a performance gain, then it would be a good move.
from ccxt.
Related Issues (20)
- Biance USA, error on startup HOT 5
- Coinbase watch_order_book not returning the correct one if there are multiple streams running HOT 6
- Memory Leaks in PHP HOT 12
- Docs: clarify difference between coinbase and coinbaseexchange
- fetch_ohlcv() on Binance returns 18 candles instead of 1000 HOT 3
- Document how to instantiate exchanges HOT 1
- Docs: sort class methods somehow
- Note at the top of each generated file that it's generated and what the source is, so contributors don't waste time editing them HOT 1
- Isn't Mexc open for use? HOT 3
- I need to know how to resolve KeyError: 'broad'. HOT 2
- MEXC returns BSC instead of BEP20 as a network indicator. HOT 2
- Close A Position In Phemex HOT 4
- OKX Invalid API key HOT 1
- Why does market order on MEXC requires price to be specified? HOT 8
- Networks inconsistencies HOT 5
- Bitmart : FetchTickers does not return the same result as FetchTicker HOT 1
- Pagination Cursor breaks when receiving multiple entries with the same timestamp, leading to gaps in data HOT 6
- okx create_order_ws doesn't support multiple calls in rapid succession HOT 5
- HTX watchTicker does not return bid and ask HOT 2
- ByBit: parseToNumeric in parseLedgerEntry throws Error for 'FEE_REFUND' ledger entries HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ccxt.