Skip to content

Instantly share code, notes, and snippets.

@bobjansen
Last active March 10, 2016 17:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bobjansen/3fa8d539a44eb6b39e41 to your computer and use it in GitHub Desktop.
Save bobjansen/3fa8d539a44eb6b39e41 to your computer and use it in GitHub Desktop.
cpr Sessions, better response
// This is based on the example cpr project
/*
* My output was:
➜ build git:(master) ✗ ./example
elapsed time: 0.533153s
elapsed time: 0.537888s
elapsed time: 0.502068s
elapsed time: 0.122032s
elapsed time: 0.125355s
*/
#include <chrono>
#include <cpr/cpr.h>
#include <iostream>
void request()
{
auto start = std::chrono::system_clock::now();
auto response = cpr::Get(cpr::Url{"https://httpbin.org/get"});
auto end = std::chrono::system_clock::now();
printDuration(start, end);
//std::cout << response.text << std::endl;
}
void request2(cpr::Session& session)
{
auto start = std::chrono::system_clock::now();
auto response = session.Get();
auto end = std::chrono::system_clock::now();
printDuration(start, end);
//std::cout << response.text << std::endl;
}
int main(int argc, char** argv) {
// Without session mgmt.
request();
request();
cpr::Session session;
auto url = cpr::Url{"https://httpbin.org/get"};
session.SetUrl(url);
request2(session);
request2(session);
request2(session);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment