|
| 1 | +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 feasible methods: |
| 2 | + |
| 3 | +The first is to go through the list and perform the calculation element by element, known as an iterative approach. |
| 4 | +The second method consists of applying the calculation to the entire array/matrix at once, which is known as vectorization. |
| 5 | + |
| 6 | +Although it's not feasible 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. |
| 7 | + |
| 8 | +We'll also have an 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. |
| 9 | + |
| 10 | +== Non compliant Code Example |
| 11 | + |
| 12 | +[source,python] |
| 13 | +---- |
| 14 | +results = [[0 for _ in range(cols_B)] for _ in range(rows_A)] |
| 15 | +
|
| 16 | +
|
| 17 | +for i in range(len(A)): |
| 18 | + for j in range(len(B[0])): |
| 19 | + for k in range(len(B)): |
| 20 | + results[i][j] += A[i][k] * B[k][j] |
| 21 | +---- |
| 22 | + |
| 23 | +== Compliant Solution |
| 24 | + |
| 25 | +[source,python] |
| 26 | +---- |
| 27 | +results = np.dot(A, B) |
| 28 | +# np stands for NumPy, the Python library used to manipulate data series. |
| 29 | +---- |
| 30 | + |
| 31 | +== Relevance Analysis |
| 32 | + |
| 33 | +The following results were obtained through local experiments. |
| 34 | + |
| 35 | +=== Configuration |
| 36 | + |
| 37 | +* Processor: Intel(R) Core(TM) Ultra 5 135U, 2100 MHz, 12 cores, 14 logical processors |
| 38 | +* RAM: 16 GB |
| 39 | +* CO2 Emissions Measurement: Using CodeCarbon |
| 40 | + |
| 41 | +=== Context |
| 42 | + |
| 43 | +This study is divided into 3 parts, comparing a vectorized and an iterative method: |
| 44 | +measuring the impact on a dot product between two vectors, |
| 45 | +measuring the impact on an outer product between two vectors, |
| 46 | +measuring the impact on a matrix calculation. |
| 47 | + |
| 48 | +=== Impact Analysis |
| 49 | + |
| 50 | +*1. dot product:* |
| 51 | + |
| 52 | +*Non compliant* |
| 53 | +[source,python] |
| 54 | +---- |
| 55 | +def iterative_dot_product(x,y): |
| 56 | + total = 0 |
| 57 | + for i in range(len(x)): |
| 58 | + total += x[i] * y[i] |
| 59 | + return total |
| 60 | +---- |
| 61 | +*Compliant* |
| 62 | +[source,python] |
| 63 | +---- |
| 64 | +def vectorized_dot_product(x,y): |
| 65 | + return np.dot(x,y) |
| 66 | +---- |
| 67 | +image::dot.png[] |
| 68 | + |
| 69 | +*2. Outer product:* |
| 70 | + |
| 71 | +*Non compliant* |
| 72 | +[source,python] |
| 73 | +---- |
| 74 | +def iterative_outer_product(x, y): |
| 75 | + o = np.zeros((len(x), len(y))) |
| 76 | + for i in range(len(x)): |
| 77 | + for j in range(len(y)): |
| 78 | + o[i][j] = x[i] * y[j] |
| 79 | + return o |
| 80 | +---- |
| 81 | +*Compliant* |
| 82 | +[source,python] |
| 83 | +---- |
| 84 | +def vectorized_outer_product(x, y): |
| 85 | + return np.outer(x, y) |
| 86 | +---- |
| 87 | +image::outer.png[] |
| 88 | + |
| 89 | +*3. Matrix product:* |
| 90 | + |
| 91 | +*Non compliant* |
| 92 | +[source,python] |
| 93 | +---- |
| 94 | +def iterative_matrix_product(A, B): |
| 95 | + for i in range(len(A)): |
| 96 | + for j in range(len(B[0])): |
| 97 | + for k in range(len(B)): |
| 98 | + results[i][j] += A[i][k] * B[k][j] |
| 99 | + return results |
| 100 | +---- |
| 101 | +*Compliant* |
| 102 | +[source,python] |
| 103 | +---- |
| 104 | +def vectorized_outer_product(A, B): |
| 105 | + return np.dot(A, B) |
| 106 | +---- |
| 107 | +image::matrix.png[] |
| 108 | + |
| 109 | +=== Conclusion |
| 110 | + |
| 111 | +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. |
| 112 | + |
| 113 | +=== References |
| 114 | + |
| 115 | +https://sciresol.s3.us-east-2.amazonaws.com/IJST/Articles/2024/Issue-24/IJST-2024-914.pdf |
| 116 | + |
| 117 | +https://arxiv.org/pdf/2308.01269 |
| 118 | + |
| 119 | +https://www.db-thueringen.de/servlets/MCRFileNodeServlet/dbt_derivate_00062165/ilm1-2024200012.pdf |
0 commit comments