Skip to content

Commit f0ecc41

Browse files
authored
Merge pull request #376 from cleophass/rules_doc
Add documentation for rules: GCI10, GCI203, GCI404, GCI72, GCI74, GCI7.
2 parents bdecda1 + b9abd8c commit f0ecc41

File tree

17 files changed

+271
-2
lines changed

17 files changed

+271
-2
lines changed

src/main/rules/GCI10/python/GCI10.asciidoc

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,34 @@ SVG images generated by common drawing softwares contains unnecessary data: calc
3939
<circle cx="104.02724" cy="152.19028" r="73.177132" style="fill:#ff00ff;stroke-width:0.264583"/>
4040
</svg>
4141
----
42+
43+
== Relevance Analysis
44+
45+
The following results were obtained through local experiments.
46+
47+
=== Configuration
48+
49+
* SQLite Database: 5-6 GB
50+
* Processor: Intel(R) Core(TM) Ultra 5 135U, 2100 MHz, 12 cores, 14 logical processors
51+
* RAM: 16 GB
52+
* CO2 Emissions Measurement: Using CodeCarbon
53+
54+
=== Context
55+
56+
For example, by using SVGOMG, a tool for optimizing SVGs, we were able to reduce the file size from 22 KB to 11 KB, a 50% reduction. This is useful for page loading or data storage.
57+
58+
image::image.png[]
59+
60+
=== Test Execution
61+
62+
For this analysis, we simulated a process of manipulating these SVG files. The objective was to measure the impact of file size on CO2 emissions. We used an "optimized" SVG file and a "non-optimized" SVG file. The modification consisted of changing certain parts of the SVG and saving the final result.
63+
64+
=== Impact Analysis
65+
66+
image::output.png[]
67+
68+
=== Conclusion
69+
70+
The emissions with the non-optimized file are 1.3928360831225074e-08, while the emissions with the optimized file are 9.68571752760373e-09, representing a 30% reduction in emissions.
71+
72+
We recommend incorporating file optimization techniques as part of our standard development practices and exploring additional opportunities to reduce the environmental impact of our digital products.
227 KB
Loading
16.9 KB
Loading
110 KB
Loading

src/main/rules/GCI203/python/GCI203.asciidoc

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,30 @@ Or
8383
// ...
8484
}
8585
----
86+
87+
== Relevance Analysis
88+
89+
The following results were obtained through local experiments.
90+
91+
=== Configuration
92+
93+
* SQLite Database: 5-6 GB
94+
* Processor: Intel(R) Core(TM) Ultra 5 135U, 2100 MHz, 12 cores, 14 logical processors
95+
* RAM: 16 GB
96+
* CO2 Emissions Measurement: Using CodeCarbon
97+
98+
=== Context
99+
100+
This rule depends heavily on the context. For simple icon, the use of SVG is relevant because they will be lighter than their JPG counterparts. However, for more complex images, such as photographs, SVG will be heavier.
101+
102+
Example: for the same photo in JPG, it will be 370 KB and in SVG, it will be 830 KB.
103+
104+
image::photo.png[]
105+
106+
This is because the vector format is not suitable for details, it is suitable for simple shapes (circle, line, icon, etc.). It has advantages in this area because it is not composed of pixels, it is extremely scalable. In the following cases: data science, convolutional neural networks, multimodal models, OCR, the SVG format is not suitable, it is preferable to use JPG or PNG. And this, because the models were trained on JPG and PNG formats and this is the input format of the CNNs, the use of SVG would involve additional preprocessing of the image. For example, the well-known CIFAR-10 dataset is in JPG format as can be seen in the following image, representing the architecture of a CNN.
107+
108+
image::CNN_architecture.png[]
109+
110+
=== References
111+
https://www.tensorflow.org/tutorials/images/cnn?hl=fr
112+
https://www.researchgate.net/publication/373715986_Convolutional_Neural_Network_CNN_The_architecture_and_applications
4.33 MB
Loading

