Skip to content

Commit a2995ec

Browse files
authored
Merge pull request #381 from cleophass/PreferAppendLeft
GCI97 PreferAppendLeft #Python #DLG #RulesSpecifications
2 parents 82b2b45 + f617019 commit a2995ec

File tree

8 files changed

+68
-2
lines changed

8 files changed

+68
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

12+
- [#381](https://github.com/green-code-initiative/creedengo-rules-specifications/pull/381) Add rule GCI 108 Prefer Append Left
1213
- [#380](https://github.com/green-code-initiative/creedengo-rules-specifications/pull/380) Added rule GCI107 : DATA - Avoid Iterative Matrix Operations
1314

1415
### Changed

RULES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ Some are applicable for different technologies.
8484
| GCI105 | Python String Concatenation | Python String Concatenation - Use Join Instead or f-Strings instead of += | | 🚫 | 🚫 | 🚫 | 🚧 | 🚫 | 🚫 | 🚫 |
8585
| GCI106 | Avoid Square Root Operations In Loop | Using scalar `math.sqrt` (or `numpy.sqrt` on individual values) inside loops leads to inefficient code | | 🚫 | 🚫 | 🚫 || 🚫 | 🚫 | 🚫 |
8686
| GCI107 | Data : Avoid Iterative Matrix Operations | Use vectorization by the usage of the built-in functions of TensorFlow, NumPy or Pandas | | 🚫 | 🚫 | 🚫 | 🚧 | 🚫 | 🚫 | 🚫 |
87+
| GCI108 | Prefer Append Left | When you want to insert an element at the beginning of a list, it's better to use a deques or a double linkedlist. | |||| 🚀 ||||
8788
| GCI203 | Detect unoptimized file formats | When it is possible, to use svg format image over other image format | | 🚧 | 🚀 | 🚀 || 🚀 | 🚀 | 🚫 |
8889
| GCI404 | Avoid list comprehension in iterations | Use generator comprehension instead of list comprehension in for loop declaration | | 🚫 | 🚫 | 🚫 || 🚫 | 🚫 | 🚫 |
8990
| GCI522 | Sobriety: Brightness Override | To avoid draining the battery, iOS and Android devices adapt the brightness of the screen depending on the environment light. | | 🚫 | 🚫 || 🚫 | 🚫 | 🚫 | 🚫 |

src/main/rules/GCI108/GCI108.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"title": "DATA: Prefer AppendLeft Over Insert(0)",
3+
"type": "CODE_SMELL",
4+
"status": "ready",
5+
"remediation": {
6+
"func": "Constant\/Issue",
7+
"constantCost": "10min"
8+
},
9+
"tags": [
10+
"creedengo",
11+
"eco-design",
12+
"performance",
13+
"data"
14+
],
15+
"defaultSeverity": "Minor"
16+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
When you want to insert an element at the beginning of a list, it's better to use a deques or a double linked list.
2+
3+
== Non compliant Code Example
4+
5+
[source,python]
6+
----
7+
numbers.insert(0,val)
8+
----
9+
10+
== Compliant Solution
11+
12+
[source,python]
13+
----
14+
numbers.appendleft(val)
15+
----
16+
17+
== Relevance Analysis
18+
19+
The following results were obtained through local experiments.
20+
21+
=== Configuration
22+
23+
* Processor: Intel(R) Core(TM) Ultra 5 135U, 2100 MHz, 12 cores, 14 logical processors
24+
* RAM: 16 GB
25+
* CO2 Emissions Measurement: Using CodeCarbon
26+
27+
=== Context
28+
29+
We'll analyze the impact of inserting 10**6 elements at index 0 of a list and compare this with inserting the same number of elements using a deque (deque stands for Double-Ended Queue. It is a data structure that allows adding and removing elements from both ends efficiently. Unlike regular queues, which are typically operated on using FIFO (First In, First Out) principles, a deque supports both FIFO and LIFO (Last In, First Out))
30+
31+
32+
=== Impact Analysis
33+
34+
*1. Carbon emissions:*
35+
36+
image::carbon.png[]
37+
38+
*2. Execution time:*
39+
40+
image::time.png[]
41+
42+
=== Conclusion
43+
44+
The results show that using a deque is significantly more efficient than using a list for inserting elements at the beginning. The carbon emissions and execution time are both much lower when using a deque, making it the preferred choice for this operation.
45+
46+
=== References
47+
https://www.geeksforgeeks.org/deque-in-python/
48+
https://github.com/green-code-initiative/creedengo-challenge/issues/46
21.6 KB
Loading
19.2 KB
Loading

src/main/rules/GCI97/GCI97.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@
1313
],
1414
"defaultSeverity": "Minor"
1515
}
16-
16+

src/main/rules/GCI97/python/GCI97.asciidoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,4 @@ the rule is relevant, it also applies to the case where the user wishes to set h
9393

9494
=== References
9595

96-
https://chrissardegna.com/blog/python-expontentiation-performance/
96+
https://chrissardegna.com/blog/python-expontentiation-performance/

0 commit comments

Comments
 (0)