How many CPU cycles does an instruction take ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kevin Klein

    How many CPU cycles does an instruction take ?

    I am talking of C/C++ on unix platform using gcc. So feel free
    to take off any crossposted NG's if others are not offended,
    but I seek generic C/C++ and also unix and gcc specific answers.

    Essentially:

    /1/ How do I time a program in seconds or CPU cycles?
    /2/ How do I use the facilities to increase performance of
    any given program?
    /3/ CPU has many clocks I have heard. There is also a bus
    clock.
    /4/ I looked in a number of books. I found a little on time.h.
    But being humble, I thought I would ask experts as well.

    I have played with the statements like:

    #include <time.h>

    cycles=10000;

    start = clock();
    for (i=0; i<cycles; i++){
    return = slowfunction( arguments );
    }
    end = clock( );

    printf("Start time: %d, Stop time: %d, CLOCKS_PER_SEC: %d\nSeconds for %d calls: %f \n",
    start, end, CLOCKS_PER_SEC, cycles, ((dbl)(end - start)/CLOCKS_PER_SEC) );
    printf("Ticks for %d calls: %f \n", cycles, difftime(end, start) );


    // I am not exactly clear about CLOCKS_PER_SEC, difftime() .
    // I wrote this w/o clear conceptual understanding .
    // But I want clear concept so I know it works before I write it.
    // Using: gcc 2.7.2.3


    Kevin Klein

  • Malcolm

    #2
    Re: How many CPU cycles does an instruction take ?


    "Kevin Klein" <newnes@orz.com > wrote in message[color=blue]
    >
    > /1/ How do I time a program in seconds or CPU cycles?
    >[/color]
    As you've done it. A slow function can be timed in seconds, using time(), a
    fast function in clock ticks using clock().[color=blue]
    >
    > /2/ How do I use the facilities to increase performance of
    > any given program?
    >[/color]
    You really need a profiler to tell you where the program is spending its
    time. Then you optimise the bottlenecks. Occasionally timing something will
    tell you whether one version is faster than another, but it won't be the
    main weapon in your armoury.[color=blue]
    >
    > /3/ CPU has many clocks I have heard. There is also a bus
    > clock.
    >[/color]
    Things get complicated with modern processors. Early machines would execute
    exactly one instruction per cycle. However now processors are very fast but
    memory reads and writes haven't caught up, also some instructions are
    executed in parallel.[color=blue]
    >
    > /4/ I looked in a number of books. I found a little on time.h.
    > But being humble, I thought I would ask experts as well.
    >
    > I have played with the statements like:
    >
    > #include <time.h>
    >
    > cycles=10000;
    >
    > start = clock();
    > for (i=0; i<cycles; i++){
    > return = slowfunction( arguments );[/color]
    This is a syntax error. "return" is a keyword.[color=blue]
    > }
    > end = clock( );
    >
    > printf("Start time: %d, Stop time: %d, CLOCKS_PER_SEC: %d\nSeconds for[/color]
    %d calls: %f \n",[color=blue]
    > start, end, CLOCKS_PER_SEC, cycles, ((dbl)(end -[/color]
    start)/CLOCKS_PER_SEC) );[color=blue]
    > printf("Ticks for %d calls: %f \n", cycles, difftime(end, start) );
    >
    >[/color]
    On your machine clock_t may well be an int, however this isn't guaranteed.
    You need to cast values before you pass them to printf().


    Comment

    • Kelsey Bjarnason

      #3
      Re: How many CPU cycles does an instruction take ?

      [snips]

      On Thu, 01 Apr 2004 09:55:18 -0500, Kevin Klein wrote:
      [color=blue]
      > /1/ How do I time a program in seconds or CPU cycles?[/color]

      Your best bet is a profiling tool.
      [color=blue]
      > /2/ How do I use the facilities to increase performance of
      > any given program?[/color]

      By figuring out which bits of the program suck up the most time and
      therefore could benefit from optimization.
      [color=blue]
      > I have played with the statements like:
      >
      > #include <time.h>
      >
      > cycles=10000;
      >
      > start = clock();
      > for (i=0; i<cycles; i++){
      > return = slowfunction( arguments );
      > }
      > end = clock( );
      >
      > printf("Start time: %d, Stop time: %d, CLOCKS_PER_SEC: %d\nSeconds for
      > %d calls: %f \n",
      > start, end, CLOCKS_PER_SEC, cycles, ((dbl)(end -
      > start)/CLOCKS_PER_SEC) );
      > printf("Ticks for %d calls: %f \n", cycles, difftime(end, start) );[/color]

      difftime tells you the difference (in seconds) from time2 to time1 (oddly
      enough. You'd think the order would be the other way around, but anyhow...)

      It's a potentially useful metric... but not really good for finding
      hotspots in your code, without a fair bit of work. You really need a
      profiler - something that'll "automatica lly" watch your program run,
      recording what it's doing, how long it's taking, etc. It should also
      produce a report saying which functions were called the most frequently,
      which ones took the most time, etc.


      Comment

      • Thomas Matthews

        #4
        Re: How many CPU cycles does an instruction take ?

        Kevin Klein wrote:
        [color=blue]
        > I am talking of C/C++ on unix platform using gcc. So feel free
        > to take off any crossposted NG's if others are not offended,
        > but I seek generic C/C++ and also unix and gcc specific answers.
        >
        > Essentially:
        >
        > /1/ How do I time a program in seconds or CPU cycles?[/color]
        Off-topic in all newsgroups you posted to.
        Cpu cycles and instruction timing are dependent on the actual
        processor. Unix and gcc run on many processors.

        [color=blue]
        > /2/ How do I use the facilities to increase performance of
        > any given program?[/color]
        Use a profiler.

        [color=blue]
        > /3/ CPU has many clocks I have heard. There is also a bus
        > clock.[/color]
        Some have many clocks some have a few. Some may have a separate
        bus clock others may have one clock to rule them all.
        Again, depends on your processor. For example, the ARM processor
        differs in clock cycles from an Intel, Motorola and Signetics
        processor. Texas Instruments has different processors that
        each have different clock cycles.

        [color=blue]
        > /4/ I looked in a number of books. I found a little on time.h.
        > But being humble, I thought I would ask experts as well.
        >[/color]
        [snip]

        Ask in a newsgroup about your platform.
        You _could_ also try one of the "asm" newsgroups as well as
        news:comp.arch. embedded.
        [color=blue]
        >
        > Kevin Klein
        >[/color]


        --
        Thomas Matthews

        C++ newsgroup welcome message:

        C++ Faq: http://www.parashift.com/c++-faq-lite
        C Faq: http://www.eskimo.com/~scs/c-faq/top.html
        alt.comp.lang.l earn.c-c++ faq:

        Other sites:
        http://www.josuttis.com -- C++ STL Library book

        Comment

        • Dan Pop

          #5
          Re: How many CPU cycles does an instruction take ?

          In <uNebc.49384$gL 2.28046@newssvr 16.news.prodigy .com> Thomas Matthews <Thomas_Matthew sSpitsOnSpamBot s@sbcglobal.net > writes:
          [color=blue]
          >Kevin Klein wrote:
          >[color=green]
          >> I am talking of C/C++ on unix platform using gcc. So feel free
          >> to take off any crossposted NG's if others are not offended,
          >> but I seek generic C/C++ and also unix and gcc specific answers.
          >>
          >> Essentially:
          >>
          >> /1/ How do I time a program in seconds or CPU cycles?[/color][/color]
          ^^^^^^^^^^[color=blue]
          >Off-topic in all newsgroups you posted to.[/color]

          Timing a program in seconds is on topic on all the groups where time(),
          difftime() and clock() are topical, and this includes comp.lang.c,
          comp.lang.c++ and comp.unix.progr ammer.

          Dan
          --
          Dan Pop
          DESY Zeuthen, RZ group
          Email: Dan.Pop@ifh.de

          Comment

          Working...