Reputation: 160
I have program that is written in python3.6
and now I wanted to re-write it in c++
to make it faster. Before doing so, I decided to make a small test to get a inference on how much speed will I gain. The program makes a lot of http request so I decided to test for http request speed. But I was really surprised by the results because python was faster c++ in each request by the average of ~50ms
(sometimes ~100ms
and sometimes ~10ms
) and I didn't understand why.
here is my c++ code:
#include "swish/swish.h"
#include <chrono>
int main() {
swish::Client httpclient = swish::Client();
std::chrono::milliseconds since = std::chrono::duration_cast< std::chrono::milliseconds >(std::chrono::system_clock::now().time_since_epoch());
std::pair<swish::Response<swish::ResponseHeaderBuffer>, swish::StatusCode> resp = httpclient.Get("https://api.btcturk.com/api/v2/ticker");
std::chrono::milliseconds now = std::chrono::duration_cast< std::chrono::milliseconds >(std::chrono::system_clock::now().time_since_epoch());
std::cout << now.count() - since.count() << std::endl;
return 0;
}
I am using this swish http client
library(which is just curl wrapper. Here is the library repo) to make the request and compiling it with the command clang++ testcpp.cpp -std=c++17 -o test -lcurl -O3
.
And here is my Python code:
import time
import requests
current_milli_time = lambda: int(round(time.time() * 1000))
if __name__ == "__main__":
since = current_milli_time()
res = requests.get('https://api.btcturk.com/api/v2/ticker')
now = current_milli_time()
print(now - since)
Is it because most of those milliseconds because of waiting for response so it is completely network related? but python is faster each time. I tried 20~30 times with different interval. Or is it because python code for this library is really optimized??
Upvotes: 2
Views: 3730
Reputation: 2554
The difference may be because Python has a very optimized way of page requesting, and the swish
lib/code you are using hasn't.
The network time may be the same (in fact, it should be the same), but you really can't know (unless you developed swish
lib) what that library is doing.
Asking for a web page in c++
can be done in several ways and no one can be compared unless you write the code for both.
I would say that your method (calling swish
) is not optimal (but is useful in all cases) and Python is designed for optimal "request and wait response."
So in this case, Python has the advantage of having thousands users reporting feedback that allows developers to make it optimized.
Swish may not have so many users, may don't have so much feedback and it isn't so optimized as Python (which calls c
after all.)
If someone has doubts, there are methods to speed up things like TCP_NODELAY that can make a difference. Or just the timeouts in the select
call.
When talking about network the language is not the bottleneck, it is the network. So if you aren't satisfied with your lib performance you should change the library not the language.
Upvotes: 5