Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Estimate time of execution c-function execution

Estimate time of execution c-function execution

Scheduled Pinned Locked Moved C / C++ / MFC
linuxalgorithmsperformancehelp
6 Posts 5 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CodingStyle
    wrote on last edited by
    #1

    Hello all! I need help. I need to estimate function execution time in under Linux OS. I guess that it's right way to estimate code/algorithm performance. I'm trying, something like this:

    #include #include #include void test(){
    int i;int s=0;

    for(i=1; i<1000000; i++){s+=sin(i)/i;}
    

    }
    float timedifference_msec(struct timeval t0, struct timeval t1)
    {
    return (t1.tv_sec - t0.tv_sec) * 1000.0f + (t1.tv_usec - t0.tv_usec) / 1000.0f;
    }

    int main(void)
    {
    struct timeval t0;
    struct timeval t1;
    float elapsed;
    gettimeofday(&t0, 0);
    f1();
    gettimeofday(&t1, 0);
    elapsed = timedifference_msec(t0, t1);
    printf("Code executed in %f milliseconds.\n", elapsed);
    return 0;
    }

    . But It's wall clock, I'm not sure that I can estimate performance by this way, besides, I need approach which will allow to measure time of each function which I choose. I've heard that there is approach to use CPU-time.

    L J D A 4 Replies Last reply
    0
    • C CodingStyle

      Hello all! I need help. I need to estimate function execution time in under Linux OS. I guess that it's right way to estimate code/algorithm performance. I'm trying, something like this:

      #include #include #include void test(){
      int i;int s=0;

      for(i=1; i<1000000; i++){s+=sin(i)/i;}
      

      }
      float timedifference_msec(struct timeval t0, struct timeval t1)
      {
      return (t1.tv_sec - t0.tv_sec) * 1000.0f + (t1.tv_usec - t0.tv_usec) / 1000.0f;
      }

      int main(void)
      {
      struct timeval t0;
      struct timeval t1;
      float elapsed;
      gettimeofday(&t0, 0);
      f1();
      gettimeofday(&t1, 0);
      elapsed = timedifference_msec(t0, t1);
      printf("Code executed in %f milliseconds.\n", elapsed);
      return 0;
      }

      . But It's wall clock, I'm not sure that I can estimate performance by this way, besides, I need approach which will allow to measure time of each function which I choose. I've heard that there is approach to use CPU-time.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      CodingStyle wrote:

      I've heard that there is approach to use CPU-time.

      And there is even a man page[^] for it.

      1 Reply Last reply
      0
      • C CodingStyle

        Hello all! I need help. I need to estimate function execution time in under Linux OS. I guess that it's right way to estimate code/algorithm performance. I'm trying, something like this:

        #include #include #include void test(){
        int i;int s=0;

        for(i=1; i<1000000; i++){s+=sin(i)/i;}
        

        }
        float timedifference_msec(struct timeval t0, struct timeval t1)
        {
        return (t1.tv_sec - t0.tv_sec) * 1000.0f + (t1.tv_usec - t0.tv_usec) / 1000.0f;
        }

        int main(void)
        {
        struct timeval t0;
        struct timeval t1;
        float elapsed;
        gettimeofday(&t0, 0);
        f1();
        gettimeofday(&t1, 0);
        elapsed = timedifference_msec(t0, t1);
        printf("Code executed in %f milliseconds.\n", elapsed);
        return 0;
        }

        . But It's wall clock, I'm not sure that I can estimate performance by this way, besides, I need approach which will allow to measure time of each function which I choose. I've heard that there is approach to use CPU-time.

        J Offline
        J Offline
        Jochen Arndt
        wrote on last edited by
        #3

        For high accuray difference time measurements with Linux use clock_gettime[^] with timer ID CLOCK_MONOTONIC or CLOCK_PROCESS_CPUTIME_ID. This requires including time.h and linking with the real time library librt (link option -lrt). An example can be found at http://www.guyrutenberg.com/2007/09/22/profiling-code-using-clock_gettime/[^].

        1 Reply Last reply
        0
        • C CodingStyle

          Hello all! I need help. I need to estimate function execution time in under Linux OS. I guess that it's right way to estimate code/algorithm performance. I'm trying, something like this:

          #include #include #include void test(){
          int i;int s=0;

          for(i=1; i<1000000; i++){s+=sin(i)/i;}
          

          }
          float timedifference_msec(struct timeval t0, struct timeval t1)
          {
          return (t1.tv_sec - t0.tv_sec) * 1000.0f + (t1.tv_usec - t0.tv_usec) / 1000.0f;
          }

          int main(void)
          {
          struct timeval t0;
          struct timeval t1;
          float elapsed;
          gettimeofday(&t0, 0);
          f1();
          gettimeofday(&t1, 0);
          elapsed = timedifference_msec(t0, t1);
          printf("Code executed in %f milliseconds.\n", elapsed);
          return 0;
          }

          . But It's wall clock, I'm not sure that I can estimate performance by this way, besides, I need approach which will allow to measure time of each function which I choose. I've heard that there is approach to use CPU-time.

          D Offline
          D Offline
          Dr Fry
          wrote on last edited by
          #4

          Yeah. There is such approach like, count the number of ticks to estimate performance. I do not think that it's panacea. Richard MacCutchan and Jochen Arndt also correct. But I guess that you mean universal and independent implementation version. Look here.

          C 1 Reply Last reply
          0
          • D Dr Fry

            Yeah. There is such approach like, count the number of ticks to estimate performance. I do not think that it's panacea. Richard MacCutchan and Jochen Arndt also correct. But I guess that you mean universal and independent implementation version. Look here.

            C Offline
            C Offline
            CodingStyle
            wrote on last edited by
            #5

            Thank you!

            1 Reply Last reply
            0
            • C CodingStyle

              Hello all! I need help. I need to estimate function execution time in under Linux OS. I guess that it's right way to estimate code/algorithm performance. I'm trying, something like this:

              #include #include #include void test(){
              int i;int s=0;

              for(i=1; i<1000000; i++){s+=sin(i)/i;}
              

              }
              float timedifference_msec(struct timeval t0, struct timeval t1)
              {
              return (t1.tv_sec - t0.tv_sec) * 1000.0f + (t1.tv_usec - t0.tv_usec) / 1000.0f;
              }

              int main(void)
              {
              struct timeval t0;
              struct timeval t1;
              float elapsed;
              gettimeofday(&t0, 0);
              f1();
              gettimeofday(&t1, 0);
              elapsed = timedifference_msec(t0, t1);
              printf("Code executed in %f milliseconds.\n", elapsed);
              return 0;
              }

              . But It's wall clock, I'm not sure that I can estimate performance by this way, besides, I need approach which will allow to measure time of each function which I choose. I've heard that there is approach to use CPU-time.

              A Offline
              A Offline
              Arthur V Ratz
              wrote on last edited by
              #6

              This is probably what you need:

              #include <time.h>
              #include <sys/time.h>
              #include <stdio.h>
              void test(){
              int i;int s=0;

              for(i=1; i<1000000; i++){s+=sin(i)/i;}
              

              }

              int main(void)
              {
              clock_t time[2], timediff;
              float elapsed;
              time[0] = clock();
              f1();
              time[1] = clock();
              elapsed = ((float)abs(time[0] - time[1]))/CLOCKS_PER_SEC;
              printf("Code executed in %f milliseconds.\n", elapsed);
              return 0;
              }

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups