Holycrabbe Holycrabbe - 2 years ago 177
C++ Question

C++ second counter

I have created a function which counts seconds, after the choice of a user. It all works, but can it be done smarter and more efficient? Because it seems very heavy and slow. Is there a lib that solves this problem? Or how can we get around it?

Here is my code:

#include <ctime>
#include <iomanip>
#include <iostream>

using namespace std;

int main() {
double a,c, x,b;

int nutid=0;

cout<<"Please enter a number: ";
x = time(0);
c = a-1;

while (true) {
if (!cin) {
cout<<"... Error";
else {

nutid = b+c;

return 0;

Answer Source

You can measure time using the library <chrono> (since c++11)


#include <iostream>
#include <chrono>
using namespace std;
using namespace chrono;

int main() {
    auto start = high_resolution_clock::now();

    // your code here

    auto end = high_resolution_clock::now();
    // you can also use 'chrono::microseconds' etc.
    cout << duration_cast<seconds>(end - start).count() << '\n';
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download