how to massure cpu time to evalute sorting algorithems

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bary2000
    New Member
    • Jan 2010
    • 6

    how to massure cpu time to evalute sorting algorithems

    hi
    I am writing a program that have some sorting algorithms, and i want to compare between this algorithms and i need the code in C++ to see how much the cpu take time to execute each algorithm.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Easier said than done. It depends on a) the CPU, b) what else is running on the machine, c) buffering, d) randomness of the data, e) the operating system, f) number of cores, etc...

    You might do some research on profiling. You may find that your measurement comes form controlled executions rather than direct CPU usage measurements.

    Comment

    • logicbloke
      New Member
      • Jan 2010
      • 7

      #3
      The time ? if that's what you're looking for, include time.h then declare two
      variables, with clock_t as type, just like the following

      [CODE=C]clock_t start,end;

      start = clock(); //mark the beginnning
      function();
      end = clock(); // mark the end
      printf("Time elapsed : %ld ms.",(end-start)*1000/CLOCKS_PER_SEC) ;
      [/CODE]
      CLOCKS_PER_SEC is a constant that will let you know how many clocks your configuration (hardware+softw are) generates per second.

      I hope this helps

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Unfortunately on todays multicore processors and multitasking operating systems that doesn't work.

        clock() returns the number of clock ticks elapsed since the program was launched. On such an operating sytem your algorithm ends up measuring not just how long the algorithm took to run but also any operating system activity and the runtime of any other programs that received processor time while your program was running.

        If you want to know how long a particular algorithm (or function) is taking to run in a program probably your best bet is to get a decent profiler.

        Some operating systems (including Windows) can provide information on how much processor time a specific process (or thread) has had in which case writing a small test program for the algorithm/function and then accessing the OS stats might be of use.

        Comment

        Working...