# Why does compute\_gradient execution time increase with each epoch?

**URL:** <https://fenicsproject.discourse.group/t/why-does-compute-gradient-execution-time-increase-with-each-epoch/16326>\
**Category:** dolfin-adjoint\
**Created:** [November 19, 2024, 2:40am UTC](https://fenicsproject.discourse.group/t/why-does-compute-gradient-execution-time-increase-with-each-epoch/16326 "2024-11-19T02:40:39Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![ekqls5264](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/ekqls5264/32/7787_2.png) [@ekqls5264](https://fenicsproject.discourse.group/u/ekqls5264)\
**Post date:** [November 19, 2024, 3:55am UTC](https://fenicsproject.discourse.group/t/why-does-compute-gradient-execution-time-increase-with-each-epoch/16326/2 "2024-11-19T03:55:12Z")

</div>

I referred to an answer in the thread **[Program killed when running compute\_gradient on transient problem](https://fenicsproject.discourse.group/t/program-killed-when-running-compute-gradient-on-transient-problem/10365/5)** and updated my code as follows:

```auto
# Gradient computation
compute_grad_start_time = time.time()
dLdk = ReducedFunctional(L, control_k)
compute_grad_end_time = time.time()

# Retrieve vector values
vector_start_time = time.time()
grad_L_k = dLdk.derivative().vector().get_local().mean()
vector_end_time = time.time()

```

However, even with this modification, the execution time continues to increase as the epochs progress. Specifically, the `vector_time` grows as follows:

- **Epoch 1:** `vector_time = 0:05:52`
- **Epoch 2:** `vector_time = 0:06:24`
- **Epoch 3:** `vector_time = 0:06:56`
- **Epoch 4:** `vector_time = 0:07:24`

---

_[View the full topic](https://fenicsproject.discourse.group/t/why-does-compute-gradient-execution-time-increase-with-each-epoch/16326)._
