Comparing runtimes [closed]

I am trying to get some accurate runtime comparisons of PHP vs Python (and potentially any other language that I have to include). Timing within a script is not my problem but timing within a script does not account for everything from the moment the request is made to run the script to output.

1) Is it actually worth taking such things into account?

2) Assuming it is worth taking it into account, how do I do this?

I'm using a Mac so I've got access to Linux commands and I'm not afraid to compile/create a command to help me, I just don't know how to write such a command.

Asked by: William658 | Posted: 28-01-2022

Answer 1

If your idea is to compare the languages, I'd say anything outside them is not relevant for comparison purposes.

Nonetheless you can use the time command to measure everything and can compare it with the timing within a script.

Like this:

$ time script.php

real    0m3.218s
user    0m0.080s
sys     0m0.064s

It will give you clock time, user time (php interpreter) and sys time (OS time)

If you are thinking web, then it gets a lot harder because you would be mixing webserver overhead and that is not always easy to compare if, say, you are using WSGI v/s mod_php. Then you'd have to hook probes into the webserving parts of the chain as well

Answered by: Alina609 | Posted: 01-03-2022

Answer 2

  1. It's worth taking speed into account if you're optimizing code. You should generally know why you're optimizing code (as in: a specific task in your existing codebase is taking too long, not "I heard PHP is slower than Python"). It's not worth taking speed into account if you don't actually plan on switching languages. Just because one tiny module does something slightly faster doesn't mean rewriting your app in another language is a good idea. There are many other factors to choosing a language besides speed.

  2. You benchmark, of course. Run the two codebases multiple times and compare the timing. You can use the time command if both scripts are executable from the shell, or use respective benchmarking functionality from each language; the latter case depends heavily on the actual language, naturally.

Answered by: Adelaide888 | Posted: 01-03-2022

Answer 3

Well, you can use the "time" command to help:

you@yourmachine:~$ time echo "hello world"
hello world

real    0m0.000s
user    0m0.000s
sys 0m0.000s

And this will get around timing outside of the environment.

As for whether you need to actually time that extra work... that entirely depends on what you are doing. I assume this is for some kind of web application of some sort, so it depends on how the framework you use actually works... does it cache some kind of compiled (or parsed) version of the script? If so, then startup time will be totally irrelevant (since the first hit will be the only one that startup time exists in).

Also, make sure to run your tests in a loop so you can discount the first run (and include the cost on the first run in your report if you want). I have done some tests in Java, and the first run is always slowest due to the JIT doing its job (and the same sort of hit may exist in PHP, Python and any other languages you try).

Answered by: Kelvin355 | Posted: 01-03-2022

Similar questions

In Python, is there a concise way of comparing whether the contents of two text files are the same?

I don't care what the differences are. I just want to know whether the contents are different.

python - Comparing List of Arguments to it self?

Kind of a weird question, but. I need to have a list of strings i need to make sure that every string in that list is the same. E.g: a = ['foo', 'foo', 'boo'] #not valid b = ['foo', 'foo', 'foo'] #valid Whats the best way to go about doing that? FYI, i don't know how many strings are going to be in the list. Also this is a super easy question, but i am just too tired to thi...

Comparing and updating array values in Python

I'm developing a Sirius XM radio desktop player in Python, in which I want the ability to display a table of all the channels and what is currently playing on each of them. This channel data is obtained from their website as a JSON string. I'm looking for the best data structure that would allow the cleanest way to compare and update the channel data. Arrays are problematic because I would want to be able...

python - comparing and sorting array

From two unequal arrays, i need to compare & delete based on the last value of an array. Example: m[0] and n[0] are read form a text file & saved as a array, [0] - their column number in text file. m[0] = [0.00, 1.15, 1.24, 1.35, 1.54, 2.32, 2.85, 3.10, 3.40, 3.80, 4.10, 4.21, 4.44] n[0] = [0.00, 1.12, 1.34, 1.45, 2.54, 3.12, 3.57]

Comparing elements in a list in Python's for -loop

What is wrong in the method end in the code? The method end returns always 1 although it should return 0 with the current data. # return 1 if the sum of four consecutive elements equal the sum over other sum of the other three sums # else return 0 # Eg the current sums "35 34 34 34" should return 0 data = "2|15|14|4|12|6|7|9|8|10|11|5...

Comparing Python nested lists

I have two nested lists, each nested list containing two strings e.g.: list 1 [('EFG', '[3,4,5]'), ('DEF', '[2,3,4]')] and list 2 [('DEF', '[2,3,4]'), ('FGH', '[4,5,6]')] I would like to compare the two lists and recover those nested lists which are identical with each other. In this case only ('DEF','[2,3,4]') would be returned. The lists could get long. Is there an effici...

plone - Comparing list item values to other items in other list in Python

I want to compare the values in one list to the values in a second list and return all those that are in the first list but not in the second i.e. list1 = ['one','two','three','four','five'] list2 = ['one','two','four'] would return 'three' and 'five'. I have only a little experience with python, so this may turn out to be a ridiculous and stupid way to attempt to solve it, but thi...

python - Comparing dicts and update a list of result

I have a list of dicts and I want to compare each dict in that list with a dict in a resulting list, add it to the result list if it's not there, and if it's there, update a counter associated with that dict. At first I wanted to use the solution described at Python : List of dict, if ...

datetime - Comparing a time delta in python

I have a variable which is <type 'datetime.timedelta'> and I would like to compare it against certain values. Lets say d produces this datetime.timedelta value 0:00:01.782000 I would like to compare it like this: #if d is greater than 1 minute if d>1:00: print "elapsed time is greater than 1 minute" I have tried converting...

Comparing two text files in python

I need to compare two files and redirect the different lines to third file. I know using diff command i can get the difference . But, is there any way of doing it in python ? Any sample code will be helpful

Still can't find your answer? Check out these communities...

PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python