diff --git a/python/dune/codegen/sumfact/autotune.py b/python/dune/codegen/sumfact/autotune.py
index e0788ca6c3e1a41e524593e7ec4cd66160144602..4a40a37a48c188aa656d26364e94e54800d90730 100644
--- a/python/dune/codegen/sumfact/autotune.py
+++ b/python/dune/codegen/sumfact/autotune.py
@@ -95,10 +95,8 @@ def write_global_data(sf, filename):
 
 
 def write_setup_code(sf, filename, define_thetas=True):
-    opcounting = get_option("opcounter")
     with open(filename, "a") as f:
         # Setup a polynomial object (normally done in the LocalOperator members)
-        set_option("opcounter", False)
         from dune.codegen.loopy.target import type_floatingpoint
         real = type_floatingpoint()
         f.write("  using RF = {};\n".format(real))
@@ -173,7 +171,6 @@ def write_setup_code(sf, filename, define_thetas=True):
                       "  rng.seed(42);\n",
                       "  std::uniform_int_distribution<> dis(0, {});\n".format(size / (get_option("precision_bits") / 8)),
                       ])
-    return opcounting
 
 
 def generate_standalone_code_google_benchmark(sf, filename):
@@ -188,6 +185,10 @@ def generate_standalone_code_google_benchmark(sf, filename):
     first_line = knl.member.lines[0]
     arguments = first_line[first_line.find("(") + 1:first_line.find(")")]
 
+    # Turn off opcounting
+    opcounting = get_option("opcounter")
+    set_option("opcounter", False)
+
     with open(filename, "w") as f:
         f.writelines(["// {}".format(first_line),
                       "\n",
@@ -213,7 +214,7 @@ def generate_standalone_code_google_benchmark(sf, filename):
         f.write("\n\n")
         f.write("static void BM_sumfact_kernel(benchmark::State& state){\n")
 
-    opcounting = write_setup_code(sf, filename, define_thetas=False)
+    write_setup_code(sf, filename, define_thetas=False)
 
     additional_arguments = [i.split()[-1] for i in sf.interface.signature_args]
     additional_arguments = ', '.join(additional_arguments)
@@ -228,6 +229,8 @@ def generate_standalone_code_google_benchmark(sf, filename):
                       "\n",
                       "BENCHMARK_MAIN();"
                       ])
+
+    # Maybe turn opcounting on again
     set_option("opcounter", opcounting)
 
 
@@ -239,6 +242,10 @@ def generate_standalone_code(sf, filename):
     knl = realize_sumfact_kernel_function(sf)
     first_line = knl.member.lines[0]
 
+    # Turn off opcounting
+    opcounting = get_option("opcounter")
+    set_option("opcounter", False)
+
     with open(filename, "w") as f:
         f.writelines(["// {}".format(first_line),
                       "\n",
@@ -258,7 +265,7 @@ def generate_standalone_code(sf, filename):
                       "{\n",
                       ])
 
-    opcounting = write_setup_code(sf, filename)
+    write_setup_code(sf, filename)
 
     # Write measurement
     with open(filename, "a") as f:
@@ -284,7 +291,9 @@ def generate_standalone_code(sf, filename):
                       "  std::cout << accum;\n",
                       "}\n",
                       ])
-        set_option("opcounter", opcounting)
+
+    # Maybe turn opcounting on again
+    set_option("opcounter", opcounting)
 
 
 def autotune_realization(sf):