Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REST requests extremely slow #18

Closed
alexanderkoumis opened this issue Mar 8, 2018 · 2 comments
Closed

REST requests extremely slow #18

alexanderkoumis opened this issue Mar 8, 2018 · 2 comments

Comments

@alexanderkoumis
Copy link

alexanderkoumis commented Mar 8, 2018

This C++ API is slower than other Binance interfaces (python-binance) because of the poor performance of libcurl. I timed reading the ticker/allBookTicker endpoint from two small programs in both C++ (with this API) and python (using python-binance):

#include <chrono>
#include <cstdio>
#include <json/json.h>
#include "binacpp.h"

int main(int argc, char** argv) {
    std::string api_key = "";
    std::string api_secret = "";
    BinaCPP::init(api_key, api_secret);

    auto time_start = std::chrono::high_resolution_clock::now();

    Json::Value result;
    BinaCPP::get_allBookTickers( result );

    auto time_end = std::chrono::high_resolution_clock::now();
    auto time_elapsed = time_end - time_start

    double time = std::chrono::duration_cast<std::chrono::microseconds>(time_elapsed).count() / 1000000.0;
    printf("%f\n", time);

    return 0;
}
from time import time
from binance.client import Client

client = Client('', '')

time_start = time()
client.get_orderbook_tickers()
time_end = time()

print(time_end - time_start)

The Python example grabs the data almost always 3-4x faster than the C++ code, with the Python code running in about 200ms and the C++ version taking ~800ms. This line, calling out to the libcurl API, is the slowest part of the binacpp library function. Why is libcurl so much slower here than the Python requests package?

@alexanderkoumis
Copy link
Author

This is because the CURL object is initialized/cleaned up for every single request. I made a modification to only perform the initialization once and it performed on par with the Python version. Pull request coming soon.

alexanderkoumis added a commit to alexanderkoumis/binacpp that referenced this issue Mar 20, 2018
Only instantiate curl object during BinaCPP::init

Use faster/more robust std::string.append function in curl callback

Fixes issue binance-exchange#18
@alexanderkoumis
Copy link
Author

Pull request merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant