2022-07-01 / Bartłomiej Kurek
Benchmark-py: for vs while

Problem

Co jest szybsze?

def FOR(limit):
    for i in range(limit):
        pass


def WHILE(limit):
    i = 0
    while i < limit:
        i += 1

Benchmark

--------------------------------------------------------------------------------
→ default
   →              FOR-1000000     1024 2.13316398
   →            WHILE-1000000     1024 5.31997699
================================================================================
RESULTS
================================================================================
GROUP               | CASE                | N_TIMES | TOTAL TIME  | N/SEC       
--------------------------------------------------------------------------------
default             | FOR-1000000         | 1024    | 2.13316398  | 480.04      
default             | WHILE-1000000       | 1024    | 5.31997699  | 192.48      
--------------------------------------------------------------------------------

Wykres