-
-
Notifications
You must be signed in to change notification settings - Fork 122
GCI107 AvoidIterativeMatrixOperations #Python #DLG #RulesSpecifications #380
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
Merged
dedece35
merged 6 commits into
green-code-initiative:main
from
cleophass:AvoidIterativeMatrixOperations
Aug 2, 2025
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
df16d20
GCI96 AvoidIterativeMatrixOperations
cleophass 6c1ff73
AvoidIterativeMatrixOperations Add explanation
cleophass d60ab17
AvoidIterativeMatrixOperations Add explanation
cleophass 0b8a177
AvoidIterativeMatrixOPerations update/GCI96.json: include data in title
cleophass 73d5f32
GCI107 AvoidIterativeMatrixOperations fix: change rule number (96->10…
cleophass 2a90b74
GCI107 AvoidIterativeMatrixOperations fix: resolve typo
cleophass File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| { | ||
|
cleophass marked this conversation as resolved.
|
||
| "title": "Avoid Iterative Matrix Operations", | ||
|
cleophass marked this conversation as resolved.
Outdated
|
||
| "type": "CODE_SMELL", | ||
| "status": "ready", | ||
| "remediation": { | ||
| "func": "Constant\/Issue", | ||
| "constantCost": "10min" | ||
| }, | ||
| "tags": [ | ||
| "creedengo", | ||
| "eco-design", | ||
| "performance", | ||
| "data", | ||
| "ai", | ||
| "vector", | ||
| "pandas", | ||
| "numpy" | ||
| ], | ||
| "defaultSeverity": "Minor" | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| Before going into more detail, it's important to understand how vectorization works in Python. When performing a calculation on an array/matrix, there are several possible methods: | ||
|
cleophass marked this conversation as resolved.
|
||
|
|
||
| The first is to go through the list and perform the calculation element by element, known as an iterative approach. | ||
| The second method consists of applying the calculation to the entire array/matrix at once, which is known as vectorization. | ||
|
|
||
| Although it's not possible to do this in all cases without applying real parallelism using a GPU, for example, we speak of vectorization when we use the built-in functions of TensorFlow, NumPy or Pandas. | ||
|
|
||
| We'll also have a iterative loop, but it will be executed in lower-level code (C). As with the use of built-in functions in general, since low-level languages like C are optimized, execution will be much faster and therefore emit less CO2. | ||
|
cleophass marked this conversation as resolved.
cleophass marked this conversation as resolved.
|
||
|
|
||
| == Non compliant Code Example | ||
|
|
||
| [source,python] | ||
| ---- | ||
| for i in range(len(A)): | ||
| for j in range(len(B[0])): | ||
| for k in range(len(B)): | ||
| results[i][j] += A[i][k] * B[k][j] | ||
| ---- | ||
|
|
||
| == Compliant Solution | ||
|
|
||
| [source,python] | ||
| ---- | ||
| results = np.dot(A, B) | ||
|
dedece35 marked this conversation as resolved.
|
||
| ---- | ||
|
|
||
| == Relevance Analysis | ||
|
|
||
| The following results were obtained through local experiments. | ||
|
|
||
| === Configuration | ||
|
|
||
| * Processor: Intel(R) Core(TM) Ultra 5 135U, 2100 MHz, 12 cores, 14 logical processors | ||
| * RAM: 16 GB | ||
| * CO2 Emissions Measurement: Using CodeCarbon | ||
|
|
||
| === Context | ||
|
|
||
| This study is divided into 3 parts, comparing a vectorized and an iterative method: | ||
| measuring the impact on a dot product between two vectors, | ||
| measuring the impact on an outer product between two vectors, | ||
| measuring the impact on a matrix calculation. | ||
|
|
||
| === Impact Analysis | ||
|
|
||
| *1. dot product:* | ||
|
|
||
| *Non compliant* | ||
| [source,python] | ||
| ---- | ||
| def iterative_dot_product(x,y): | ||
| total = 0 | ||
| for i in range(len(x)): | ||
| total += x[i] * y[i] | ||
| return total | ||
| ---- | ||
| *Compliant* | ||
| [source,python] | ||
| ---- | ||
| def vectorized_dot_product(x,y): | ||
| return np.dot(x,y) | ||
| ---- | ||
| image::dot.png[] | ||
|
|
||
| *2. Outer product:* | ||
|
|
||
| *Non compliant* | ||
| [source,python] | ||
| ---- | ||
| def iterative_outer_product(x, y): | ||
| o = np.zeros((len(x), len(y))) | ||
| for i in range(len(x)): | ||
| for j in range(len(y)): | ||
| o[i][j] = x[i] * y[j] | ||
| return o | ||
| ---- | ||
| *Compliant* | ||
| [source,python] | ||
| ---- | ||
| def vectorized_outer_product(x, y): | ||
| return np.outer(x, y) | ||
| ---- | ||
| image::outer.png[] | ||
|
|
||
| *3. Matrix product:* | ||
|
|
||
| *Non compliant* | ||
| [source,python] | ||
| ---- | ||
| def iterative_matrix_product(A, B): | ||
| for i in range(len(A)): | ||
| for j in range(len(B[0])): | ||
| for k in range(len(B)): | ||
| results[i][j] += A[i][k] * B[k][j] | ||
|
cleophass marked this conversation as resolved.
|
||
| return results | ||
| ---- | ||
| *Compliant* | ||
| [source,python] | ||
| ---- | ||
| def vectorized_outer_product(A, B): | ||
| return np.dot(A, B) | ||
| ---- | ||
| image::matrix.png[] | ||
|
|
||
| === Conclusion | ||
|
|
||
| The results show that the vectorized method is significantly faster than the iterative method. The CO2 emissions are also lower. This is a clear example of how using built-in functions can lead to more efficient code, both in terms of performance and environmental impact. | ||
|
|
||
| === References | ||
|
|
||
| https://sciresol.s3.us-east-2.amazonaws.com/IJST/Articles/2024/Issue-24/IJST-2024-914.pdf | ||
|
|
||
| https://arxiv.org/pdf/2308.01269 | ||
|
|
||
| https://www.db-thueringen.de/servlets/MCRFileNodeServlet/dbt_derivate_00062165/ilm1-2024200012.pdf | ||
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.