src/main/rules/GCI404/python/GCI404.asciidoc

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,67 @@ for var in [var2 for var2 in range(100)]:
2020
for var in (var2 for var2 in range(100)):
2121
...
2222
----
23+
24+
== Relevance Analysis
25+
26+
The following results were obtained through local experiments.
27+
28+
Credit : https://github.com/AghilesAzzoug
29+
30+
=== Configuration
31+
32+
12th Gen Intel Core I7-12700H
33+
16 Gb RAM (4800 Mhz)
34+
Windows 11 OS version 22H2
35+
Python 3.9.15
36+
memory_profiler==0.61.0 (for RAM experiments)
37+
codecarbon==2.1.4 (for CO2 emissions)
38+
39+
=== Context
40+
41+
The use of generator expressions instead of list comprehensions in for-loops declaration can save RAM usage. It has multiple benefits like reducing CO2 emissions as well as releasing memory constraints on the hardware.
42+
Python generators resemble lazy lists from other programming languages: when iterated over, they compute their values on the fly. They lack some list behaviors (indexing, len method, ...) but are memory-efficient, as they do not store each of their values in memory, unlike lists. Thus, when declared in a for-loop declaration, list comprehensions can be safely replaced with generator expressions.
43+
For more details on list comprehensions vs generator expressions, see Python documentation.
44+
45+
46+
=== Impact Analysis
47+
48+
49+
Local experiments* on list comprehensions vs generator comprehensions gives the following results on:
50+
51+
*1. Memory usage:*
52+
53+
image::memory.png[]
54+
55+
*2. Co2 Emissions*
56+
57+
Using CodeCarbon we get the following results:
58+
https://codecarbon.io/
59+
60+
image::carbone.png[]
61+
62+
For both metrics, the bigger the list, the greater is the gain is.
63+
64+
=== Additional Study
65+
66+
A complementary benchmark from the Creedengo Challenge Issue #113 further supports the recommendation to avoid list comprehensions in loop declarations.
67+
68+
In a controlled containerized test:
69+
70+
The "bad" implementation using a list comprehension consumed: 4,493,365,500 bytes (~4.19 GB)
71+
72+
The "good" implementation using a generator expression consumed: 4,478,423,217 bytes (~4.17 GB)
73+
74+
Memory savings: 14,942,283 bytes (~14.24 MB)
75+
76+
Credit: https://github.com/green-code-initiative/creedengo-challenge/issues/113
77+
78+
=== Conclusion
79+
80+
Our analysis clearly demonstrates that replacing list comprehensions with generator expressions in Python for-loops offers substantial benefits in terms of both memory efficiency and environmental impact. As the data size increases, the advantages become increasingly significant.
81+
82+
=== References
83+
84+
Source: https://github.com/green-code-initiative/creedengo-rules-specifications/pull/152
85+
86+
https://docs.python.org/3/howto/functional.html#generator-expressions-and-list-comprehensions
32.2 KB
Loading
24.9 KB
Loading

src/main/rules/GCI7/python/GCI7.asciidoc

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,52 @@ client.age # Récupérer l'attribut age
3434
client.age = 26 # Modifier l'attribut age
3535
----
3636

37+
== Relevance Analysis
38+
39+
The following results were obtained through local experiments.
40+
41+
=== Configuration
42+
- Processor: Intel(R) Core(TM) Ultra 5 135U, 2100 MA1Hz, 12 cores, 14 logical processors
43+
- RAM: 16 GB
44+
- CO2 emissions measurement: Using CodeCarbon
45+
- Memory usage measurement: Using psutil
46+
- Time measurement: Using timeit
47+
48+
=== Impact Analysis
49+
50+
To measure the impact of using getters and setters, we created two classes: one with getters and setters and the other without. We then measured the memory usage of each class. Additionally, we measured the execution time of each class using timeit. Below is the code used to evaluate both time and carbon emissions.
51+
52+
[source,python]
53+
----
54+
class Direct():
55+
def __init__(self, age):
56+
self.age = age
57+
58+
59+
class WithGetter():
60+
def __init__(self, age):
61+
self._age = age
62+
63+
def get_age(self):
64+
return self._age
65+
66+
----
67+
68+
=== Results
69+
image::tracking_1.png[]
70+
71+
72+
For 100,000,000 iterations, the difference in CO2 equivalent emissions is 3.7*10^-6, which corresponds to 17 mm in equivalent emissions of a thermal car (see converter).
73+
74+
:hide-uri-scheme:
75+
https://impactco2.fr/outils/comparateur
76+
77+
=== Conclusion
78+
79+
Based on the results, we can conclude that using getters and setters in a class increases carbon emissions and execution time. Therefore, it is recommended to avoid them when possible. The performance difference comes from the overhead of method calls.
80+
81+
It is important to keep in mind that for security or control reasons, the use of getters and setters may be necessary. In such cases, using the @property decorator can be a good alternative to traditional getters and setters. It allows restricting access to the attribute and adding logic when the attribute is accessed or modified. Performance is better than recreating getter and setter methods but not as good as direct attribute access.
82+
83+
=== References
84+
:hide-uri-scheme:
85+
https://www.datacamp.com/tutorial/property-getters-setters

0 commit comments

Comments
 (0)