====== Différences ====== Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédente | ||
|
python:benchmark [2015/03/06 12:09] nliautaud |
python:benchmark [2015/03/06 18:57] (Version actuelle) nliautaud |
||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| ======Benchmark====== | ======Benchmark====== | ||
| - | Tous les résultats sont indiqués en secondes. | + | Les benchmarks concernent [[https://docs.python.org/3|Python 3]] et les timings sont généralement calculés via ''[[https://docs.python.org/3/library/timeit.html|timeit]]'', en x64 sous Windows. |
| - | La fonction something() consiste généralement au choix d'un nombre flottant aléatoire : | + | Le but n'est pas d'invalider un élément du langage, puisqu'évidemment chaque chose comparée a sa raison d'être, et peut potentiellement se révéler plus adapté dans un contexte différent. Ce genre de comparaison est purement théorique, et permet d'étendre sa compréhention du langage et de ses nombreuses spécificités. |
| - | <Code python> | + | Et finalement, bien que minimes pris indépendemment, la prise en compte conjointe de ces faces à faces peut avoir un effet notable sur les temps d'exécution. Sans oublier bien sûr que //l'optimisation prématurée est la racine de tous les maux//. |
| - | from random import uniform | + | |
| - | def something(): | + | |
| - | return uniform(-100000,100000) | + | |
| - | </Code> | + | |
| - | Bien que ne semblant avoir indépendemment que peu d'effets, l'application de l'ensemble de ces astuces dans un programme peut avoir un effet drastique sur son temps d'exécution. | ||
| - | ====Appel de fonction==== | + | **a faire :** tri de tableaux/listes et dictionnaires, concaténation et vitesse des opérateurs mathématiques et de comparaison. |
| - | + | ||
| - | Appel dans une boucle : | + | |
| - | <Code python> | + | |
| - | def doit(): | + | |
| - | something() | + | |
| - | + | ||
| - | rng = range(x) | + | |
| - | for i in rng: | + | |
| - | doit() | + | |
| - | </Code> | + | |
| - | + | ||
| - | Boucle dans la fonction : | + | |
| - | <Code python> | + | |
| - | def doit(x): | + | |
| - | rng = range(x) | + | |
| - | for i in rng: | + | |
| - | something() | + | |
| - | + | ||
| - | doit(x) | + | |
| - | </Code> | + | |
| - | + | ||
| - | ===Bench=== | + | |
| - | + | ||
| - | 1.000.000 de tours : | + | |
| - | + | ||
| - | - Appel dans une boucle : 1.341 | + | |
| - | - Boucle dans la fonction : 1.164 | + | |
| - | + | ||
| - | <barchart>0|50x20|100|Dans:100,Hors:70</barchart> | + | |
| - | + | ||
| - | ===Conclusion=== | + | |
| - | + | ||
| - | L'appel à une fonction prends du temps, il est donc bien plus rapide de mettre la boucle à l'intérieur de la fonction. | + | |
| - | + | ||
| - | =====Imports===== | + | |
| - | + | ||
| - | ====From et import==== | + | |
| - | + | ||
| - | From : | + | |
| - | <Code python> | + | |
| - | rng = range(x) | + | |
| - | for i in rng: | + | |
| - | from math import * | + | |
| - | something() | + | |
| - | </Code> | + | |
| - | + | ||
| - | <note important>Cette syntaxe (//import *//) n'est correcte qu'au premier niveau du programme. J'ai ici passé outre les alertes afin d'en calculer la vitesse.</note> | + | |
| - | + | ||
| - | From limité : | + | |
| - | <Code python> | + | |
| - | rng = range(x) | + | |
| - | for i in rng: | + | |
| - | from math import pi | + | |
| - | something() | + | |
| - | </Code> | + | |
| - | + | ||
| - | Import : | + | |
| - | <Code python> | + | |
| - | rng = range(x) | + | |
| - | for i in rng: | + | |
| - | import math | + | |
| - | something() | + | |
| - | </Code> | + | |
| - | + | ||
| - | ===Bench=== | + | |
| - | + | ||
| - | 1.000.000 de tours : | + | |
| - | + | ||
| - | - From : 12.56 | + | |
| - | - From limité : 2.99 | + | |
| - | - Import : 0.99 | + | |
| - | + | ||
| - | <barchart>0|50x20|13|From:12.56,From2:2.99,Import:0.99</barchart> | + | |
| - | + | ||
| - | ===Conclusion=== | + | |
| - | + | ||
| - | En cas de besoin ou de volonté d'utiliser la syntaxe //from//, l'import d'éléments précis dans le module s'avère cinq à six fois plus véloce que l'import complet dudit module. | + | |
| - | + | ||
| - | L'utilisation de la syntaxe //import// seule reste quand à elle de 12 à 13 fois plus efficiente. | + | |
| - | + | ||
| - | ====Réimporter==== | + | |
| - | + | ||
| - | Import multiple : | + | |
| - | <Code python> | + | |
| - | rng = range(x) | + | |
| - | for i in rng: | + | |
| - | import math | + | |
| - | something() | + | |
| - | </Code> | + | |
| - | + | ||
| - | Import unique : | + | |
| - | <Code python> | + | |
| - | import math | + | |
| - | rng = range(x) | + | |
| - | for i in rng: | + | |
| - | something() | + | |
| - | </Code> | + | |
| - | + | ||
| - | ===Bench=== | + | |
| - | + | ||
| - | 1.000.000 de tours : | + | |
| - | + | ||
| - | - Import multiple : 9.95 | + | |
| - | - Import unique : 0.79 | + | |
| - | + | ||
| - | <barchart>0|50x20|10|Multiple:9.95,Unique:0.79</barchart> | + | |
| - | + | ||
| - | ===Conclusion=== | + | |
| - | + | ||
| - | Un import peut-être très long (cela dépend de la taille du module), il faut donc toujours éviter d'importer plusieurs fois la même chose, en faisant par exemple : | + | |
| - | + | ||
| - | <Code python> | + | |
| - | math = None | + | |
| - | rng = range(x) | + | |
| - | for i in rng: | + | |
| - | if math is None: | + | |
| - | import math | + | |
| - | something() | + | |
| - | </Code> | + | |
| - | + | ||
| - | + | ||
| - | =====Opérateurs===== | + | |
| - | + | ||
| - | ====Division et multiplication==== | + | |
| - | + | ||
| - | Division : | + | |
| - | <Code python> | + | |
| - | rng = range(x) | + | |
| - | for i in rng: | + | |
| - | 123456.789 / 4 | + | |
| - | </Code> | + | |
| - | + | ||
| - | Multiplication : | + | |
| - | <Code python> | + | |
| - | rng = range(x) | + | |
| - | for i in rng: | + | |
| - | 123456.789 * 0.25 | + | |
| - | </Code> | + | |
| - | + | ||
| - | Le résultat de ces calculs est le même. | + | |
| - | + | ||
| - | ===Bench=== | + | |
| - | + | ||
| - | Calcul effectué 1.000.000 de fois : | + | |
| - | + | ||
| - | - Division : 0.121 | + | |
| - | - Multiplication : 0.047 | + | |
| - | + | ||
| - | <barchart>0|50x20|12.1|Division:12.1,Multiplication:4.7</barchart> | + | |
| - | + | ||
| - | ===Conclusion=== | + | |
| - | + | ||
| - | La multiplication est de deux à quatre fois plus rapide que la division. | + | |
| - | + | ||
| - | ====Incrémentation et addition==== | + | |
| - | + | ||
| - | Addition : | + | |
| - | <Code python> | + | |
| - | rng = range(x) | + | |
| - | for i in rng: | + | |
| - | i = i + 1 | + | |
| - | </Code> | + | |
| - | + | ||
| - | Incrémentation : | + | |
| - | <Code python> | + | |
| - | rng = range(x) | + | |
| - | for i in rng: | + | |
| - | i += 1 | + | |
| - | </Code> | + | |
| - | + | ||
| - | ===Bench=== | + | |
| - | + | ||
| - | Calcul effectué 1.000.000 de fois : | + | |
| - | + | ||
| - | - Addition : 0.140 | + | |
| - | - Incrémentation : 0.117 | + | |
| - | + | ||
| - | <barchart>0|50x20|14|Addition:14,Incrémentation:11.7</barchart> | + | |
| - | + | ||
| - | ===Conclusion=== | + | |
| - | + | ||
| - | L'incrémentation est légèrement plus rapide que l'addition. | + | |
| - | + | ||
| - | =====Classes==== | + | |
| - | + | ||
| - | ====Accès aux méthodes==== | + | |
| - | + | ||
| - | Point dans la boucle : | + | |
| - | <Code python> | + | |
| - | arr = [] | + | |
| - | rng = range(x) | + | |
| - | for i in rng: | + | |
| - | arr.append(something()) | + | |
| - | </Code> | + | |
| - | + | ||
| - | Point hors de la boucle : | + | |
| - | <Code python> | + | |
| - | arr = [] | + | |
| - | append = arr.append | + | |
| - | rng = range(x) | + | |
| - | for i in rng: | + | |
| - | append(something()) | + | |
| - | </Code> | + | |
| - | + | ||
| - | ===Bench=== | + | |
| - | + | ||
| - | Calcul effectué 1.000.000 de fois : | + | |
| - | + | ||
| - | - Point dans la boucle : 1.307 | + | |
| - | - Point hors de la boucle : 1.157 | + | |
| - | + | ||
| - | <barchart>0|50x20|1.3|Dans:1.3,Hors:1.1</barchart> | + | |
| - | + | ||
| - | ===Conclusion=== | + | |
| - | + | ||
| - | La référence à une méthode de classe (syntaxe du point), au lieu d'être recalculée à chaque tour de boucle, peut être une fois pour toutes stockée dans une variable, accélérant notablement les calculs. | + | |
| - | + | ||
| - | =====WIP===== | + | |
| - | En cours d'écriture. | + | |
| - | + | ||
| - | A venir : benchmark code, parcourt et tri de tableaux/listes et dictionnaires, concaténation et vitesse des opérateurs mathématiques et de comparaison. | + | |