Properly measure execution time for benchmarking











up vote
0
down vote

favorite












I hope this is the correct place to ask this question.



I would like to benchmark a program using some test instances. I’m primarily interested in the cpu time the program took to execute and to this end, I’ve found the time command that gives me the real, user and sys time. As I’m quite new to linux, I’m not sure which of these to take. For me, user and sys seem to be my best bet, but I’m not quite sure which one to take. Does it make sense to add them up to get the total cpu-seconds or is user the most meaningful in my application?



Any help appreciated!










share|improve this question









New contributor




Gorid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • time sleep 10
    – j-money
    21 hours ago















up vote
0
down vote

favorite












I hope this is the correct place to ask this question.



I would like to benchmark a program using some test instances. I’m primarily interested in the cpu time the program took to execute and to this end, I’ve found the time command that gives me the real, user and sys time. As I’m quite new to linux, I’m not sure which of these to take. For me, user and sys seem to be my best bet, but I’m not quite sure which one to take. Does it make sense to add them up to get the total cpu-seconds or is user the most meaningful in my application?



Any help appreciated!










share|improve this question









New contributor




Gorid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • time sleep 10
    – j-money
    21 hours ago













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I hope this is the correct place to ask this question.



I would like to benchmark a program using some test instances. I’m primarily interested in the cpu time the program took to execute and to this end, I’ve found the time command that gives me the real, user and sys time. As I’m quite new to linux, I’m not sure which of these to take. For me, user and sys seem to be my best bet, but I’m not quite sure which one to take. Does it make sense to add them up to get the total cpu-seconds or is user the most meaningful in my application?



Any help appreciated!










share|improve this question









New contributor




Gorid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I hope this is the correct place to ask this question.



I would like to benchmark a program using some test instances. I’m primarily interested in the cpu time the program took to execute and to this end, I’ve found the time command that gives me the real, user and sys time. As I’m quite new to linux, I’m not sure which of these to take. For me, user and sys seem to be my best bet, but I’m not quite sure which one to take. Does it make sense to add them up to get the total cpu-seconds or is user the most meaningful in my application?



Any help appreciated!







command-line 18.04 performance






share|improve this question









New contributor




Gorid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Gorid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 9 hours ago









dessert

20.9k55896




20.9k55896






New contributor




Gorid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 21 hours ago









Gorid

1




1




New contributor




Gorid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Gorid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Gorid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • time sleep 10
    – j-money
    21 hours ago


















  • time sleep 10
    – j-money
    21 hours ago
















time sleep 10
– j-money
21 hours ago




time sleep 10
– j-money
21 hours ago










1 Answer
1






active

oldest

votes

















up vote
0
down vote













By default, there are two time commands on your system:




  • a bash shell keyword

    This is called when you run time, you can get help about it with help time. If you want to tweak its output, read about the TIMEFORMAT variable in man bash.

  • a binary /usr/bin/time

    If you want this you need to call it with its full path, you can get help about it with man time.


The /usr/bin/time binary comes with a nice -f, --format option which lets you choose the information in its output. The mentioned manpage has the full list of specifiers under FORMATTING THE OUTPUT, here are just some suggestions that may be relevant for you:




K Average total (data+stack+text) memory use of the process, in Kilobytes.
S Total number of CPU-seconds used by the system on behalf of the process (in kernel mode), in seconds.
U Total number of CPU-seconds that the process used directly (in user mode), in seconds.




If S and U is what’s important in your case and you want those two on separate lines, your command line could look like this:



/usr/bin/time -f "%Sn%U" /path/to/program


As S and U are available for bash’s time as well, this translates to:



TIMEFORMAT=$'%Sn%U' && time /path/to/program


It’s worth noting that the bash keyword is able to output milliseconds while the binary does only output centiseconds. The keyword may also be faster, as no external program needs to be called.



For an overview on the timing topic, I recommend reading man 7 time.






