Measure execution time in different languages

356 views Asked by At

I am working on one project, the purpose of which is to compare sorting algorithms in different languages. My comparisons will be of two types:

  1. Comparison based on calendar time
  2. Comparison based on processor time

I want to know, if i can compare this methods / functions / procedures:

Comparison based on calendar time

  1. Java - System.nanoTime() then time difference /1000000 on millisec

  2. Python - time.time() then time difference *1000

  3. C - gettimeofday() then

     totalTime = (end.tv_sec - start.tv_sec) * 1e6;
     totalTime = (totalTime + (end.tv_usec -  start.tv_usec)) * 1e-3;
    
  4. Pascal - nowand then time difference using MilliSecondsBetween()

Comparison based on processor time

  1. Java - getThreadCpuTime() then time difference

  2. Python - thread_time() then time difference

  3. C - clock() then time difference

  4. Pascal - getTickCount64 then time difference //I am not sure of this, can you give advice?

1

There are 1 answers

0
Monsieur Merso On

I would go with external tool for such measure. Have to remember performance measurement is a tricky field and you have to ensure equal environment conditions and inputs for each of your implementations.

More than that:

  • performance of a C program may vary depending on a compiler that is being used and compiler flags.
  • performance of a python program will depend on the interpreter implementation Cython or Jython and so on.
  • there are multiple java implementations OpenJDK, Oracle java and so on.

It is really hard to compare performance of algorithm implementation across different languages because there are so many factors.

As a tool for measurement you can start with linux time command:

time java_program
# some output
real    0m20.608s
user    0m0.007s
sys 0m0.000s

time c_program
# some output
real    0m20.608s
user    0m0.007s
sys 0m0.000s
...

More about time command

For windows there is a powershell cmd-let Measure-Command, more about it