Skip to content

Instantly share code, notes, and snippets.

@juniorcesarabreu
Last active April 13, 2017 13:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juniorcesarabreu/69f7ed91150bebe745be7b35252369b1 to your computer and use it in GitHub Desktop.
Save juniorcesarabreu/69f7ed91150bebe745be7b35252369b1 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <chrono>
#include <ctime>
 
long fibonacci(unsigned n)
{
    if (n < 2) return n;
    return fibonacci(n-1) + fibonacci(n-2);
}
 
int main()
{
    std::chrono::time_point<std::chrono::system_clock> start, end;
    start = std::chrono::system_clock::now();
    std::cout << "f(42) = " << fibonacci(42) << '\n';
    end = std::chrono::system_clock::now();
 
    std::chrono::duration<double> elapsed_seconds = end-start;
    std::time_t end_time = std::chrono::system_clock::to_time_t(end);
 
    std::cout << "finished computation at " << std::ctime(&end_time)
              << "elapsed time: " << elapsed_seconds.count() << "s\n";
}

Possible output:

f(42) = 267914296
finished computation at Mon Jul 29 08:41:09 2013
elapsed time: 0.670427s

Fonte

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment