Measuring time for the execution of functions










-1














I am stuck at measuring execution time for the functions in C++. I tried clock_t clock(void) but it measures just the time for the one function in the program. In the below simple example how can i measure time for the two functions?



#include <iostream>

using namespace std;

int add(int a, int b)

return a+b;


int subtract(int a, int b)

return a - b;


int main()

int a,b;
cin >> a >> b;

int c = add(a,b);
int d = subtract(a,b);
cout << c << " " << d << "n";
return 0;










share|improve this question



















  • 1




    Please read Why you should not #include <bits/stdc++.h>. Coupling that with using namespace std; is even worse.
    – StoryTeller
    Nov 11 at 9:18











  • Show what you have tried so far.
    – Swordfish
    Nov 11 at 9:22










  • See here: Easily measure elapsed time using <chrono>...
    – Ruks
    Nov 11 at 9:45







  • 2




    Welcome to Stack Overflow! Please take the tour and read How to Ask. Concerning your question, please also do at least a little bit of research. Just searching the web for something like "how to measure time in C++" should turn up hundreds of hit and at least some of them should do the job for you. Also, as mentioned, your problem description is vague. Your question could be paraphrased like "here's some code, I tried to modify it in some way I only hint at but it didn't fulfil my unclear expectations". That's unfortunately a bad question.
    – Ulrich Eckhardt
    Nov 11 at 9:46






  • 2




    Generally speaking, there is little point in trying to measure the time for a single function call that does a trivial operation like integer addition. There is too much variation in the times to make such measurement meaningful - do it a number of times, and the measurements will change. Instead, call the function some large number of times, to get a statistical measure (e.g. mean). Generally it will be necessary to obtain time before running the functions, obtain time after the functions are complete, and compute the difference.
    – Peter
    Nov 11 at 9:47















-1














I am stuck at measuring execution time for the functions in C++. I tried clock_t clock(void) but it measures just the time for the one function in the program. In the below simple example how can i measure time for the two functions?



#include <iostream>

using namespace std;

int add(int a, int b)

return a+b;


int subtract(int a, int b)

return a - b;


int main()

int a,b;
cin >> a >> b;

int c = add(a,b);
int d = subtract(a,b);
cout << c << " " << d << "n";
return 0;










share|improve this question



















  • 1




    Please read Why you should not #include <bits/stdc++.h>. Coupling that with using namespace std; is even worse.
    – StoryTeller
    Nov 11 at 9:18











  • Show what you have tried so far.
    – Swordfish
    Nov 11 at 9:22










  • See here: Easily measure elapsed time using <chrono>...
    – Ruks
    Nov 11 at 9:45







  • 2




    Welcome to Stack Overflow! Please take the tour and read How to Ask. Concerning your question, please also do at least a little bit of research. Just searching the web for something like "how to measure time in C++" should turn up hundreds of hit and at least some of them should do the job for you. Also, as mentioned, your problem description is vague. Your question could be paraphrased like "here's some code, I tried to modify it in some way I only hint at but it didn't fulfil my unclear expectations". That's unfortunately a bad question.
    – Ulrich Eckhardt
    Nov 11 at 9:46






  • 2




    Generally speaking, there is little point in trying to measure the time for a single function call that does a trivial operation like integer addition. There is too much variation in the times to make such measurement meaningful - do it a number of times, and the measurements will change. Instead, call the function some large number of times, to get a statistical measure (e.g. mean). Generally it will be necessary to obtain time before running the functions, obtain time after the functions are complete, and compute the difference.
    – Peter
    Nov 11 at 9:47













-1












-1








-1







I am stuck at measuring execution time for the functions in C++. I tried clock_t clock(void) but it measures just the time for the one function in the program. In the below simple example how can i measure time for the two functions?



#include <iostream>

using namespace std;

int add(int a, int b)

return a+b;


int subtract(int a, int b)

return a - b;


int main()

int a,b;
cin >> a >> b;

int c = add(a,b);
int d = subtract(a,b);
cout << c << " " << d << "n";
return 0;










share|improve this question















I am stuck at measuring execution time for the functions in C++. I tried clock_t clock(void) but it measures just the time for the one function in the program. In the below simple example how can i measure time for the two functions?



#include <iostream>

using namespace std;

int add(int a, int b)

return a+b;


int subtract(int a, int b)

return a - b;


int main()

int a,b;
cin >> a >> b;

int c = add(a,b);
int d = subtract(a,b);
cout << c << " " << d << "n";
return 0;







c++ time






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 9:31









StoryTeller

92.9k12184251




92.9k12184251










asked Nov 11 at 9:17









Emil Bədrəddinli

256




256







  • 1




    Please read Why you should not #include <bits/stdc++.h>. Coupling that with using namespace std; is even worse.
    – StoryTeller
    Nov 11 at 9:18











  • Show what you have tried so far.
    – Swordfish
    Nov 11 at 9:22










  • See here: Easily measure elapsed time using <chrono>...
    – Ruks
    Nov 11 at 9:45







  • 2




    Welcome to Stack Overflow! Please take the tour and read How to Ask. Concerning your question, please also do at least a little bit of research. Just searching the web for something like "how to measure time in C++" should turn up hundreds of hit and at least some of them should do the job for you. Also, as mentioned, your problem description is vague. Your question could be paraphrased like "here's some code, I tried to modify it in some way I only hint at but it didn't fulfil my unclear expectations". That's unfortunately a bad question.
    – Ulrich Eckhardt
    Nov 11 at 9:46






  • 2




    Generally speaking, there is little point in trying to measure the time for a single function call that does a trivial operation like integer addition. There is too much variation in the times to make such measurement meaningful - do it a number of times, and the measurements will change. Instead, call the function some large number of times, to get a statistical measure (e.g. mean). Generally it will be necessary to obtain time before running the functions, obtain time after the functions are complete, and compute the difference.
    – Peter
    Nov 11 at 9:47












  • 1




    Please read Why you should not #include <bits/stdc++.h>. Coupling that with using namespace std; is even worse.
    – StoryTeller
    Nov 11 at 9:18











  • Show what you have tried so far.
    – Swordfish
    Nov 11 at 9:22










  • See here: Easily measure elapsed time using <chrono>...
    – Ruks
    Nov 11 at 9:45







  • 2




    Welcome to Stack Overflow! Please take the tour and read How to Ask. Concerning your question, please also do at least a little bit of research. Just searching the web for something like "how to measure time in C++" should turn up hundreds of hit and at least some of them should do the job for you. Also, as mentioned, your problem description is vague. Your question could be paraphrased like "here's some code, I tried to modify it in some way I only hint at but it didn't fulfil my unclear expectations". That's unfortunately a bad question.
    – Ulrich Eckhardt
    Nov 11 at 9:46






  • 2




    Generally speaking, there is little point in trying to measure the time for a single function call that does a trivial operation like integer addition. There is too much variation in the times to make such measurement meaningful - do it a number of times, and the measurements will change. Instead, call the function some large number of times, to get a statistical measure (e.g. mean). Generally it will be necessary to obtain time before running the functions, obtain time after the functions are complete, and compute the difference.
    – Peter
    Nov 11 at 9:47







1




1




Please read Why you should not #include <bits/stdc++.h>. Coupling that with using namespace std; is even worse.
– StoryTeller
Nov 11 at 9:18





Please read Why you should not #include <bits/stdc++.h>. Coupling that with using namespace std; is even worse.
– StoryTeller
Nov 11 at 9:18













Show what you have tried so far.
– Swordfish
Nov 11 at 9:22




Show what you have tried so far.
– Swordfish
Nov 11 at 9:22












See here: Easily measure elapsed time using <chrono>...
– Ruks
Nov 11 at 9:45





See here: Easily measure elapsed time using <chrono>...
– Ruks
Nov 11 at 9:45





2




2




Welcome to Stack Overflow! Please take the tour and read How to Ask. Concerning your question, please also do at least a little bit of research. Just searching the web for something like "how to measure time in C++" should turn up hundreds of hit and at least some of them should do the job for you. Also, as mentioned, your problem description is vague. Your question could be paraphrased like "here's some code, I tried to modify it in some way I only hint at but it didn't fulfil my unclear expectations". That's unfortunately a bad question.
– Ulrich Eckhardt
Nov 11 at 9:46




Welcome to Stack Overflow! Please take the tour and read How to Ask. Concerning your question, please also do at least a little bit of research. Just searching the web for something like "how to measure time in C++" should turn up hundreds of hit and at least some of them should do the job for you. Also, as mentioned, your problem description is vague. Your question could be paraphrased like "here's some code, I tried to modify it in some way I only hint at but it didn't fulfil my unclear expectations". That's unfortunately a bad question.
– Ulrich Eckhardt
Nov 11 at 9:46




2




2




Generally speaking, there is little point in trying to measure the time for a single function call that does a trivial operation like integer addition. There is too much variation in the times to make such measurement meaningful - do it a number of times, and the measurements will change. Instead, call the function some large number of times, to get a statistical measure (e.g. mean). Generally it will be necessary to obtain time before running the functions, obtain time after the functions are complete, and compute the difference.
– Peter
Nov 11 at 9:47




Generally speaking, there is little point in trying to measure the time for a single function call that does a trivial operation like integer addition. There is too much variation in the times to make such measurement meaningful - do it a number of times, and the measurements will change. Instead, call the function some large number of times, to get a statistical measure (e.g. mean). Generally it will be necessary to obtain time before running the functions, obtain time after the functions are complete, and compute the difference.
– Peter
Nov 11 at 9:47












1 Answer
1






active

oldest

votes


















0














If you want to do benchmarking, instead of using std::clock_t you should consider using time_point<high_resolution_clock> using std::chrono:



 using namespace std::chrono; 

high_resolution_clock::time_point t = high_resolution_clock::now();
//... do what is to be measured
high_resolution_clock::time_point t2 = high_resolution_clock::now();
cout << duration_cast<milliseconds>(t2 - t).count();


