Files
org-roamings/20220416112952-timethese.org
2022-06-04 12:57:39 +02:00

2.8 KiB

timethese

Introduction

  • Package permettant de mesurer les temps nécessaires à l'exécution de plusieurs méthodes et de les comparer.
  • Approche en 3 étapes :

    • Définition des méthodes à benchmarker,
    • Préparation et exécution des tests (usage de la classe cmpthese),
    • Formattage des résultas (pretty print).

Installation

pip install timethese

Exemple

from timethese import cmpthese, pprint_cmp, timethese

xs = range(10)


# 1. DEFINE FUNCTIONS

def map_hex():
    list(map(hex, xs))


def list_compr_hex():
    list([hex(x) for x in xs])


def map_lambda():
    list(map(lambda x: x + 2, xs))


def map_lambda_fn():
    fn = lambda x: x + 2
    list(map(fn, xs))


def list_compr_nofn():
    list([x + 2 for x in xs])


# 2. FEED THE FUNCTIONS TO CMPTHESE

# AS DICT:

cmp_res_dict = cmpthese(
    10000,
    {
        "map_hex": map_hex,
        "list_compr_hex": list_compr_hex,
        "map_lambda": map_lambda,
        "map_lambda_fn": map_lambda_fn,
        "list_compr_nofn": list_compr_nofn,
    },
    repeat=3,
)


# OR AS LIST:

cmp_res_list = cmpthese(
    10000, [map_hex, list_compr_hex, map_lambda, map_lambda_fn, list_compr_nofn,], repeat=3,
)

# 3. PRETTY PRINT THE RESULTS

print(pprint_cmp(cmp_res_dict))
print(pprint_cmp(cmp_res_list))
                      Rate  list_compr_nofn  map_hex  map_lambda  map_lambda_fn  list_compr_hex
list_compr_nofn  1889389/s                .      21%         42%            45%             68%
        map_hex  1565635/s             -17%        .         18%            21%             40%
     map_lambda  1328914/s             -30%     -15%           .             2%             18%
  map_lambda_fn  1298638/s             -31%     -17%         -2%              .             16%
 list_compr_hex  1121963/s             -41%     -28%        -16%           -14%               .
                        Rate  4.list_compr_nofn  0.map_hex  3.map_lambda_fn  2.map_lambda  1.list_compr_hex
4.list_compr_nofn  1836243/s                  .        17%              37%           38%               73%
        0.map_hex  1568271/s               -15%          .              17%           18%               47%
  3.map_lambda_fn  1336453/s               -27%       -15%                .            0%               26%
     2.map_lambda  1334377/s               -27%       -15%              -0%             .               25%
 1.list_compr_hex  1063984/s               -42%       -32%             -20%          -20%                 .

Références