How to make std chrono output in milliseconds or nanoseconds? [duplicate] - c++

This question already has answers here:
How to get duration, as int milli's and float seconds from <chrono>?
(5 answers)
Closed 13 days ago.
#include <iostream>
#include <chrono>
using namespace std;
class Time
{
chrono::steady_clock::time_point start;
public:
Time()
{
start = chrono::steady_clock::now();
}
~Time()
{
auto end = chrono::steady_clock::now();
std::chrono::duration<double> time = end-start;
cout << "time " << time.count() << endl;
}
};
void revstr(char* input)
{
int length = strlen(input);
int last_pos = length-1;
for(int i = 0; i < length/2; i++)
{
char tmp = input[i];
input[i] = input[last_pos - i];
input[last_pos - i] = tmp;
}
}
int main()
{
Time t;
char str[] = "abcd";
cout << str << endl;
revstr(str);
cout << str << endl;
}
The above outputs:
time 7.708e-06
How should we turn that in milliseconds, nanoseconds or seconds?
I tried (replace std::chrono::duration<double> time = end-start;)
std::chrono::duration<std::chrono::nanoseconds> time = end-start;
But it says:
A duration representation cannot be a duration.

Use std::chrono::duration_cast():
#include <chrono>
#include <iostream>
int main()
{
std::chrono::duration<double> time{7.708e-06};
auto nanos = std::chrono::duration_cast<std::chrono::nanoseconds>(time);
std::cout << nanos.count() << '\n';
}

Related

How people used to measure time before chrono?

Since C++11 we can measure time as in https://en.cppreference.com/w/cpp/chrono
#include <iostream>
#include <chrono>
long fibonacci(unsigned n)
{
if (n < 2) return n;
return fibonacci(n-1) + fibonacci(n-2);
}
int main()
{
auto start = std::chrono::steady_clock::now();
std::cout << "f(42) = " << fibonacci(42) << '\n';
auto end = std::chrono::steady_clock::now();
std::chrono::duration<double> elapsed_seconds = end-start;
std::cout << "elapsed time: " << elapsed_seconds.count() << "s\n";
}
How people used to measure time before chrono? Was there a C++ way to do it or people used to reply on OS specific facilities?

Why does a function has so different performance timing?

I'm learning C++. While solving Leetcode 0009, I wanted to time the function. For 4 different inputs I got the following results consistently:
1
time: 8993
0
time: 88
0
time: 1374
1
time: 1199
My question is why is it taking so long for the first run but not the others? And any other feedback is appreciated.
Here is the code:
//leetcode 0009. Palindrome Number
#include <iostream>
#include <sstream>
#include <string>
#include <chrono>
bool isPalindrome(int x) {
if (x < 0) return false;
std::stringstream ss;
ss << x;
std::string str;
ss >> str;
int len = str.length();
for (int i=0; i<len/2; i++) {
if (str[i] != str[len-i-1]) {
return false;
}
}
return true;
}
void timeit (bool (*f)(int), int x) {
auto s = std::chrono::high_resolution_clock::now();
auto res = f(x);
auto e = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(e - s);
std::cout << res << std::endl;
std::cout << "time: " << duration.count() << std::endl; //true
}
int main() {
timeit(isPalindrome, 121);
timeit(isPalindrome, -121);
timeit(isPalindrome, 10);
timeit(isPalindrome, 1001);
}

Cannot call front() on queue in C++

