Benchmark Serial Arduino
De Wiki_du_Réseau_des_Electroniciens_du_CNRS
Aller à la navigationAller à la recherche
/*
Objectif
Montrer l'effet de l'usage abusif
de Serial sur les performances.
Le programme exécute une opération idiote d'incrémentation
d'une variable et son affichage.
Nous mesurons le temps d'exécution avec l'usage de Serial.print
et sans.
Les temps et le rapport entre les deux sont affichés à la fin.
Auteur: MarcDexet (at) gmail.com
23 janvier 2015
Licence CC By SA 3
https://creativecommons.org/licenses/by-sa/3.0/legalcode
*/
#define NB_OF_ITERATIONS 1000
byte runOnce = 0; // Flag pour fonctionner 1 fois
void setup() {
Serial.begin(9600);
}
void loop() {
if ( ! runOnce ) {
runOnce = 1;
//---------------------
// Avec sortie SERIE
//---------------------
volatile int k=0;
unsigned long startTs = micros();
for(int i =0; i < NB_OF_ITERATIONS; i++) {
k++;
Serial.print(k);
}
unsigned long testWithSerial = micros() - startTs;
//---------------------
// Sans sortie SERIE
//---------------------
k=0;
startTs = micros();
for(int i =0; i < NB_OF_ITERATIONS; i++) {
k++;
}
unsigned long testWithoutSerial = micros() - startTs;
//- affichage final
Serial.print("\n\n AVEC ");
Serial.print(NB_OF_ITERATIONS);
Serial.print("\nAVEC SORTIE PAR PORT SERIE (microsecond) ");
Serial.println(testWithSerial);
Serial.print("SANS PORT SERIE (microsecond) ");
Serial.println(testWithoutSerial);
Serial.println("RAPPORT :");
Serial.println( ((float) testWithSerial)/((float) testWithoutSerial));
}
}
Voici les résultats sur un CoreI5 8Go de mémoire
AVEC 1000 AVEC SORTIE PAR PORT SERIE (microsecond) 2938100 SANS PORT SERIE (microsecond) 1076 RAPPORT : 2730.58
L'opération est 2730 fois plus lente avec Serial.print que sans