binance-exchange / binacpp

Binance C++ library
MIT License
212 stars 108 forks source link

REST requests extremely slow #18

Closed alexanderkoumis closed 6 years ago

alexanderkoumis commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

Pull request merged