LUCIT-Systems-and-Development / unicorn-binance-websocket-api

A Python SDK by LUCIT to use the Binance Websocket API`s (com+testnet, com-margin+testnet, com-isolated_margin+testnet, com-futures+testnet, com-coin_futures, us, tr, dex/chain+testnet) in a simple, fast, flexible, robust and fully-featured way.
https://unicorn-binance-websocket-api.docs.lucit.tech/
Other
683 stars 164 forks source link

get_stream_buffer_length() #12

Closed DaWe35 closed 5 years ago

DaWe35 commented 5 years ago

I needed a function that returns the number of items in buffer. If you find it useful, feel free to pull :) (And the performance of get_stream_buffer_length() is better than len(self.stream_buffer), but I don't think it matters very much.)

oliver-zehentleitner commented 5 years ago

I see the point of the improvment! Thank you very much!

DaWe35 commented 5 years ago

5 commit: pop() changed to pop(0), I think it was a bug

oliver-zehentleitner commented 5 years ago

I think your first suggestion is correct! len() is counting all elements of the list every time its used. If we the use append and pop methods to increase or decrease with 1, then for sure its faster to use self.stream_buffer_length instead of len(array).

if someone has the reason to control every second the stream_buffer size, then your first solution is definitely the best way ...

5 commit: pop() changed to pop(0), I think it was a bug

Yes, it should be a FIFO stack, thanks!

DaWe35 commented 5 years ago

Hmm. I don't know. It would be logical, but I think python already does that. There is a conversion about len(): https://stackoverflow.com/questions/699177/python-do-python-lists-keep-a-count-for-len-or-does-it-count-for-each-call If you decide to use self.stream_buffer_length instead of len(array), just ignore #6 (a16e9aa)commit.

oliver-zehentleitner commented 5 years ago

i am thinking about it ... on the other hand, the stream_buffer should be almost 0 all the time, so its a bigger waste to count hundreds of +/- 1 operations per second, if you dont need them.

DaWe35 commented 5 years ago

True. And what is with sys.getsizeof? :)

DaWe35 commented 5 years ago

I can't update the documentation (https://www.unicorn-data.com/unicorn-binance-websocket-api.html), so if you want, you need to add get_stream_buffer_length()

oliver-zehentleitner commented 5 years ago

True. And what is with sys.getsizeof? :)

Its because, if I count the size this way, its compare able to the self.total_received_bytes value. But if i use the size of the stream_buffer variable itself, its very different. not only the raw data is used in the size, also the metadata for the complete list object of the stream_buffer and we receive a json and convert it! But at least its not accurate information, we should switch to use the data from the stream_buffer itself.

I can't update the documentation (https://www.unicorn-data.com/unicorn-binance-websocket-api.html), so if you want, you need to add get_stream_buffer_length()

Thanks, I will do it asap!

oliver-zehentleitner commented 5 years ago

Its done, changes are available in the new release: https://github.com/unicorn-data-analysis/unicorn-binance-websocket-api/blob/master/CHANGELOG.md#138

DaWe35 commented 5 years ago

Cooool!