Speed performance of a Finite State Machine


To implement a Finite State Machine in C the goto Statement is a very good idea. It is possible to jump between states. And the speed of that kind of automata is amazing. Here is my code:

#include <stdio.h>
 
void main () {
int i;

LABEL1:
printf("label1\n");
goto LABEL2;

LABEL2:
printf("label2\n");
goto LABEL3;

LABEL3:
printf("label3\n");
goto LABEL4;

LABEL4:
printf("label4, %d\n",i);
i++;
goto LABEL1;
}

gcc 2.c -O2
time ./a.out > out.txt
real	0m1.892s
user	0m1.592s
sys	0m0.188s

tail out.txt 
label3
label4, 9859431
label1
label2
label3
label4, 9859432
label1
label2
label3

The code itself is easy, there are 4 state which are ordered in a linear fashion. After a complete cycle a counter is incremented. A speed test gave as a result, that in one second 20,8 Million States of a state-machine can be calculated. Sure, the states itself are doing nothing, but 20 Million is a huge number. That means, an ordinary PC is a little bit too fast for state-machine. The question is, what kind of state-machine can bring up the cpu load to a high level?

Advertisements

Kommentar verfassen

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit Deinem WordPress.com-Konto. Abmelden / Ändern )

Twitter-Bild

Du kommentierst mit Deinem Twitter-Konto. Abmelden / Ändern )

Facebook-Foto

Du kommentierst mit Deinem Facebook-Konto. Abmelden / Ändern )

Google+ Foto

Du kommentierst mit Deinem Google+-Konto. Abmelden / Ändern )

Verbinde mit %s