However, timing is always an imprecise measure that depends on the clock resolution (on windows for example, the resolution is around 15 ms). So the shorter the time you measure, the higher is the relative error in the measure. So if your function takes 15 ms to execute, on windows, you'll have an error in the measure of +/- 100%.



The only way you can increase reliability of the measure, is to measure a longer time, by having a huge number of iterations. So in the example, with 1 million iterations, the relative error of the measure is 0,0001%.



If you can benchmark your functions separately, just do it like that. But if you can't measure your functions separately (for example because one produces the input for the other), you can't use this approach). If you want to find the bottleneck your code in such case, then you should use the right tool: a profiler.






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247334%2fmeasuring-time-for-the-execution-of-functions%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    If you want to do benchmarking, instead of using std::clock_t you should consider using time_point<high_resolution_clock> using std::chrono:



     using namespace std::chrono; 

    high_resolution_clock::time_point t = high_resolution_clock::now();
    //... do what is to be measured
    high_resolution_clock::time_point t2 = high_resolution_clock::now();
    cout << duration_cast<milliseconds>(t2 - t).count();


    However, timing is always an imprecise measure that depends on the clock resolution (on windows for example, the resolution is around 15 ms). So the shorter the time you measure, the higher is the relative error in the measure. So if your function takes 15 ms to execute, on windows, you'll have an error in the measure of +/- 100%.



    The only way you can increase reliability of the measure, is to measure a longer time, by having a huge number of iterations. So in the example, with 1 million iterations, the relative error of the measure is 0,0001%.



    If you can benchmark your functions separately, just do it like that. But if you can't measure your functions separately (for example because one produces the input for the other), you can't use this approach). If you want to find the bottleneck your code in such case, then you should use the right tool: a profiler.






    share|improve this answer



























      0














      If you want to do benchmarking, instead of using std::clock_t you should consider using time_point<high_resolution_clock> using std::chrono:



       using namespace std::chrono; 

      high_resolution_clock::time_point t = high_resolution_clock::now();
      //... do what is to be measured
      high_resolution_clock::time_point t2 = high_resolution_clock::now();
      cout << duration_cast<milliseconds>(t2 - t).count();


      However, timing is always an imprecise measure that depends on the clock resolution (on windows for example, the resolution is around 15 ms). So the shorter the time you measure, the higher is the relative error in the measure. So if your function takes 15 ms to execute, on windows, you'll have an error in the measure of +/- 100%.



      The only way you can increase reliability of the measure, is to measure a longer time, by having a huge number of iterations. So in the example, with 1 million iterations, the relative error of the measure is 0,0001%.



      If you can benchmark your functions separately, just do it like that. But if you can't measure your functions separately (for example because one produces the input for the other), you can't use this approach). If you want to find the bottleneck your code in such case, then you should use the right tool: a profiler.






      share|improve this answer

























        0












        0








        0






        If you want to do benchmarking, instead of using std::clock_t you should consider using time_point<high_resolution_clock> using std::chrono:



         using namespace std::chrono; 

        high_resolution_clock::time_point t = high_resolution_clock::now();
        //... do what is to be measured
        high_resolution_clock::time_point t2 = high_resolution_clock::now();
        cout << duration_cast<milliseconds>(t2 - t).count();


        However, timing is always an imprecise measure that depends on the clock resolution (on windows for example, the resolution is around 15 ms). So the shorter the time you measure, the higher is the relative error in the measure. So if your function takes 15 ms to execute, on windows, you'll have an error in the measure of +/- 100%.



        The only way you can increase reliability of the measure, is to measure a longer time, by having a huge number of iterations. So in the example, with 1 million iterations, the relative error of the measure is 0,0001%.



        If you can benchmark your functions separately, just do it like that. But if you can't measure your functions separately (for example because one produces the input for the other), you can't use this approach). If you want to find the bottleneck your code in such case, then you should use the right tool: a profiler.






        share|improve this answer














        If you want to do benchmarking, instead of using std::clock_t you should consider using time_point<high_resolution_clock> using std::chrono:



         using namespace std::chrono; 

        high_resolution_clock::time_point t = high_resolution_clock::now();
        //... do what is to be measured
        high_resolution_clock::time_point t2 = high_resolution_clock::now();
        cout << duration_cast<milliseconds>(t2 - t).count();


        However, timing is always an imprecise measure that depends on the clock resolution (on windows for example, the resolution is around 15 ms). So the shorter the time you measure, the higher is the relative error in the measure. So if your function takes 15 ms to execute, on windows, you'll have an error in the measure of +/- 100%.



        The only way you can increase reliability of the measure, is to measure a longer time, by having a huge number of iterations. So in the example, with 1 million iterations, the relative error of the measure is 0,0001%.



        If you can benchmark your functions separately, just do it like that. But if you can't measure your functions separately (for example because one produces the input for the other), you can't use this approach). If you want to find the bottleneck your code in such case, then you should use the right tool: a profiler.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 11 at 10:26

























        answered Nov 11 at 10:05









        Christophe

        38.9k43474




        38.9k43474



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247334%2fmeasuring-time-for-the-execution-of-functions%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

            Syphilis

            Darth Vader #20