Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- [#438](https://github.com/green-code-initiative/creedengo-rules-specifications/pull/438) Add rule GCI113 (Python): AI - Prefer XGBoost to RandomForest
- [#456](https://github.com/green-code-initiative/creedengo-rules-specifications/pull/456) Adding a check to a rule already implemented in Python

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add the number of the rule


### Changed

Expand Down
161 changes: 150 additions & 11 deletions src/main/rules/GCI35/python/GCI35.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,161 @@ When an exception is thrown, a variable (the exception itself) is created in a c

[source,python]
----
try:
f = open(path)
print(fh.read())
except:
print('No such file '+path
finally:
f.close()
try :
with open(path, "r") as f:
_ = f.read()
except :
pass
Comment thread
dedece35 marked this conversation as resolved.
----

== Compliant Solution
== Compliant Code Example

[source,python]
----
if os.path.isfile(path):
fh = open(path, 'r')
print(fh.read())
fh.close
with open(path, "r") as f:
_ = f.read()
----

== Experimental Verification

The rule was evaluated using:

* CodeCarbon to estimate CO₂ emissions
* execution time measurements
* multiple iteration scales to simulate real execution contexts

=== Configuration

[source,python]
----
from codecarbon import EmissionsTracker
import matplotlib.pyplot as plt
import os
import time

path = ('text.txt') # the famous file not founded

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aaudric please, take into account copilot feedback

----

=== Non compliant Implementation

[source,python]
----
#function who use the no compliant_solution
def no_compliant_solution():
for i in range(nb_runs):
try :
with open(path, "r") as f:
_ = f.read()
except :
pass
Comment thread
dedece35 marked this conversation as resolved.
----

=== Compliant Implementation

[source,python]
----
#function who use the compliant solution
def compliant_solution():
for i in range(nb_runs):
if os.path.isfile(path):
with open(path, "r") as f:
_ = f.read()

----

=== Measurement Function

[source,python]
----
# function to measure the impact of the both solutions
def measure(function):
start_time = time.time()
tracker = EmissionsTracker()
tracker.start()

function()
end_time = time.time()

emission = tracker.stop()
full_time = end_time-start_time

return emission, full_time

print(f"Emisssion no compliant solution :{no_emission_compliant_solution:.2e}")
print(f"Time for no compliant solution :{no_time_compliant_solution:.2f}")

print(f"Emisssion compliant solution : {emission_compliant_solution:.2e}")
print(f"Time for compliant solution :{time_compliant_solution:.2f}")
Comment thread
dedece35 marked this conversation as resolved.
----

=== Initial Results

Emisssion no compliant solution : 2.76e-05
Time for no compliant solution : 39.21

Emisssion compliant solution : 1.86e-05
Time for compliant solution : 26.35

The compliant solution shows:

* lower execution time
* lower CO₂ emissions
* reduced unnecessary resource consumption
Comment on lines +108 to +110

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aaudric please, take into account copilot feedback


== Large-Scale Iteration Analysis

To simulate real-world execution scenarios, the two implementations were tested with increasing numbers of iterations.

[source,python]
----
#Code to show the impact with a simulation of a real context
runs = [100, 500, 1000, 5000, 10000, 100000, 1000000, 10000000, 100000000]
times_no = []
times_yes = []
em_no = []
em_yes = []

for r in runs:
nb_runs = r
e1, t1 = measure(no_compliant_solution)
e2, t2 = measure(compliant_solution)

times_no.append(t1)
times_yes.append(t2)
em_no.append(e1)
em_yes.append(e2)

plt.plot(runs, times_no, label="try/except")
plt.plot(runs, times_yes, label="os.path.isfile")
plt.xlabel("Iterations")
plt.ylabel("Time (s)")
plt.legend()
plt.show()

plt.plot(runs, em_no, label="try/except")
plt.plot(runs, em_yes, label="os.path.isfile")
plt.xlabel("Iterations")
plt.ylabel("CO2 emissions en kg")
plt.legend()
plt.show()
----

=== Results
image::../python/comparison_plots.png[Metrics for the both splutions]

The emissions trend follows the same behavior as execution time:

* the try/except implementation consistently produces more emissions
* the gap increases with the number of iterations
* avoiding unnecessary exceptions improves energy efficiency

== Conclusion

Using try/except blocks to manage expected situations such as missing files introduces avoidable overhead in Python applications.

In repetitive or performance-sensitive code paths:

* exception handling increases execution time
* unnecessary object creation consumes additional resources
* higher CPU usage leads to greater energy consumption and CO₂ emissions
Binary file added src/main/rules/GCI35/python/comparison_plots.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading