Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EC75] [Python] Concatenate Strings in loop can be applied to Python as well #100

Open
LordPatate opened this issue Apr 5, 2023 · 6 comments
Assignees
Labels
🗃️ rule rule improvment or rule development or bug 🏆 challenge2023 🏆 Work done during the ecoCode Challenge 2023 __PRIO_MEDIUM__ python

Comments

@LordPatate
Copy link

(TO BE MEASURED)
The Rule EC75 about String concatenation in a loop is said "Not applicable" for Python, but there actually is a way to generate strings that might be a better manner than concatenation in a loop: creating a list of string using a list comprehension then calling the join() built-in.

Non-compliant code example:

s = ""
for fruit in fruits:
  s += fruit.name

Compliant code example:

l = [fruit.name for fruit in fruits]
"".join(l)
@dedece35 dedece35 added 🗃️ rule rule improvment or rule development or bug 🏆 challenge2023 🏆 Work done during the ecoCode Challenge 2023 python labels Apr 5, 2023
@dedece35 dedece35 changed the title Rule EC75: Concatenate Strings in loop can be applied to Python as well [EC75] [Python] Concatenate Strings in loop can be applied to Python as well Aug 25, 2023
@dedece35
Copy link
Member

Hi @LordPatate,
thank you for your issue and the new idea !
We aren't Python experts, and your contribution is a chance for us !

If we see the error message of EC75 rule, we can observe that the rule is specific for Java because we talk about StringBuilder.
Any way, the idea of this rule, can maybe applied to other languages. If yes, we will must change the error message.

By the other hand, this rule is really good for java because the usage of StringBuilder avoid construction of many String objects in memory, thus environment impact
Could you tell us how your idea can improve environment impact, with CPU or memory improvements ?

TO DISCUSS INSIDE CORE-TEAM : opinion about this new idea ?

@LordPatate
Copy link
Author

Hi. I'm thinking that using generator expression are generally more efficient than traditional "for" loops in Python and that Python built-ins like the join function are often implemented directly in C when using the CPython interpreter, making it more efficient than equivalent Python code.
Those are assumptions that needs to be verified through measurements.

@LordPatate
Copy link
Author

The benefits would likely be seen in term of CPU consumption

@dedece35
Copy link
Member

dedece35 commented Oct 6, 2023

@LordPatate, thank you for explanation ... do you have some references documentation or studies about this way of implementation and CPU consumption ? (to reference them in our documentation for future explanations)

@LordPatate
Copy link
Author

I don't have any references sadly. It's just my intuition, which is why I said it has to be measured

@glalloue
Copy link
Contributor

glalloue commented Oct 6, 2023

We are currently working on this subject (with Societe Generale company)
Rules related to this subject should be committed soon.

(seen with @dedece35 and @jhertout )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🗃️ rule rule improvment or rule development or bug 🏆 challenge2023 🏆 Work done during the ecoCode Challenge 2023 __PRIO_MEDIUM__ python
Projects
None yet
Development

No branches or pull requests

4 participants