I am attemtping to access and pop the front element from a queue in C++, but the line
tuple<int, string, future<string>> post = posterVerificationQueue.front();
is underlined in red and gives the error:
function "std::tuple<_This, _Rest...>::tuple(const std::tuple<_This,
_Rest...> &) [with _This=int, _Rest=<std::string, std::futurestd::string>]" (declared at line 320 of "C:\Program Files
(x86)\Microsoft Visual
Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\tuple") cannot
be referenced -- it is a deleted function
I imagine it is an issue with me using a future within a tuple, but not too sure. Here is my code:
#include <iostream>
#include <ctime>
#include <chrono>
#include <thread>
#include <future>
#include <queue>
#include <mutex>
#include <tuple>
#include <map>
#include "TCPClient.h"
#include "ThreadPool.h"
#include "RequestGenerator.h"
#include "ResponseVerifier.h"
#include "Storage.h"
#define DEFAULT_PORT 12345
void readRequest(string serverIp, int threadIndex, double timeDurationSecs);
void postRequest(string serverIp, int threadIndex, double timeDurationSecs);
int readRequests = 0;
int postRequests = 0;
mutex mLock;
map<int, tuple<double, int>> posterThreadMap;
queue<tuple<int, string, future<string>>> posterVerificationQueue;
map<int, tuple<double, int>> readerThreadMap;
Storage* db = new Storage();
RequestGenerator* requestGenerator = new RequestGenerator();
int main(int argc, char **argv)
{
// Default parameters
unsigned int posterCount = 5;
unsigned int readerCount = 0;
double timeDurationSecs = 10;
bool throttle = false;
string serverIp = "127.0.0.1";
// Validate the parameters
if (argc != 6) {
std::cout << "\nUsage (required parameters): server_IP number_of_poster_threads number_of_reader_threads time_duration throttle(0|1)\n\n";
std::cout << "server_IP - IP of the server\n";
std::cout << "number_of_poster_threads - number of threads performing POST operations\n";
std::cout << "number_of_reader_threads - number of threads performing READ operations\n";
std::cout << "time_duration - duration of test execution in seconds\n";
std::cout << "throttle(0|1) - 0: do not throttle message speed\n";
std::cout << "\t\t1: throttle message speed\n\n";
std::cout << "\nDefault Parameters:\n";
std::cout << "\tserver_IP - " << serverIp << "\n";
std::cout << "\tnumber_of_poster_threads - " << posterCount << "\n";
std::cout << "\tnumber_of_reader_threads - " << readerCount << "\n";
std::cout << "\ttime_duration - " << timeDurationSecs << "s\n";
std::cout << "\tthrottle - " << (throttle ? "true" : "false") << "\n\n";
std::cout << "Enter dev mode using default paramaters?\n";
system("pause");
}
else
{
serverIp = argv[1];
posterCount = (int)argv[2];
readerCount = (int)argv[3];
timeDurationSecs = (int)argv[4];
throttle = (int)argv[5];
}
cout << "\nStarting throughput test...\n";
ThreadPool posterPool(posterCount);
vector<future<void>> posterFutures;
vector<tuple<string, int, int>> incorrectPostResponses;
double posterTotalTime = 0.0; // The total time in seconds that all poster threads took to run
ThreadPool readerPool(readerCount);
vector<future<void>> readerFutures;
double readerTotalTime = 0.0; // The total time in seconds that all reader threads took to run
for (int i = 0; i < posterCount; i++)
posterFutures.push_back(posterPool.enqueue(postRequest, serverIp, i, timeDurationSecs));
for (int i = 0; i < readerCount; i++)
readerFutures.push_back(readerPool.enqueue(readRequest, serverIp, i, timeDurationSecs));
for (int i = 0; i < posterFutures.size(); i++)
posterFutures[i].wait();
for (int i = 0; i < readerFutures.size(); i++)
readerFutures[i].wait();
for (int i = 0; i < posterThreadMap.size(); i++)
{
double posterRequestsPerSecond = get<1>(posterThreadMap[i]);
double threadRunTime = get<0>(posterThreadMap[i]);
posterTotalTime += threadRunTime;
std::cout << "\nPoster thread " << i << " (ran for " << threadRunTime << "s) - Average post requests per second: " << posterRequestsPerSecond << "\n";
}
// Should verification be optional?
while (!posterVerificationQueue.empty())
{
tuple<int, string, future<string>> post = posterVerificationQueue.front();
posterVerificationQueue.pop();
int postIndex = get<0>(post);
string request = get<1>(post);
string response = get<2>(post).get();
tuple<bool, int, int> postVerification = db->addPosterValue(postIndex, request, response);
bool isValid = get<0>(postVerification);
if (!isValid)
{
int correctResponse = get<1>(postVerification);
int actualResponse = get<2>(postVerification);
incorrectPostResponses.push_back(make_tuple(request, correctResponse, actualResponse));
}
}
cout << "\nTotal poster runtime: " << posterTotalTime << "s" << "\n";
cout << "\nTotal post requests: " << postRequests << "\n";
cout << "\nAverage post requests per second per thread: " << postRequests / posterTotalTime << "\n";
cout << "\nIncorrect responses: " << incorrectPostResponses.size() << "\n";
for (int i = 0; i < incorrectPostResponses.size(); i++)
{
tuple<string, int, int> incorrectResponse = incorrectPostResponses[i];
string request = get<0>(incorrectResponse);
int correctResponse = get<1>(incorrectResponse);
int actualResponse = get<2>(incorrectResponse);
cout << "Incorrect response #" << i + 1 << "\n";
cout << "Request: " << request << "\n";
cout << "Expected response: " << correctResponse << "\n";
cout << "Actual response: " << actualResponse << "\n\n";
}
// TODO: Implement the block above for reader threads
delete db;
delete requestGenerator;
system("pause");
return 0;
}
string sendRequest(TCPClient client, string request)
{
return client.send(request);
}
void postRequest(string serverIp, int threadIndex, double timeDurationSecs)
{
TCPClient client(serverIp, DEFAULT_PORT);
client.OpenConnection();
int threadPostCount = 0;
double timeSpan;
chrono::high_resolution_clock::time_point endTime;
chrono::high_resolution_clock::time_point startTime = chrono::high_resolution_clock::now();
do
{
/*
Could limit with
if (!throttle || (throttle && threadPostCount < (timeDurationSecs * 1000)))
{}
*/
string request = requestGenerator->generateWriteRequest();
mLock.lock();
future<string> responseFut = async(launch::async, sendRequest, client, request);
posterVerificationQueue.push(make_tuple(postRequests, request, move(responseFut)));
postRequests++;
mLock.unlock();
threadPostCount++;
endTime = chrono::high_resolution_clock::now();
timeSpan = chrono::duration_cast<chrono::duration<double>>(endTime - startTime).count();
} while (timeSpan < timeDurationSecs);
double totalRunTime = (endTime - startTime).count();
double posterRequestsPerSecond = threadPostCount / timeSpan;
tuple<double, double> returnValues = make_tuple(timeSpan, posterRequestsPerSecond);
mLock.lock();
postRequests += threadPostCount;
posterThreadMap[threadIndex] = returnValues;
mLock.unlock();
client.CloseConnection();
}
void readRequest(string serverIp, int threadIndex, double timeDurationSecs)
{
//return 0.0;
}
std::tuple<_This, _Rest...>::tuple(const std::tuple<_This, _Rest...> &)
...
cannot be referenced -- it is a deleted function
Well, that's the tuple copy constructor, right?
I imagine it is an issue with me using a future within a tuple, but not too sure.
Let's check the documentation for the constructors of std::future ...
future( const future& other ) = delete;
std::future is not CopyConstructible.
So I'd say your imagination was correct. You can't copy the tuple, because it can't copy one of its members.
You can use std::move to move construct from the queue element, but remember it would be a problem if you didn't pop the front immediately after (in the code shown, you do this, so it's fine).

