Skip to content

Instantly share code, notes, and snippets.

@carsongee
Last active August 29, 2015 14:24
Show Gist options
  • Select an option

  • Save carsongee/d6a34da47346d082f158 to your computer and use it in GitHub Desktop.

Select an option

Save carsongee/d6a34da47346d082f158 to your computer and use it in GitHub Desktop.

Revisions

  1. carsongee renamed this gist Jul 14, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. carsongee created this gist Jul 14, 2015.
    20 changes: 20 additions & 0 deletions n_vs_for.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    def x_in_loop():
    x = range(500)
    for i in x:
    if x == 253:
    return True
    return False

    def x_in_range():
    x = range(500)
    return 253 in x


    if __name__=='__main__':
    from timeit import Timer
    timer = Timer("x_in_range()", "from __main__ import x_in_range")
    print("Timeit for `in`")
    print(timer.timeit())
    timer = Timer("x_in_loop()", "from __main__ import x_in_loop")
    print("Timeit for `for` loop")
    print(timer.timeit())