2

I want to compare the performance of two different implementations (Python and Java) of the same algorithm. I run scripts on the terminal (using Ubuntu 18) like this:

time script_name

I'm not sure how accurate this is. Is it possible to increase the accuracy of this benchmark? Perhaps there is a way to remove any restrictions or set-up in Python or Java?

Peiffap
  • 163
  • 1
  • 14
  • 1
    see https://stackoverflow.com/a/2866456/1358308 for an example in Python, there will be similar articles for other languages – Sam Mason Oct 17 '18 at 21:18
  • 1
    note that if you use `time` to measure performance, you also measure the startup time of the jvm and the python interpreter – Lars Oct 17 '18 at 21:20

2 Answers2

1

The accuracy of the time command is probably fine for most testing.

But if you wanted, you could write a 2nd Python script for timing with an import subprocess that then uses the subprocess.call() function (or one of several related functions in the subprocess module) to run the two versions of the algorithm.

Your timing script could also import time and then do datetime.datetime.now().time() before and after the algorithm runs, to show how much time passed.

corecursion
  • 826
  • 7
  • 10
1

As explained in this answer, the correct way to benchmark a program using time is the following command: sudo chrt -f 99 /usr/bin/time --verbose <benchmark>. However, note that this will only be accurate if the algorithm takes at least a second to execute, as otherwise the exec call might take up a big part of the benchmark.

This answer suggests using perf stat instead, as such: perf stat -r 10 -d <your app and arguments>.

Peiffap
  • 163
  • 1
  • 14
  • Note mentioning that java vm has a 'warm-up' time during performance is bad. I'm not familiar with Python, but maybe it needs a 'warm-up' too. See https://stackoverflow.com/q/1481853/6937584 for java vm – cdalxndr Oct 17 '18 at 21:42
  • Well ideally, I'd suggest using a real benchmarking program, but this question was about how to use the `time` command. – Peiffap Oct 17 '18 at 21:52