C++ program crashes when attempting to call get() on a future stored in a vector

I am currently working on a project that requires me to store a vector of future so I can measure the execution time accurately, however when I call
posterTotal += posterDurationFutures[i].get();
the program just crashes with no error message. Wrapping the line in a try/catch block also does not work, as it doesn't seem to be throwing an exception.
Here is my code (please bare in mind that this is a work in progress):
#include <iostream>
#include <ctime>
#include <chrono>
#include <thread>
#include <future>
#include <queue>
#include "TCPClient.h"
#include "ThreadPool.h"
#include "RequestGenerator.h"
#include "ResponseVerifier.h"
#include "Storage.h"
#define DEFAULT_PORT 12345
double readRequest(TCPClient client, int threadIndex, double timeDurationSecs);
double postRequest(TCPClient client, int threadIndex, double timeDurationSecs);
string sendViaClient(TCPClient client, string request);
unsigned int readRequests = 0;
unsigned int postRequests = 0;
Storage* db = new Storage();
ResponseVerifier* responseVerifier = new ResponseVerifier();
RequestGenerator* requestGenerator = new RequestGenerator();
int main(int argc, char **argv)
{
// Default parameters
unsigned int posterCount = 5;
unsigned int readerCount = 0;
double timeDurationSecs = 10;
bool throttle = false;
string serverIp = "127.0.0.1";
// Validate the parameters
if (argc != 6) {
std::cout << "\nUsage (required parameters): server_IP number_of_poster_threads number_of_reader_threads time_duration throttle(0|1)\n\n";
std::cout << "server_IP - IP of the server\n";
std::cout << "number_of_poster_threads - number of threads performing POST operations\n";
std::cout << "number_of_reader_threads - number of threads performing READ operations\n";
std::cout << "time_duration - duration of test execution in seconds\n";
std::cout << "throttle(0|1) - 0: do not throttle message speed\n";
std::cout << "\t\t1: throttle message speed\n\n";
std::cout << "\nDefault Parameters:\n";
std::cout << "\tserver_IP - " << serverIp << "\n";
std::cout << "\tnumber_of_poster_threads - " << posterCount << "\n";
std::cout << "\tnumber_of_reader_threads - " << readerCount << "\n";
std::cout << "\ttime_duration - " << timeDurationSecs << "s\n";
std::cout << "\tthrottle - " << (throttle ? "true" : "false") << "\n\n";
std::cout << "Enter dev mode using default parameters?\n";
system("pause");
}
else
{
serverIp = argv[1];
posterCount = (int)argv[2];
readerCount = (int)argv[3];
timeDurationSecs = (int)argv[4];
throttle = (int)argv[5];
}
//std::queue<future<bool>> posterVerificationQueue;
//std::queue<bool> readerVerificationQueue;
ThreadPool posterPool(posterCount);
//ThreadPool readerPool(readerCount);
//vector<thread> posterThreads;
//vector<thread> readerThreads;
vector<future<double>> posterDurationFutures;
TCPClient client(serverIp, DEFAULT_PORT);
client.OpenConnection();
chrono::high_resolution_clock::time_point endTime;
chrono::high_resolution_clock::time_point startTime = chrono::high_resolution_clock::now();
for (int i = 0; i < posterCount; i++)
{
//posterThreads.emplace_back(postRequest, client, i, timeDurationSecs);
//std::future<double> durationFut = std::async(std::launch::async, postRequest, client, i, timeDurationSecs);
//posterDurationFutures.push_back(async(launch::async, postRequest, client, i, timeDurationSecs));
posterDurationFutures.push_back(posterPool.enqueue(postRequest, client, i, timeDurationSecs));
}
double posterTotal = 0;
for (int i = 0; i < posterDurationFutures.size(); i++)
{
bool valid = posterDurationFutures[i].valid();
//posterDurationFutures[i].wait();
posterTotal += posterDurationFutures[i].get();
}
cout << posterTotal;
for (int i = 0; i < readerCount; i++)
{
//readerThreads.emplace_back();
}
client.CloseConnection();
delete db;
delete responseVerifier;
delete requestGenerator;
system("pause");
return 0;
}
inline string sendViaClient(TCPClient client, string request)
{
return client.send(request);
}
double postRequest(TCPClient client, int threadIndex, double timeDurationSecs)
{
int threadPostCount = 0;
chrono::high_resolution_clock::time_point endTime;
chrono::high_resolution_clock::time_point startTime = chrono::high_resolution_clock::now();
do
{
string request = requestGenerator->generateWriteRequest();
string response = client.send(request);
bool isValidResponse = db->addPosterValue(request, response);
endTime = chrono::high_resolution_clock::now();
postRequests++;
threadPostCount++;
} while (chrono::duration_cast<chrono::duration<double>>(endTime - startTime).count() < timeDurationSecs);
double totalRunTime = (endTime - startTime).count();
double posterRequestsPerSecond = threadPostCount / totalRunTime;
std::cout << "Thread: " << threadIndex << endl;
std::cout << "Average post requests per second: " << posterRequestsPerSecond << endl;
return totalRunTime;
}
double readRequest(TCPClient client, int threadIndex, double timeDurationSecs)
{
return 0.0;
}