share|improve this answer























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "89"
    };
    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',
    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
    });


    }
    });






    Gorid is a new contributor. Be nice, and check out our Code of Conduct.










     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1092983%2fproperly-measure-execution-time-for-benchmarking%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








    up vote
    0
    down vote













    By default, there are two time commands on your system:




    • a bash shell keyword

      This is called when you run time, you can get help about it with help time. If you want to tweak its output, read about the TIMEFORMAT variable in man bash.

    • a binary /usr/bin/time

      If you want this you need to call it with its full path, you can get help about it with man time.


    The /usr/bin/time binary comes with a nice -f, --format option which lets you choose the information in its output. The mentioned manpage has the full list of specifiers under FORMATTING THE OUTPUT, here are just some suggestions that may be relevant for you:




    K Average total (data+stack+text) memory use of the process, in Kilobytes.
    S Total number of CPU-seconds used by the system on behalf of the process (in kernel mode), in seconds.
    U Total number of CPU-seconds that the process used directly (in user mode), in seconds.




    If S and U is what’s important in your case and you want those two on separate lines, your command line could look like this:



    /usr/bin/time -f "%Sn%U" /path/to/program


    As S and U are available for bash’s time as well, this translates to:



    TIMEFORMAT=$'%Sn%U' && time /path/to/program


    It’s worth noting that the bash keyword is able to output milliseconds while the binary does only output centiseconds. The keyword may also be faster, as no external program needs to be called.



    For an overview on the timing topic, I recommend reading man 7 time.






    share|improve this answer



























      up vote
      0
      down vote













      By default, there are two time commands on your system:




      • a bash shell keyword

        This is called when you run time, you can get help about it with help time. If you want to tweak its output, read about the TIMEFORMAT variable in man bash.

      • a binary /usr/bin/time

        If you want this you need to call it with its full path, you can get help about it with man time.


      The /usr/bin/time binary comes with a nice -f, --format option which lets you choose the information in its output. The mentioned manpage has the full list of specifiers under FORMATTING THE OUTPUT, here are just some suggestions that may be relevant for you:




      K Average total (data+stack+text) memory use of the process, in Kilobytes.
      S Total number of CPU-seconds used by the system on behalf of the process (in kernel mode), in seconds.
      U Total number of CPU-seconds that the process used directly (in user mode), in seconds.




      If S and U is what’s important in your case and you want those two on separate lines, your command line could look like this:



      /usr/bin/time -f "%Sn%U" /path/to/program


      As S and U are available for bash’s time as well, this translates to:



      TIMEFORMAT=$'%Sn%U' && time /path/to/program


      It’s worth noting that the bash keyword is able to output milliseconds while the binary does only output centiseconds. The keyword may also be faster, as no external program needs to be called.



      For an overview on the timing topic, I recommend reading man 7 time.






      share|improve this answer

























        up vote
        0
        down vote










        up vote
        0
        down vote









        By default, there are two time commands on your system:




        • a bash shell keyword

          This is called when you run time, you can get help about it with help time. If you want to tweak its output, read about the TIMEFORMAT variable in man bash.

        • a binary /usr/bin/time

          If you want this you need to call it with its full path, you can get help about it with man time.


        The /usr/bin/time binary comes with a nice -f, --format option which lets you choose the information in its output. The mentioned manpage has the full list of specifiers under FORMATTING THE OUTPUT, here are just some suggestions that may be relevant for you:




        K Average total (data+stack+text) memory use of the process, in Kilobytes.
        S Total number of CPU-seconds used by the system on behalf of the process (in kernel mode), in seconds.
        U Total number of CPU-seconds that the process used directly (in user mode), in seconds.




        If S and U is what’s important in your case and you want those two on separate lines, your command line could look like this:



        /usr/bin/time -f "%Sn%U" /path/to/program


        As S and U are available for bash’s time as well, this translates to:



        TIMEFORMAT=$'%Sn%U' && time /path/to/program


        It’s worth noting that the bash keyword is able to output milliseconds while the binary does only output centiseconds. The keyword may also be faster, as no external program needs to be called.



        For an overview on the timing topic, I recommend reading man 7 time.






        share|improve this answer














        By default, there are two time commands on your system:




        • a bash shell keyword

          This is called when you run time, you can get help about it with help time. If you want to tweak its output, read about the TIMEFORMAT variable in man bash.

        • a binary /usr/bin/time

          If you want this you need to call it with its full path, you can get help about it with man time.


        The /usr/bin/time binary comes with a nice -f, --format option which lets you choose the information in its output. The mentioned manpage has the full list of specifiers under FORMATTING THE OUTPUT, here are just some suggestions that may be relevant for you:




        K Average total (data+stack+text) memory use of the process, in Kilobytes.
        S Total number of CPU-seconds used by the system on behalf of the process (in kernel mode), in seconds.
        U Total number of CPU-seconds that the process used directly (in user mode), in seconds.




        If S and U is what’s important in your case and you want those two on separate lines, your command line could look like this:



        /usr/bin/time -f "%Sn%U" /path/to/program


        As S and U are available for bash’s time as well, this translates to:



        TIMEFORMAT=$'%Sn%U' && time /path/to/program


        It’s worth noting that the bash keyword is able to output milliseconds while the binary does only output centiseconds. The keyword may also be faster, as no external program needs to be called.



        For an overview on the timing topic, I recommend reading man 7 time.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 8 hours ago

























        answered 9 hours ago









        dessert

        20.9k55896




        20.9k55896






















            Gorid is a new contributor. Be nice, and check out our Code of Conduct.










             

            draft saved


            draft discarded


















            Gorid is a new contributor. Be nice, and check out our Code of Conduct.













            Gorid is a new contributor. Be nice, and check out our Code of Conduct.












            Gorid is a new contributor. Be nice, and check out our Code of Conduct.















             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1092983%2fproperly-measure-execution-time-for-benchmarking%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

            Mont Emei

            Province de Neuquén

            Journaliste