#!/usr/bin/env python
"""Benchmark TODO."""

from __future__ import print_function
import timeit

NAME = "TODO"
REPEATS = 3
ITERATIONS = 1000000


def print_version():
    """Print the TAP version."""
    print("TAP version 13")


def print_summary(total, passing):
    """Print the benchmark summary.

    # Arguments

    * `total`: total number of tests
    * `passing`: number of passing tests

    """
    print("#")
    print("1.." + str(total))  # TAP plan
    print("# total " + str(total))
    print("# pass  " + str(passing))
    print("#")
    print("# ok")


def print_results(elapsed):
    """Print benchmark results.

    # Arguments

    * `elapsed`: elapsed time (in seconds)

    # Examples

    ``` python
    python> print_results(0.131009101868)
    ```
    """
    rate = ITERATIONS / elapsed

    print("  ---")
    print("  iterations: " + str(ITERATIONS))
    print("  elapsed: " + str(elapsed))
    print("  rate: " + str(rate))
    print("  ...")


def benchmark():
    """Run the benchmark and print benchmark results."""
    setup = "from math import TODO; from random import random;"
    stmt = "y = TODO(random())"

    t = timeit.Timer(stmt, setup=setup)

    print_version()

    for i in xrange(REPEATS):
        print("# python::" + NAME)
        elapsed = t.timeit(number=ITERATIONS)
        print_results(elapsed)
        print("ok " + str(i+1) + " benchmark finished")

    print_summary(REPEATS, REPEATS)


def main():
    """Run the benchmark."""
    benchmark()


if __name__ == "__main__":
    main()

@@ -138,16 +138,85 @@
 1000000%0A
+COUNT = %5B0%5D  # use a list to allow modification within nested scopes%0A
 %0A%0Adef pr
@@ -643,32 +643,44 @@
 f print_results(
+iterations, 
 elapsed):%0A    %22%22
@@ -719,24 +719,65 @@
  Arguments%0A%0A
+    * %60iterations%60: number of iterations%0A
     * %60elaps
@@ -865,16 +865,24 @@
 results(
+100000, 
 0.131009
@@ -916,26 +916,26 @@
  rate = 
-ITERATIONS
+iterations
  / elaps
@@ -990,26 +990,26 @@
 %22 + str(
-ITERATIONS
+iterations
 ))%0A    p
@@ -1112,16 +1112,45 @@
 nchmark(
+name, setup, stmt, iterations
 ):%0A    %22
@@ -1201,48 +1201,241 @@
 lts.
-%22%22%22
+%0A
 %0A    
-setup = %22from math import TODO; 
+# Arguments%0A%0A    * %60name%60: benchmark name (suffix)%0A    * %60setup%60: benchmark setup%0A    * %60stmt%60: statement to benchmark%0A    * %60iterations%60: number of iterations%0A%0A    # Examples%0A%0A    %60%60%60 python%0A    python%3E benchmark(%22::random%22, %22
 from
@@ -1461,38 +1461,23 @@
 om;%22
-%0A    stmt =
+,
  %22y = 
-TODO(
 random()
 )%22%0A%0A
@@ -1476,11 +1476,35 @@
 om()
-)%22%0A
+%22, 1000000)%0A    %60%60%60%0A    %22%22%22
 %0A   
@@ -1570,32 +1570,35 @@
     
-for i in xrange(
+i = 0%0A    while i %3C 
 REPEATS
-)
 :%0A  
@@ -1628,17 +1628,46 @@
 %22 + NAME
-)
+ + name)%0A        COUNT%5B0%5D += 1
 %0A       
@@ -1693,26 +1693,26 @@
 (number=
-ITERATIONS
+iterations
 )%0A      
@@ -1727,16 +1727,28 @@
 results(
+iterations, 
 elapsed)
@@ -1778,11 +1778,16 @@
 str(
-i+1
+COUNT%5B0%5D
 ) + 
@@ -1813,44 +1813,22 @@
 d%22)%0A
-%0A
     
-print_summary(REPEATS, REPEATS)
+    i += 1
 %0A%0A%0Ad
@@ -1862,16 +1862,17 @@
 enchmark
+s
 .%22%22%22%0A   
@@ -1872,26 +1872,209 @@
 %22%22%22%0A    
-benchmark(
+name = %22:TODO%22%0A    setup = %22from math import TODO; from random import random;%22%0A    stmt = %22y = TODO(random())%22%0A    benchmark(name, setup, stmt, ITERATIONS)%0A%0A    print_summary(COUNT%5B0%5D, COUNT%5B0%5D
 )%0A%0A%0Aif _

Refactor snippet to facilitate writing multiple benchmarks