0

I am using visual studio 2013 and i need to find out the execution time of my code(C++). Is there anyway that make me do that ?

codex
  • 60
  • 1
  • 1
  • 7
  • 5
    Lock your XBox away until you've completed the task? (Questions about motivational psychology are generally off-topic here.) – Kerrek SB Aug 09 '15 at 14:14
  • You can define a timer in terms of the std::chrono facilities. Then just measure. If this is for Windows only you can do it more easily using Windows API function `GetTickCount`. – Cheers and hth. - Alf Aug 09 '15 at 14:16
  • Or you can download the source of [wget](http://ftp.gnu.org/gnu/wget/) and look for a file `ptime.c`. There are some useful functions fore cross-platform time measuring. – ForceBru Aug 09 '15 at 14:17
  • http://en.cppreference.com/w/cpp/chrono/time_point – πάντα ῥεῖ Aug 09 '15 at 14:20
  • 1
    Rewrite it in Ruby, use a stopwatch and/or calendar. – Martin James Aug 09 '15 at 14:28
  • @kerrekSB it have nothing to do with motivational psychology am trying different techniques to help make execution time faster. you either post something useful or don't post at all. – codex Aug 09 '15 at 14:29
  • OK then, this is useful: when you say 'execution time', do you mean just code execution or do you include time spent blocked on I/O, inter-thread synchro and sleeping?. – Martin James Aug 09 '15 at 14:55
  • 1
    Why is this down-voted? It's a serious question asking about 'execution time', I'm confused :C – Vishwa Iyer Aug 09 '15 at 14:59
  • execution time means the time of code execution. it's written up there it can't be made any clearer. please read the question carefully. – codex Aug 09 '15 at 15:24

3 Answers3

2

This maybe one of the possible answer :

For Visual Studio : go to Tools / Options / Projects and Solutions / VC++ Project Settings and set Build Timing option to 'yes'. After that the time of every build will be displayed in the Output window.

For C

    #include <time.h>
    int main(void) 
    {
       clock_t tStart = clock();
       /* Do your stuff here */
       printf("Time taken: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
       return 0;
    }

For C++

For C++11

Community
  • 1
  • 1
udit043
  • 1,552
  • 3
  • 19
  • 35
1

Try to use function clock() from <time.h>:

#include <time.h>
#include <iostream>
int main()
{
    clock_t clkStart;
    clock_t clkFinish;

    clkStart = clock();
    for(int i = 0; i < 10000000; i++)
        ;
    //other code
    clkFinish = clock();
    std::cout << clkFinish - clkStart;

    system("pause");
    return 0;
}
Konstantin Dedov
  • 427
  • 2
  • 10
0

I use something like this:

#include <chrono>
#include <thread>
#include <iostream>

class Timer
{
    // make things readable
    using clk = std::chrono::steady_clock;

    clk::time_point b; // begin
    clk::time_point e; // end

public:
    void clear() { b = e = clk::now(); }
    void start() { b = clk::now(); }
    void stop() { e = clk::now(); }

    friend std::ostream& operator<<(std::ostream& o, const Timer& timer)
    {
        return o << timer.secs();
    }

    // return time difference in seconds
    double secs() const
    {
        if(e <= b)
            return 0.0;
        auto d = std::chrono::duration_cast<std::chrono::microseconds>(e - b);
        return d.count() / 1000000.0;
    }
};

int main()
{
    Timer timer;

    timer.start();

    // do your stuff...
    for(int i = 0; i < 1000; ++i)
        std::this_thread::sleep_for(std::chrono::milliseconds(10));

    timer.stop();

    std::cout << "time: " << timer << " secs" << '\n';
}
Galik
  • 42,526
  • 3
  • 76
  • 100