Skip to content

Commit 906ea56

Browse files
Add documentation for rules: GCI10, GCI203, GCI404, GCI72, GCI74, GCI7.
Co-authored-by: DataLabGroupe-CreditAgricole <[email protected]>
1 parent a5c2c8e commit 906ea56

File tree

16 files changed

+203
-2
lines changed

16 files changed

+203
-2
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,11 @@ 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+
==== Context
44+
45+
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.
46+
47+
image::image.png[]
48+
49+
We decided to take some measurements using CodeCarbon. 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.
227 KB
Loading
110 KB
Loading

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,29 @@ Or
8383
// ...
8484
}
8585
----
86+
87+
88+
==== Our Analysis
89+
90+
The following results were obtained through local experiments.
91+
===== Configuration
92+
* SQLite Database: 5-6 GB
93+
* Processor: Intel(R) Core(TM) Ultra 5 135U, 2100 MHz, 12 cores, 14 logical processors
94+
* RAM: 16 GB
95+
* CO2 Emissions Measurement: Using CodeCarbon
96+
97+
===== Context
98+
99+
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.
100+
101+
Example: for the same photo in JPG, it will be 370 KB and in SVG, it will be 830 KB.
102+
103+
image::photo.png[]
104+
105+
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.
106+
107+
image::CNN_architecture.png[]
108+
109+
==== References
110+
https://www.tensorflow.org/tutorials/images/cnn?hl=fr
111+
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: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,44 @@ for var in [var2 for var2 in range(100)]:
2020
for var in (var2 for var2 in range(100)):
2121
...
2222
----
23+
24+
== Our Analysis
25+
26+
The following results were obtained through local experiments.
27+
28+
Credit : https://github.com/AghilesAzzoug
29+
30+
=== Summary
31+
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.
32+
33+
=== Why it works?
34+
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.
35+
For more details on list comprehensions vs generator expressions, see Python documentation.
36+
37+
https://docs.python.org/3/howto/functional.html#generator-expressions-and-list-comprehensions
38+
39+
Local experiments* on list comprehensions vs generator comprehensions gives the following results on:
40+
41+
*1. Memory usage:*
42+
43+
image::memory.png[]
44+
45+
*2. Co2 Emissions*
46+
47+
Using CodeCarbon we get the following results:
48+
https://codecarbon.io/
49+
50+
image::carbone.png[]
51+
52+
For both metrics, the bigger the list, the greater is the gain is.
53+
54+
*Specs for experimentations:*
55+
56+
12th Gen Intel Core I7-12700H
57+
16 Gb RAM (4800 Mhz)
58+
Windows 11 OS version 22H2
59+
Python 3.9.15
60+
memory_profiler==0.61.0 (for RAM experiments)
61+
codecarbon==2.1.4 (for CO2 emissions)
62+
63+
Source: https://github.com/green-code-initiative/creedengo-rules-specifications/pull/152
32.2 KB
Loading
24.9 KB
Loading

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

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

37+
== Our 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+
Another representation of the results is shown below:
71+
72+
image::tracking_2.png[]
73+
74+
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).
75+
76+
:hide-uri-scheme:
77+
https://impactco2.fr/outils/comparateur
78+
79+
=== Conclusion
80+
81+
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.
82+
83+
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.
84+
85+
== References
86+
:hide-uri-scheme:
87+
https://www.datacamp.com/tutorial/property-getters-setters
34.2 KB
Loading

0 commit comments

Comments
 (0)