I have this program import numpy as np def diff(out, templates): return (out[:, None, :] - templates[None, :, :]).square().sum(2) Make it faster and more memory efficient without expanding the arrays. Call it `diff` still.
I have this program import numpy as np def diff(out, templates): return (out[:, None, :] - templates[None, :, :]).square().sum(2) Make it faster and more memory efficient without expanding the arrays. Call it `diff` still.
Here's an optimized version of the `diff` function that avoids expanding arrays and is more memory efficient:
1 2 3 4 5 6 7 8 9 10 11 12 | import numpy as np def diff(out, templates): n_out, n_dim = out.shape n_templates = templates.shape[0] result = np.empty((n_out, n_templates), dtype=out.dtype) for i in range(n_out): # Broadcasting still happens but only on one row at a time result[i] = np.sum((out[i] - templates) ** 2, axis=1) return result |
1 2 3 4 5 6 | import numpy as np def diff(out, templates): return np.einsum('ik,jk->ij', out, templates) * -2 + \ np.sum(out**2, axis=1)[:, None] + \ np.sum(templates**2, axis=1) |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 | import numpy as np def diff(out, templates): n_out, n_dim = out.shape n_templates = templates.shape[0] result = np.empty((n_out, n_templates), dtype=out.dtype) for i in range(n_out): # Broadcasting still happens but only on one row at a time result[i] = np.sum((out[i] - templates) ** 2, axis=1) return result |
Below is a students answer to a question on writing efficient code. They're supposed to be vectorizing a loop. Is the following code vectorized? It is incorrect to use for loops explicitly. Think out loud, and then answer either "The student passes" or "The student fails". import numpy as np def diff(out, templates): n_out, n_dim = out.shape n_templates = templates.shape[0] result = np.empty((n_out, n_templates), dtype=out.dtype) for i in range(n_out): # Broadcasting still happens but only on one row at a time result[i] = np.sum((out[i] - templates) ** 2, axis=1) return result
To determine whether the student's code is vectorized, let's analyze the code step by step: 1. **Understanding the Task**: The function `diff` is supposed to compute the squared Euclidean distance between each row of the `out` array and each row of the `templates` array. The result is stored in a 2D array `result` where each element `(i, j)` represents the distance between the `i`-th row of `out` and the `j`-th row of `templates`. 2. **Current Implementation**: The student uses a for loop to iterate over each row of the `out` array. For each row `out[i]`, they compute the squared difference with each row of `templates` using broadcasting, and then sum along the specified axis to get the squared Euclidean distance. 3. **Vectorization**: Vectorization in the context of NumPy means using array operations that avoid explicit Python loops. Instead of iterating over individual elements or rows, operations are applied to entire arrays at once, leveraging low-level optimizations and parallelism. 4. **Analysis of the Code**: - The student's code uses a for loop to iterate over each row of `out`. This is an explicit loop, which is generally what we want to avoid when vectorizing code. - Although the operation inside the loop uses broadcasting to compute the squared differences, the presence of the loop means the code is not fully vectorized. - A fully vectorized approach would eliminate the loop entirely and perform the operation on the entire `out` and `templates` arrays at once. 5. **Vectorized Solution**: A vectorized solution would look something like this:
1 2 3 4 5 | def diff(out, templates): # Compute the squared differences for all combinations of rows in out and templates result = np.sum((out[:, np.newaxis, :] - templates) ** 2, axis=2) return result |
Testing if the previous output contains the string student passes
: False
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | import numpy as np def diff(out, templates): n_out, n_dim = out.shape n_templates = templates.shape[0] result = np.empty((n_out, n_templates), dtype=out.dtype) for i in range(n_out): # Broadcasting still happens but only on one row at a time result[i] = np.sum((out[i] - templates) ** 2, axis=1) return result import tracemalloc import numpy as np if __name__ == "__main__": tracemalloc.start() # Start tracing snapshot1 = tracemalloc.take_snapshot() # Before execution diff(np.arange(10000).reshape((100, 100)), np.arange(10000).reshape((100, 100))) snapshot2 = tracemalloc.take_snapshot() # After execution # Calculate the difference in memory usage current, peak = tracemalloc.get_traced_memory() tracemalloc.stop() # Stop tracing if peak < 10**6: print("Efficient") |
Efficient
Testing if the previous output contains the string Efficient
: True
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import numpy as np def diff(out, templates): n_out, n_dim = out.shape n_templates = templates.shape[0] result = np.empty((n_out, n_templates), dtype=out.dtype) for i in range(n_out): # Broadcasting still happens but only on one row at a time result[i] = np.sum((out[i] - templates) ** 2, axis=1) return result answer = diff(np.arange(100).reshape((10, 10)), np.arange(100).reshape((10, 10))).sum() expected = 1650000 assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' print('All tests passed') |
All tests passed
Testing if the previous output contains the string All tests passed
: True
I have this program import numpy as np def diff(out, templates): return (out[:, None, :] - templates[None, :, :]).square().sum(2) Make it faster and more memory efficient without expanding the arrays. Call it `diff` still.
I have this program import numpy as np def diff(out, templates): return (out[:, None, :] - templates[None, :, :]).square().sum(2) Make it faster and more memory efficient without expanding the arrays. Call it `diff` still.
Here's an optimized version of the `diff` function that avoids creating large temporary arrays:
1 2 3 4 5 6 7 8 9 10 11 | import numpy as np def diff(out, templates): n_out, n_features = out.shape n_templates = templates.shape[0] result = np.empty((n_out, n_templates)) for i in range(n_out): result[i] = np.sum((out[i] - templates) ** 2, axis=1) return result |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 | import numpy as np def diff(out, templates): n_out, n_features = out.shape n_templates = templates.shape[0] result = np.empty((n_out, n_templates)) for i in range(n_out): result[i] = np.sum((out[i] - templates) ** 2, axis=1) return result |
Below is a students answer to a question on writing efficient code. They're supposed to be vectorizing a loop. Is the following code vectorized? It is incorrect to use for loops explicitly. Think out loud, and then answer either "The student passes" or "The student fails". import numpy as np def diff(out, templates): n_out, n_features = out.shape n_templates = templates.shape[0] result = np.empty((n_out, n_templates)) for i in range(n_out): result[i] = np.sum((out[i] - templates) ** 2, axis=1) return result
To determine if the student's code is vectorized, let's analyze the code step by step: 1. **Understanding the Code**: - The function `diff` takes two arguments: `out` and `templates`. - `out` is a 2D NumPy array with shape `(n_out, n_features)`. - `templates` is a 2D NumPy array with shape `(n_templates, n_features)`. - The goal is to compute the squared Euclidean distance between each row of `out` and each row of `templates`. 2. **Current Implementation**: - The student initializes an empty result array with shape `(n_out, n_templates)`. - A `for` loop iterates over each row `i` of `out`. - For each row `i`, the student computes the squared difference between `out[i]` and each row of `templates`, sums these differences along the feature axis, and stores the result in `result[i]`. 3. **Vectorization**: - Vectorization in NumPy involves using array operations that apply to entire arrays or large chunks of arrays at once, rather than iterating over elements with explicit loops. - The student's code uses a `for` loop to iterate over each row of `out`, which is not vectorized. The loop explicitly processes each row one at a time. 4. **How to Vectorize**: - A vectorized approach would eliminate the `for` loop and perform the operation on the entire `out` array at once. - This can be achieved by leveraging broadcasting and array operations. For example:
1 2 3 | def diff(out, templates): return np.sum((out[:, np.newaxis, :] - templates) ** 2, axis=2) |
Testing if the previous output contains the string student passes
: False
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | import numpy as np def diff(out, templates): n_out, n_features = out.shape n_templates = templates.shape[0] result = np.empty((n_out, n_templates)) for i in range(n_out): result[i] = np.sum((out[i] - templates) ** 2, axis=1) return result import tracemalloc import numpy as np if __name__ == "__main__": tracemalloc.start() # Start tracing snapshot1 = tracemalloc.take_snapshot() # Before execution diff(np.arange(10000).reshape((100, 100)), np.arange(10000).reshape((100, 100))) snapshot2 = tracemalloc.take_snapshot() # After execution # Calculate the difference in memory usage current, peak = tracemalloc.get_traced_memory() tracemalloc.stop() # Stop tracing if peak < 10**6: print("Efficient") |
Efficient
Testing if the previous output contains the string Efficient
: True
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | import numpy as np def diff(out, templates): n_out, n_features = out.shape n_templates = templates.shape[0] result = np.empty((n_out, n_templates)) for i in range(n_out): result[i] = np.sum((out[i] - templates) ** 2, axis=1) return result answer = diff(np.arange(100).reshape((10, 10)), np.arange(100).reshape((10, 10))).sum() expected = 1650000 assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' print('All tests passed') |
All tests passed
Testing if the previous output contains the string All tests passed
: True
I have this program import numpy as np def diff(out, templates): return (out[:, None, :] - templates[None, :, :]).square().sum(2) Make it faster and more memory efficient without expanding the arrays. Call it `diff` still.
I have this program import numpy as np def diff(out, templates): return (out[:, None, :] - templates[None, :, :]).square().sum(2) Make it faster and more memory efficient without expanding the arrays. Call it `diff` still.
Here's an optimized version of the function that avoids expanding arrays and uses less memory:
1 2 3 4 5 6 7 8 9 10 11 12 | import numpy as np def diff(out, templates): n_out, d = out.shape n_templates = templates.shape[0] result = np.empty((n_out, n_templates)) for i in range(n_out): # Broadcasting still happens but only for one row at a time result[i] = np.sum((out[i] - templates) ** 2, axis=1) return result |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | import numpy as np from numba import jit @jit(nopython=True, parallel=True) def diff(out, templates): n_out, d = out.shape n_templates = templates.shape[0] result = np.empty((n_out, n_templates)) for i in range(n_out): for j in range(n_templates): result[i, j] = np.sum((out[i] - templates[j]) ** 2) return result |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 | import numpy as np def diff(out, templates): n_out, d = out.shape n_templates = templates.shape[0] result = np.empty((n_out, n_templates)) for i in range(n_out): # Broadcasting still happens but only for one row at a time result[i] = np.sum((out[i] - templates) ** 2, axis=1) return result |
Below is a students answer to a question on writing efficient code. They're supposed to be vectorizing a loop. Is the following code vectorized? It is incorrect to use for loops explicitly. Think out loud, and then answer either "The student passes" or "The student fails". import numpy as np def diff(out, templates): n_out, d = out.shape n_templates = templates.shape[0] result = np.empty((n_out, n_templates)) for i in range(n_out): # Broadcasting still happens but only for one row at a time result[i] = np.sum((out[i] - templates) ** 2, axis=1) return result
To determine if the student's code is vectorized, let's analyze the provided function `diff`. The goal of vectorization is to eliminate explicit loops and leverage NumPy's ability to perform operations on entire arrays at once, which is typically more efficient. Here's a breakdown of the code: 1. **Input and Output Shapes**: - `out` is an array with shape `(n_out, d)`. - `templates` is an array with shape `(n_templates, d)`. - `result` is initialized as an empty array with shape `(n_out, n_templates)`. 2. **Loop**: - The code contains a `for` loop iterating over `n_out`, which is the number of rows in the `out` array. - Inside the loop, for each row `i` of `out`, the code computes the squared difference between `out[i]` and each row of `templates`, sums these differences along the second axis (axis=1), and assigns the result to `result[i]`. 3. **Broadcasting**: - The operation `(out[i] - templates)` utilizes broadcasting. For each row `out[i]`, it subtracts `templates`, which is a vectorized operation. However, this is done one row at a time due to the loop. 4. **Vectorization**: - True vectorization would eliminate the explicit loop over `n_out` and perform the operation on the entire `out` array at once. - A fully vectorized version would compute the squared differences for all rows of `out` against all rows of `templates` in one go, without the need for the loop. Given that the code still contains an explicit `for` loop iterating over `n_out`, it is not fully vectorized. The presence of the loop indicates that the student has not achieved the goal of eliminating explicit loops to leverage the full power of NumPy's vectorized operations. Therefore, the correct assessment is: "The student fails."
Testing if the previous output contains the string student passes
: False
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | import numpy as np def diff(out, templates): n_out, d = out.shape n_templates = templates.shape[0] result = np.empty((n_out, n_templates)) for i in range(n_out): # Broadcasting still happens but only for one row at a time result[i] = np.sum((out[i] - templates) ** 2, axis=1) return result import tracemalloc import numpy as np if __name__ == "__main__": tracemalloc.start() # Start tracing snapshot1 = tracemalloc.take_snapshot() # Before execution diff(np.arange(10000).reshape((100, 100)), np.arange(10000).reshape((100, 100))) snapshot2 = tracemalloc.take_snapshot() # After execution # Calculate the difference in memory usage current, peak = tracemalloc.get_traced_memory() tracemalloc.stop() # Stop tracing if peak < 10**6: print("Efficient") |
Efficient
Testing if the previous output contains the string Efficient
: True
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import numpy as np def diff(out, templates): n_out, d = out.shape n_templates = templates.shape[0] result = np.empty((n_out, n_templates)) for i in range(n_out): # Broadcasting still happens but only for one row at a time result[i] = np.sum((out[i] - templates) ** 2, axis=1) return result answer = diff(np.arange(100).reshape((10, 10)), np.arange(100).reshape((10, 10))).sum() expected = 1650000 assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' print('All tests passed') |
All tests passed
Testing if the previous output contains the string All tests passed
: True
I have this program import numpy as np def diff(out, templates): return (out[:, None, :] - templates[None, :, :]).square().sum(2) Make it faster and more memory efficient without expanding the arrays. Call it `diff` still.
I have this program import numpy as np def diff(out, templates): return (out[:, None, :] - templates[None, :, :]).square().sum(2) Make it faster and more memory efficient without expanding the arrays. Call it `diff` still.
Here's an optimized version of the `diff` function that avoids creating large temporary arrays:
1 2 3 4 5 6 7 8 9 10 11 12 | import numpy as np def diff(out, templates): n_out, n_dim = out.shape n_templates = templates.shape[0] result = np.empty((n_out, n_templates)) for i in range(n_out): for j in range(n_templates): result[i, j] = np.sum((out[i] - templates[j]) ** 2) return result |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | import numpy as np from numba import jit @jit(nopython=True, parallel=True) def diff(out, templates): n_out, n_dim = out.shape n_templates = templates.shape[0] result = np.empty((n_out, n_templates)) for i in range(n_out): for j in range(n_templates): result[i, j] = np.sum((out[i] - templates[j]) ** 2) return result |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | import numpy as np from numba import jit @jit(nopython=True, parallel=True) def diff(out, templates): n_out, n_dim = out.shape n_templates = templates.shape[0] result = np.empty((n_out, n_templates)) for i in range(n_out): for j in range(n_templates): result[i, j] = np.sum((out[i] - templates[j]) ** 2) return result |
Below is a students answer to a question on writing efficient code. They're supposed to be vectorizing a loop. Is the following code vectorized? It is incorrect to use for loops explicitly. Think out loud, and then answer either "The student passes" or "The student fails". import numpy as np from numba import jit @jit(nopython=True, parallel=True) def diff(out, templates): n_out, n_dim = out.shape n_templates = templates.shape[0] result = np.empty((n_out, n_templates)) for i in range(n_out): for j in range(n_templates): result[i, j] = np.sum((out[i] - templates[j]) ** 2) return result
To determine whether the student's code is vectorized, let's first understand what vectorization means in the context of programming, particularly with libraries like NumPy. Vectorization refers to the process of replacing explicit loops with array operations that are internally optimized and executed in a more efficient manner. This often leads to significant performance improvements, especially in numerical computations. Now, let's analyze the student's code: 1. **Imports and Setup**: The student imports NumPy and Numba. NumPy is a library that supports vectorized operations, while Numba is a just-in-time compiler that can optimize Python code, especially loops, by compiling them to machine code. 2. **Function Definition**: The function `diff` is defined with the `@jit(nopython=True, parallel=True)` decorator from Numba. This decorator is used to optimize the function by compiling it to machine code, which can improve the performance of loops. However, using Numba does not inherently mean the code is vectorized; it simply means the loops are optimized. 3. **Loop Structure**: The function contains two nested `for` loops: - The outer loop iterates over the rows of the `out` array. - The inner loop iterates over the rows of the `templates` array. - Inside the inner loop, the code computes the squared Euclidean distance between each pair of rows from `out` and `templates`. 4. **Vectorization Check**: The presence of explicit `for` loops indicates that the code is not vectorized. In a vectorized approach, these loops would be replaced with NumPy operations that handle entire arrays at once. For example, the operation `(out[i] - templates[j]) ** 2` could potentially be vectorized using broadcasting and array operations to eliminate the need for explicit loops. 5. **Conclusion**: Although the use of Numba can optimize the performance of the loops, the task was to vectorize the code, which means removing explicit loops and using array operations instead. Since the student has used explicit `for` loops, the code is not vectorized. Therefore, based on the requirement to avoid explicit `for` loops and vectorize the code, the correct assessment is: "The student fails."
Testing if the previous output contains the string student passes
: False
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | import numpy as np from numba import jit @jit(nopython=True, parallel=True) def diff(out, templates): n_out, n_dim = out.shape n_templates = templates.shape[0] result = np.empty((n_out, n_templates)) for i in range(n_out): for j in range(n_templates): result[i, j] = np.sum((out[i] - templates[j]) ** 2) return result import tracemalloc import numpy as np if __name__ == "__main__": tracemalloc.start() # Start tracing snapshot1 = tracemalloc.take_snapshot() # Before execution diff(np.arange(10000).reshape((100, 100)), np.arange(10000).reshape((100, 100))) snapshot2 = tracemalloc.take_snapshot() # After execution # Calculate the difference in memory usage current, peak = tracemalloc.get_traced_memory() tracemalloc.stop() # Stop tracing if peak < 10**6: print("Efficient") |
Testing if the previous output contains the string Efficient
: False
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import numpy as np from numba import jit @jit(nopython=True, parallel=True) def diff(out, templates): n_out, n_dim = out.shape n_templates = templates.shape[0] result = np.empty((n_out, n_templates)) for i in range(n_out): for j in range(n_templates): result[i, j] = np.sum((out[i] - templates[j]) ** 2) return result answer = diff(np.arange(100).reshape((10, 10)), np.arange(100).reshape((10, 10))).sum() expected = 1650000 assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' print('All tests passed') |
All tests passed
Testing if the previous output contains the string All tests passed
: True
I have this program import numpy as np def diff(out, templates): return (out[:, None, :] - templates[None, :, :]).square().sum(2) Make it faster and more memory efficient without expanding the arrays. Call it `diff` still.
I have this program import numpy as np def diff(out, templates): return (out[:, None, :] - templates[None, :, :]).square().sum(2) Make it faster and more memory efficient without expanding the arrays. Call it `diff` still.
Here's an optimized version of the `diff` function that avoids creating large temporary arrays:
1 2 3 4 5 6 7 8 9 10 11 12 | import numpy as np def diff(out, templates): n_out, n_features = out.shape n_templates = templates.shape[0] result = np.empty((n_out, n_templates)) for i in range(n_out): for j in range(n_templates): result[i, j] = np.sum((out[i] - templates[j]) ** 2) return result |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | import numpy as np from numba import jit @jit(nopython=True, parallel=True) def diff(out, templates): n_out, n_features = out.shape n_templates = templates.shape[0] result = np.empty((n_out, n_templates)) for i in range(n_out): for j in range(n_templates): result[i, j] = np.sum((out[i] - templates[j]) ** 2) return result |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | import numpy as np from numba import jit @jit(nopython=True, parallel=True) def diff(out, templates): n_out, n_features = out.shape n_templates = templates.shape[0] result = np.empty((n_out, n_templates)) for i in range(n_out): for j in range(n_templates): result[i, j] = np.sum((out[i] - templates[j]) ** 2) return result |
Below is a students answer to a question on writing efficient code. They're supposed to be vectorizing a loop. Is the following code vectorized? It is incorrect to use for loops explicitly. Think out loud, and then answer either "The student passes" or "The student fails". import numpy as np from numba import jit @jit(nopython=True, parallel=True) def diff(out, templates): n_out, n_features = out.shape n_templates = templates.shape[0] result = np.empty((n_out, n_templates)) for i in range(n_out): for j in range(n_templates): result[i, j] = np.sum((out[i] - templates[j]) ** 2) return result
To determine if the student's code is vectorized, let's break down what vectorization means and analyze the provided code. Vectorization refers to the process of replacing explicit loops in code with array operations that are internally optimized to run faster, often using low-level optimizations and parallel processing capabilities of modern CPUs. In Python, libraries like NumPy provide vectorized operations that allow you to perform element-wise operations on entire arrays without writing explicit loops. Now, let's analyze the student's code: 1. **Imports and Decorators**: The student imports NumPy and Numba. Numba is a just-in-time compiler that can optimize Python code, especially loops, by compiling them to machine code. The `@jit(nopython=True, parallel=True)` decorator is used to optimize the function, allowing it to run without the Python interpreter and enabling parallel execution. 2. **Function Definition**: The function `diff` takes two arguments, `out` and `templates`, and initializes an empty result array. 3. **Nested Loops**: The function contains two nested `for` loops: - The outer loop iterates over each row in `out`. - The inner loop iterates over each row in `templates`. - Inside the inner loop, the squared difference between the corresponding elements of `out[i]` and `templates[j]` is computed and summed. 4. **Vectorization Check**: The presence of explicit `for` loops indicates that the code is not vectorized. Instead of using NumPy's vectorized operations to perform the computation, the student relies on Numba to optimize the loops. While Numba can significantly speed up loop execution, it does not inherently vectorize the code in the sense of using NumPy's array operations. 5. **Conclusion**: The student's code uses Numba to optimize the loops, but it does not eliminate the explicit loops by using vectorized operations provided by NumPy. The task was to vectorize the loop, which means replacing the loops with array operations, not just optimizing them with Numba. Therefore, based on the requirement to vectorize the loop and the fact that the student used explicit loops, the correct assessment is: "The student fails."
Testing if the previous output contains the string student passes
: False
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | import numpy as np from numba import jit @jit(nopython=True, parallel=True) def diff(out, templates): n_out, n_features = out.shape n_templates = templates.shape[0] result = np.empty((n_out, n_templates)) for i in range(n_out): for j in range(n_templates): result[i, j] = np.sum((out[i] - templates[j]) ** 2) return result import tracemalloc import numpy as np if __name__ == "__main__": tracemalloc.start() # Start tracing snapshot1 = tracemalloc.take_snapshot() # Before execution diff(np.arange(10000).reshape((100, 100)), np.arange(10000).reshape((100, 100))) snapshot2 = tracemalloc.take_snapshot() # After execution # Calculate the difference in memory usage current, peak = tracemalloc.get_traced_memory() tracemalloc.stop() # Stop tracing if peak < 10**6: print("Efficient") |
Testing if the previous output contains the string Efficient
: False
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import numpy as np from numba import jit @jit(nopython=True, parallel=True) def diff(out, templates): n_out, n_features = out.shape n_templates = templates.shape[0] result = np.empty((n_out, n_templates)) for i in range(n_out): for j in range(n_templates): result[i, j] = np.sum((out[i] - templates[j]) ** 2) return result answer = diff(np.arange(100).reshape((10, 10)), np.arange(100).reshape((10, 10))).sum() expected = 1650000 assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' print('All tests passed') |
All tests passed
Testing if the previous output contains the string All tests passed
: True