Here I'm working in Python, but it's more of a language agnostic question, unless specific language features makes it clear that an option is better than the other.
I get my raw data from a REST API, and need to do many operations with it.
tc_builds_by_agent = [
{
'agent_id': agent_id["id"],
'agent_name': agent_id["name"],
'builds':
[
dict(
element.attrib,
**{
'duration': str(
datetime.strptime(list_element[1].text[:-5], "%Y%m%dT%H%M%S") -
datetime.strptime(list_element[0].text[:-5], "%Y%m%dT%H%M%S")
)
}
)
for element in list(
tc_rest_request(f'/app/rest/builds?locator=agent(id:{agent_id["id"]})&fields=count,build(id,number,status,state,buildTypeId,startDate,finishDate)&count=10000')
)
if (list_element := list(element))
]
} for agent_id in agents_id]
So at the same time, I do two list comprehensions, construct a dictionnary, and slice strings -> convert to date time -> calculate delta -> convert back to string, and it's not over yet.
For context the agents_id array may have tens of elements, but each result from the REST call by tc_rest_request have thousands of elements, so at the end tens of thousands elements are being processed.
As a general rule I always try to iterate as little as possible on the same data, because it just seems more efficient.
But now I think I'm hitting a road block, because I need to also work on element.attrib which are dictionnaries with less than 10 elements, and do some more operations on each of them like string splitting and grouping. And as I said there are tens of thousands of them. I'm struggling to find a way to do them in this double list comprehension. And this section of code is already quite busy, it could become even more of a monstrosity.
Optimization isn't really an issue here, meaning that if it takes 15 or 20 seconds instead of 5 it doesn't matter. Of course the faster the better, but if I don't find a solution to do what I want in the next few hours I'll just do an other iteration over tc_builds_by_agent, and it'll do just fine.
So in such a situation, when each new iteration could come at a significant performance cost, what do you prefer, or what is objectively better?
Simpler, clearer code, that may take twice or thrice as long, or a more complex dense code for the sake of performance (and maybe a sense of satisfaction that you succeeded to do it as you wanted)?
or a more complex dense code for the sake of performanceis "high" performance among requirements?