diff --git a/CHANGES/v2.2.txt b/CHANGES/v2.2.txt
index b352f97742c886fb36932fccac1f216ad28a6cf7..bbbaa6f9f5b5ec1829bf828ef529d9f9189876c2 100644
--- a/CHANGES/v2.2.txt
+++ b/CHANGES/v2.2.txt
@@ -175,6 +175,8 @@ For users:
   added if necessary.
 - Fix a bug in \ref METAD when using INTERVAL and ADAPTIVE gaussians at the same time
 - Updated gromacs patch for 5.1.x to 5.1.4
+- Fix a performance regression in the calculate loop where derivatives and forces were set to zero even if an action
+  was not active, this is relevant for postprocessing and for the on-the-fly analysis
 
 For developers:
 - Macports portile is now tested on travis at every plumed push.
diff --git a/src/core/PlumedMain.cpp b/src/core/PlumedMain.cpp
index f00c93876dad74cc04ce16f3f59d9b8aabe89aa7..5496236e5ebfa98348cd3ce9c9217c765f2fdbb6 100644
--- a/src/core/PlumedMain.cpp
+++ b/src/core/PlumedMain.cpp
@@ -652,23 +652,23 @@ void PlumedMain::justCalculate(){
   int iaction=0;
 // calculate the active actions in order (assuming *backward* dependence)
   for(ActionSet::iterator p=actionSet.begin();p!=actionSet.end();++p){
-    std::string actionNumberLabel;
-    if(detailedTimers){
-      Tools::convert(iaction,actionNumberLabel);
-      actionNumberLabel="4A "+actionNumberLabel+" "+(*p)->getLabel();
-      stopwatch.start(actionNumberLabel);
-    }
-    ActionWithValue*av=dynamic_cast<ActionWithValue*>(*p);
-    ActionAtomistic*aa=dynamic_cast<ActionAtomistic*>(*p);
-    {
-      if(av) av->clearInputForces();
-      if(av) av->clearDerivatives();
-    }
-    {
-      if(aa) aa->clearOutputForces();
-      if(aa) if(aa->isActive()) aa->retrieveAtoms();
-    }
     if((*p)->isActive()){
+      std::string actionNumberLabel;
+      if(detailedTimers){
+        Tools::convert(iaction,actionNumberLabel);
+        actionNumberLabel="4A "+actionNumberLabel+" "+(*p)->getLabel();
+        stopwatch.start(actionNumberLabel);
+      }
+      ActionWithValue*av=dynamic_cast<ActionWithValue*>(*p);
+      ActionAtomistic*aa=dynamic_cast<ActionAtomistic*>(*p);
+      {
+        if(av) av->clearInputForces();
+        if(av) av->clearDerivatives();
+      }
+      {
+        if(aa) aa->clearOutputForces();
+        if(aa) if(aa->isActive()) aa->retrieveAtoms();
+      }
       if((*p)->checkNumericalDerivatives()) (*p)->calculateNumericalDerivatives();
       else (*p)->calculate();
       // This retrieves components called bias 
@@ -677,9 +677,8 @@ void PlumedMain::justCalculate(){
       if(av)av->setGradientsIfNeeded();	
       ActionWithVirtualAtom*avv=dynamic_cast<ActionWithVirtualAtom*>(*p);
       if(avv)avv->setGradientsIfNeeded();	
+      if(detailedTimers) stopwatch.stop(actionNumberLabel);
     }
-
-    if(detailedTimers) stopwatch.stop(actionNumberLabel);
     iaction++;
   }
   stopwatch.stop("4 Calculating (forward loop)");