Get time difference in milliseconds in c++

I'm trying to get the time difference between two time stamps. Thread is sleeping between the two time stamps. but when i get the difference, it doesn't give me the time which the thread was sleeping. My code is below.
#include <iostream>
#include <locale>
#include <sys/time.h>
#include <cstdlib>
#include <unistd.h>
using namespace std;
int timeInMilli();
int main()
{
timeval t;
timeval t2;
gettimeofday(&t, NULL);
gettimeofday(&t2, NULL);
std::string buf(20, '\0');
std::strftime(&buf[0], buf.size(), "%H:%M:%S:", localtime(&t.tv_sec));
std::string hr = buf.substr(0, 2);
std::string min = buf.substr(3, 2);
std::string sec = buf.substr(6, 2);
/*std::cout << hr << '\n';
std::cout << min << '\n';
std::cout << std::atoi(sec.c_str()) << '\n';*/
int a = timeInMilli();
usleep(10);
int b = timeInMilli();
cout << b-a << endl;
}
int timeInMilli()
{
timeval t;
gettimeofday(&t, NULL);
string buf(20, '\0');
strftime(&buf[0], buf.size(), "%H:%M:%S:", localtime(&t.tv_sec));
string str_hr = buf.substr(0, 2);
string str_min = buf.substr(3, 2);
string str_sec = buf.substr(6, 2);
int hr = atoi(str_hr.c_str());
int min = atoi(str_min.c_str());
int sec = atoi(str_sec.c_str());
int milli = t.tv_usec/1000;
/*cout << hr << endl;
cout << min << endl;
cout << sec << endl;
cout << milli << endl;*/
int timeInMilli = (((hr * 60) + min) * 60 + sec) * 1000 + milli;
return timeInMilli;
}
usleep(10);
means that you pause for 10 µsec and not 10 msec since usleep works with microseconds. try with
usleep(10000);