Skip to content

timeit module is too hard to use, and it is not documented well #94335

Closed as not planned
@108806

Description

@108806
def benchmark(func):
    def inner(args):
        print(timeit.timeit('func(args)')) # I dont even know what stmt means
    return inner

@benchmark
def quicksort(array):
    """Sort the array by using quicksort."""

    less = []
    equal = []
    greater = []

    if len(array) > 1:
        pivot = array[0]
        for x in array:
            if x < pivot:
                less.append(x)
            elif x == pivot:
                equal.append(x)
            elif x > pivot:
                greater.append(x)

        return sorted(less) + equal + sorted(greater)
    # Note that you want equal ^^^^^ not pivot
    else:
        return array
  File "C:\Users\ansga\PycharmProjects\Benchmark\main.py", line 75, in <module>
    quicksort(test)
  File "C:\Users\ansga\PycharmProjects\Benchmark\main.py", line 19, in inner
    print(timeit.timeit(stmt='func(args)'))
  File "C:\Program Files\Python310\lib\timeit.py", line 234, in timeit
    return Timer(stmt, setup, timer, globals).timeit(number)
  File "C:\Program Files\Python310\lib\timeit.py", line 178, in timeit
    timing = self.inner(it, self.timer)
  File "<timeit-src>", line 6, in inner
NameError: name 'func' is not defined

Process finished with exit code 1

I don't know how to use it with my code, docs are full of garbage unpractical and ugly and unclear examples like this one:
timeit.timeit('"-".join(map(str, range(100)))', number=10000) 0.23702679807320237
And i dont fkn know what is going on there and what it has to do with benchmarking the actual function in some idiomatic way.

When I try to do it like: timeit.timeit('func(args)') it will say that func is not declared.
Absolute mess and unintiutive hell.

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dir

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions