title: Why is my Python timer class not working?
body: <p>I'v written my own class to measure the time of different parts of my script:</p>
  <pre><code>class SimulationTimer: 
      accumulated_elapsed_time = 0.0 
      def __init__(self):
          self._simulation_start_time = None
          self._simulation_stop_time = None
  
      def start(self):
          &quot;&quot;&quot;start a new timer&quot;&quot;&quot;
          if self._simulation_start_time is not None:    # attribute
              raise TimeError(f&quot;Timer is running.\n Use .stop() to stop it&quot;)
  
          self._simulation_start_time = time.perf_counter()  
      def stop(self):
          &quot;&quot;&quot;stop the time and report the elsaped time&quot;&quot;&quot;
          if self._simulation_start_time is None:
              raise TimeError(f&quot;Timer is not running.\n Use .start() to start it.&quot;)
  
          self._simulation_stop_time = time.perf_counter()
          elapsed_simulation_time = self._simulation_stop_time - self._simulation_start_time  
          self.accumulated_elapsed_time += elapsed_simulation_time # Isn't accumulating values of times. 
  
          self._simulation_start_time = None
          print(f&quot;Elapsed time: {elapsed_simulation_time:0.10f} seconds&quot;)
  
      def get_accumulated_time(self):
          &quot;&quot;&quot; Return the elapsed time for later use&quot;&quot;&quot;
          return self.accumulated_elapsed_time
  </code></pre>
  <p>I'm trying to run a stochastic simulation multiple times on different cores and then plot <code>array1</code> against <code>array2</code> and measure the time it takes to run the simulation on the different cores that I'm running with <code>multiprocessing</code> <code>pool</code> method. With the following code:</p>
  <pre><code>def func(*args):
      t = SimulationTimer()
      t.start()
      Do stuff
      t.stop()
      print(f&quot;Accumulated time: {t.get_accumulated_time():0.10f} seconds&quot;)
  return array1, array2, t.accumulated_elapsed_time
  
  array1, array2, accumulated_elapsed_time = gillespie_tau_leaping(start_state, LHS, stoch_rate, state_change_array)
  </code></pre>
  <p>Need the above line so array1 and array2 can be passed to the plotting function</p>
  <pre><code>if __name__ == '__main__':
  with Pool() as p:
      pool_results = p.map(func, [*args])
      print(f&quot;Total time:\n {sum(pool_results)}&quot;)
  
  def gillespie_plot(array1, array2):
      fig, ax = plt.subplots()
      for i, label in enumerate(['Enzyme', 'Substrate', 'Enzyme-Substrate complex', 'Product']):
          ax.plot(array2, array1[:, i], label=label)
      ax.legend()
      plt.show()
      return fig
  
  gillespie_plot(array1, array2)
  </code></pre>
  <p>But the <code>accumulate_elapsed_time</code> doesn't seem to be working. It's returning 4 separate times for each process and not accumulating the values of all the processes into to a total time.</p>
  <p>Where am I going wrong?</p>
  <p>EDIT: Its been suggested that edit <code>accumulated_elapsed_time = 0.0</code> from an instance to a <code>class</code> variable and to return <code>t.accumulated_elapsed_time </code> from my function, which I've done but it still doesn't seem to work?</p>