Power math: teste de vitesse entiers vs reels , class vs struct

Soyez le premier à donner votre avis sur cette source.

Snippet vu 3 453 fois - Téléchargée 22 fois

Contenu du snippet

Souvent la question de la rapidité de traitement se pose et il faut faire des choix avant de commencer une application.

Dans mon cas j'ai eu besoin de tester la vitesse de différents types de variables préalablement à la programmation d'une application temps réel en 4D. Je souhaitais également vérifier si l'emploi de struct ou class ne pénalisait pas les performances.

test pour:

Les entiers 8, 16, 32 et 64 bits.
Les réels 32, 64 et 80 bits.

I/O dans un tableau, global, local, class et struct.

Source / Exemple :


//******************
//*** POWER MATH ***
//******************

//Version 1.0

//Developed with Dev C++

//Programme d'evaluation de la vitesse de calcul
//Pour differents types de variables
//Affectation & operateurs 
//Boucle de 1 milliard
//Test de vitesse autres fonctions

//By Alain Galiani
//Geek Star Wars 1983 
//Alias Dedalusman
//nageondu@yahoo.fr

//Start          29 12 2009
//Finished       00 00 2012
//Last updat     05 03 2010

//History:

/*

Servira pour tester Geek Star Wars 1983

05 03 2010 test des tableaux global local class et struct

  • /
#include<iostream.h> #include<windows.h> //definition des types typedef unsigned char cop; typedef signed char co; typedef unsigned short m2op; typedef signed short m2o; typedef unsigned long lm4op; typedef signed long lm4o; typedef unsigned long long llm8op; typedef signed long long llm8o; typedef float flo4; typedef double flo8; typedef long double flo10; //*** class *** class class_objet_num { public: //constructeur et destructeur inline class_objet_num(){} ~class_objet_num(){} flo8 tab_sommets[1000];//x }; class class_objet_4d { public: //constructeur et destructeur inline class_objet_4d(){} ~class_objet_4d(){} class_objet_num numerique; }; //*** struct *** struct struct_objet_num { public: flo8 tab_sommets[1000];//x }; class struct_objet_4d { public: struct_objet_num numerique; }; flo8 global_tab[1000]; flo8 gl1,gl2,gl3,gl4; int main() { flo8 local_tab[1000]; flo8 lo1,lo2,lo3,lo4; class_objet_4d univers[10];//univers pour 10 objets 4D struct_objet_4d uni[10];//univers pour 10 objets 4D //definition du ptr univers class_objet_4d*ptr_univers; ptr_univers=&univers[0]; //definition du ptr univers struct_objet_4d*ptr_uni; ptr_uni=&uni[0]; char nom[256]; //****************************************** //*** Declaration de variables entieres *** //****************************************** //variable -128 a 127 co a1,a2,a3,a4,a5,a6,a7; //variable -32768 a 32767 m2o b1,b2,b3,b4,b5,b6,b7; //variable -2147483648 a 2147483647 lm4o c1,c2,c3,c4,c5,c6,c7; //variable -9223372036854775808 a 9223372036854775807 llm8o d1,d2,d3,d4,d5,d6,d7; //***************************************** //*** Declaration de variables reelles *** //***************************************** //reel 32 bit flo4 e1,e2,e3,e4,e5,e6,e7; //reel 64 bit flo8 f1,f2,f3,f4,f5,f6,f7; //reel 80 bit flo10 g1,g2,g3,g4,g5,g6,g7; //variables divers lm4op nb,nbb; flo4 temcal; lm4o temdeb; flo4 tab_v[90][10]; for(nb=1;nb<90;nb++) { for(nbb=1;nbb<10;nbb++) { tab_v[nb][nbb]=1234.56; } } cout<<"******************\n"; cout<<"*** POWER MATH ***\n"; cout<<"******************\n"; //goto test; //test: tout: //*********************** //*** VITESSE ENTIERS *** //*********************** cout<<"\n*** ENTIERS ***\n\n"; //teste de vitesse ************************************ temdeb=GetTickCount(); a2=45; a3=-100; for(nb=0;nb<1000000000;nb++) { a1=a2+a3; } temcal=(float)(GetTickCount()-temdeb)/1000.; cout<<"a1=a2+a3 "<<int(a1)<<" entier 8 bit 1000000000 boucles = "<<temcal<<"\n"; tab_v[1][1]=temcal; //teste de vitesse ************************************ temdeb=GetTickCount(); b2=21024; b3=-10270; for(nb=0;nb<1000000000;nb++) { b1=b2+b3; } temcal=(float)(GetTickCount()-temdeb)/1000.; cout<<"b1=b2+b3 "<<b1<<" entier 16 bit 1000000000 boucles = "<<temcal<<"\n"; tab_v[1][2]=temcal; //teste de vitesse ************************************ temdeb=GetTickCount(); c2=2007483647; c3=-1588796587; for(nb=0;nb<1000000000;nb++) { c1=c2+c3; } temcal=(float)(GetTickCount()-temdeb)/1000.; cout<<"c1=c2+c3 "<<c1<<" entier 32 bit 1000000000 boucles = "<<temcal<<"\n"; tab_v[1][3]=temcal; //teste de vitesse ************************************ temdeb=GetTickCount(); d2=8223372036854775808LL; d3=-1223372036854775807LL; for(nb=0;nb<1000000000;nb++) { d1=d2+d3; } temcal=(float)(GetTickCount()-temdeb)/1000.; cout<<"d1=d2+d3 "<<d1<<" entier 64 bit 1000000000 boucles = "<<temcal<<"\n"; tab_v[1][4]=temcal; //teste de vitesse ************************************ temdeb=GetTickCount(); a2=35; a3=-3; for(nb=0;nb<1000000000;nb++) { a1=a2*a3; } temcal=(float)(GetTickCount()-temdeb)/1000.; cout<<"a1=a2*a3 "<<int(a1)<<" entier 8 bit 1000000000 boucles = "<<temcal<<"\n"; tab_v[2][1]=temcal; //teste de vitesse ************************************ temdeb=GetTickCount(); b2=2102; b3=-11; for(nb=0;nb<1000000000;nb++) { b1=b2*b3; } temcal=(float)(GetTickCount()-temdeb)/1000.; cout<<"b1=b2*b3 "<<b1<<" entier 16 bit 1000000000 boucles = "<<temcal<<"\n"; tab_v[2][2]=temcal; //teste de vitesse ************************************ temdeb=GetTickCount(); c2=2007483; c3=-658; for(nb=0;nb<1000000000;nb++) { c1=c2*c3; } temcal=(float)(GetTickCount()-temdeb)/1000.; cout<<"c1=c2*c3 "<<c1<<" entier 32 bit 1000000000 boucles = "<<temcal<<"\n"; tab_v[2][3]=temcal; //teste de vitesse ************************************ temdeb=GetTickCount(); d2=12345678901234LL; d3=-12345LL; for(nb=0;nb<1000000000;nb++) { d1=d2*d3; } temcal=(float)(GetTickCount()-temdeb)/1000.; cout<<"d1=d2*d3 "<<d1<<" entier 64 bit 1000000000 boucles = "<<temcal<<"\n"; tab_v[2][4]=temcal; //teste de vitesse ************************************ temdeb=GetTickCount(); a2=123; a3=-13; for(nb=0;nb<1000000000;nb++) { a1=a2/a3; } temcal=(float)(GetTickCount()-temdeb)/1000.; cout<<"a1=a2/a3 "<<int(a1)<<" entier 8 bit 1000000000 boucles = "<<temcal<<"\n"; tab_v[3][1]=temcal; //teste de vitesse ************************************ temdeb=GetTickCount(); b2=21024; b3=-123; for(nb=0;nb<1000000000;nb++) { b1=b2/b3; } temcal=(float)(GetTickCount()-temdeb)/1000.; cout<<"b1=b2/b3 "<<b1<<" entier 16 bit 1000000000 boucles = "<<temcal<<"\n"; tab_v[3][2]=temcal; //teste de vitesse ************************************ temdeb=GetTickCount(); c2=2007483647; c3=-15887; for(nb=0;nb<1000000000;nb++) { c1=c2/c3; } temcal=(float)(GetTickCount()-temdeb)/1000.; cout<<"c1=c2/c3 "<<c1<<" entier 32 bit 1000000000 boucles = "<<temcal<<"\n"; tab_v[3][3]=temcal; //teste de vitesse ************************************ temdeb=GetTickCount(); d2=8223372036854775808LL; d3=-1223372036LL; for(nb=0;nb<1000000000;nb++) { d1=d2/d3; } temcal=(float)(GetTickCount()-temdeb)/1000.; cout<<"d1=d2/d3 "<<d1<<" entier 64 bit 1000000000 boucles = "<<temcal<<"\n"; tab_v[3][4]=temcal; tab_v[4][1]=tab_v[1][1]+tab_v[2][1]+tab_v[3][1]; tab_v[4][2]=tab_v[1][2]+tab_v[2][2]+tab_v[3][2]; tab_v[4][3]=tab_v[1][3]+tab_v[2][3]+tab_v[3][3]; tab_v[4][4]=tab_v[1][4]+tab_v[2][4]+tab_v[3][4]; //teste de vitesse ************************************ temdeb=GetTickCount(); a2=123; b3=4; for(nb=0;nb<1000000000;nb++) { a1=a2>>b3; } temcal=(float)(GetTickCount()-temdeb)/1000.; cout<<"a1=a2>>b3 "<<int(a1)<<" entier 8 bit 1000000000 boucles = "<<temcal<<"\n"; tab_v[5][1]=temcal; //teste de vitesse ************************************ temdeb=GetTickCount(); b2=-31123; b3=8; for(nb=0;nb<1000000000;nb++) { b1=b2>>b3; } temcal=(float)(GetTickCount()-temdeb)/1000.; cout<<"b1=b2>>b3 "<<b1<<" entier 16 bit 1000000000 boucles = "<<temcal<<"\n"; tab_v[5][2]=temcal; //teste de vitesse ************************************ temdeb=GetTickCount(); c2=2147483640; b3=10; for(nb=0;nb<1000000000;nb++) { c1=c2>>b3; } temcal=(float)(GetTickCount()-temdeb)/1000.; cout<<"c1=c2>>b3 "<<c1<<" entier 32 bit 1000000000 boucles = "<<temcal<<"\n"; tab_v[5][3]=temcal; //teste de vitesse ************************************ temdeb=GetTickCount(); d2=-8223372036854775808LL; b3=10; for(nb=0;nb<1000000000;nb++) { d1=d2>>b3; } temcal=(float)(GetTickCount()-temdeb)/1000.; cout<<"d1=d2>>b3 "<<d1<<" entier 64 bit 1000000000 boucles = "<<temcal<<"\n"; tab_v[5][4]=temcal; tab_v[6][1]=tab_v[5][1]+tab_v[4][1]; tab_v[6][2]=tab_v[5][2]+tab_v[4][2]; tab_v[6][3]=tab_v[5][3]+tab_v[4][3]; tab_v[6][4]=tab_v[5][4]+tab_v[4][4]; reels: //********************* //*** VITESSE REELS *** //********************* cout<<"\n*** REELS ***\n\n"; //teste de vitesse ************************************ temdeb=GetTickCount(); e2=12345.6789; e3=-9876.54321; for(nb=0;nb<1000000000;nb++) { e1=e2+e3; } temcal=(float)(GetTickCount()-temdeb)/1000.; cout<<"e1=e2+e3 "<<e1<<" reel 32 bit 1000000000 boucles = "<<temcal<<"\n"; tab_v[11][1]=temcal; //teste de vitesse ************************************ temdeb=GetTickCount(); f2=123456789.1234567; f3=-9876543.7654321; for(nb=0;nb<1000000000;nb++) { f1=f2+f3; } temcal=(float)(GetTickCount()-temdeb)/1000.; cout<<"f1=f2+f3 "<<f1<<" reel 64 bit 1000000000 boucles = "<<temcal<<"\n"; tab_v[11][2]=temcal; //teste de vitesse ************************************ temdeb=GetTickCount(); g2=1234567890123.123456789012; g3=-9876543210.987654321; for(nb=0;nb<1000000000;nb++) { g1=g2+g3; } temcal=(float)(GetTickCount()-temdeb)/1000.; cout<<"g1=g2+g3 "<<g1<<" reel 80 bit 1000000000 boucles = "<<temcal<<"\n"; tab_v[11][3]=temcal; //teste de vitesse ************************************ temdeb=GetTickCount(); e2=12345.6789; e3=-98.54321; for(nb=0;nb<1000000000;nb++) { e1=e2*e3; } temcal=(float)(GetTickCount()-temdeb)/1000.; cout<<"e1=e2*e3 "<<e1<<" reel 32 bit 1000000000 boucles = "<<temcal<<"\n"; tab_v[12][1]=temcal; //teste de vitesse ************************************ temdeb=GetTickCount(); f2=1234567.1234567; f3=-987.7654321; for(nb=0;nb<1000000000;nb++) { f1=f2*f3; } temcal=(float)(GetTickCount()-temdeb)/1000.; cout<<"f1=f2*f3 "<<f1<<" reel 64 bit 1000000000 boucles = "<<temcal<<"\n"; tab_v[12][2]=temcal; //teste de vitesse ************************************ temdeb=GetTickCount(); g2=1234567123.123456789012; g3=-98765.987654321; for(nb=0;nb<1000000000;nb++) { g1=g2*g3; } temcal=(float)(GetTickCount()-temdeb)/1000.; cout<<"g1=g2*g3 "<<g1<<" reel 80 bit 1000000000 boucles = "<<temcal<<"\n"; tab_v[12][3]=temcal; //teste de vitesse ************************************ temdeb=GetTickCount(); e2=12345.6789; e3=-98.54321; for(nb=0;nb<1000000000;nb++) { e1=e2/e3; } temcal=(float)(GetTickCount()-temdeb)/1000.; cout<<"e1=e2/e3 "<<e1<<" reel 32 bit 1000000000 boucles = "<<temcal<<"\n"; tab_v[13][1]=temcal; //teste de vitesse ************************************ temdeb=GetTickCount(); f2=1234567.1234567; f3=-987.7654321; for(nb=0;nb<1000000000;nb++) { f1=f2/f3; } temcal=(float)(GetTickCount()-temdeb)/1000.; cout<<"f1=f2/f3 "<<f1<<" reel 64 bit 1000000000 boucles = "<<temcal<<"\n"; tab_v[13][2]=temcal; //teste de vitesse ************************************ temdeb=GetTickCount(); g2=1234567123.123456789012; g3=-98765.987654321; for(nb=0;nb<1000000000;nb++) { g1=g2/g3; } temcal=(float)(GetTickCount()-temdeb)/1000.; cout<<"g1=g2/g3 "<<g1<<" reel 80 bit 1000000000 boucles = "<<temcal<<"\n"; tab_v[13][3]=temcal; //test: tableaux: //****************************************************** //*** VITESSE I/O TABLEAUX GLOBAL LOCAL CLASS STRUCT *** //****************************************************** cout<<"\n*** VITESSE I/O TABLEAUX GLOBAL LOCAL CLASS STRUCT ***\n\n"; //teste de vitesse ************************************ temdeb=GetTickCount(); gl1=123456789; for(nb=0;nb<1000000;nb++) { for(int nb2=0;nb2<1000;nb2++) { global_tab[nb2]=gl1; gl2=global_tab[nb2]; } } temcal=(float)(GetTickCount()-temdeb)/1000.; cout<<"flo8 global_tab[1000] entree et sortie 1000000 boucles = "<<temcal<<"\n"; //teste de vitesse ************************************ temdeb=GetTickCount(); lo1=123456789; for(nb=0;nb<1000000;nb++) { for(int nb2=0;nb2<1000;nb2++) { local_tab[nb2]=lo1; lo2=local_tab[nb2]; } } temcal=(float)(GetTickCount()-temdeb)/1000.; cout<<"flo8 local_tab[1000] entree et sortie 1000000 boucles = "<<temcal<<"\n"; //teste de vitesse ************************************ temdeb=GetTickCount(); lo1=123456789; for(nb=0;nb<1000000;nb++) { for(int nb2=0;nb2<1000;nb2++) { ptr_univers->numerique.tab_sommets[nb2]=lo1; lo2=ptr_univers->numerique.tab_sommets[nb2]; } } temcal=(float)(GetTickCount()-temdeb)/1000.; cout<<"flo8 CLASS univers[0].numerique.tab_sommets[1000] I/O 1000000 bcl = "<<temcal<<"\n"; //teste de vitesse ************************************ temdeb=GetTickCount(); lo1=123456789; for(nb=0;nb<1000000;nb++) { for(int nb2=0;nb2<1000;nb2++) { ptr_uni->numerique.tab_sommets[nb2]=lo1; lo2=ptr_uni->numerique.tab_sommets[nb2]; } } temcal=(float)(GetTickCount()-temdeb)/1000.; cout<<"flo8 STRUCT uni[0].numerique.tab_sommets[1000] I/O 1000000 bcl = "<<temcal<<"\n"; tab_v[14][1]=tab_v[11][1]+tab_v[12][1]+tab_v[13][1]; tab_v[14][2]=tab_v[11][2]+tab_v[12][2]+tab_v[13][2]; tab_v[14][3]=tab_v[11][3]+tab_v[12][3]+tab_v[13][3]; //test: cout<<"\n\n\t\t*** TABLEAU ENTIERS ***\n\n"; cout<<"\t\t8 bit\t16 bit\t32 bit\t64 bit\n\n"; for(nb=1;nb<7;nb++) { if(nb==1)cout<<"\t+\t"; if(nb==2)cout<<"\t*\t"; if(nb==3)cout<<"\t\\\t"; if(nb==4)cout<<"\n\tS_TOT\t"; if(nb==5)cout<<"\n\t>>\t"; if(nb==6)cout<<"\n\tTOTAL\t"; for(nbb=1;nbb<5;nbb++) { cout<<tab_v[nb][nbb]<<"\t"; } cout<<"\n"; } cout<<"\n\n\t\t*** TABLEAU REELS ***\n\n"; cout<<"\t\t32 bit\t64 bit\t80 bit\n\n"; for(nb=11;nb<15;nb++) { if(nb==11)cout<<"\t+\t"; if(nb==12)cout<<"\t*\t"; if(nb==13)cout<<"\t\\\t"; if(nb==14)cout<<"\n\tTOTAL\t"; for(nbb=1;nbb<4;nbb++) { cout<<tab_v[nb][nbb]<<"\t"; } cout<<"\n"; } //test: cout<<"\n\nFin des calculs, entrer une commande: \"tout\" \"reels\" ou \"tableaux\" "; cin>>nom; if(!strcmp(nom,"tout")) { goto tout; } else { if(!strcmp(nom,"reels")) { goto reels; } else { if(!strcmp(nom,"tableaux")) { goto tableaux; } } } }

Conclusion :


Sauf erreur de programmation de ma part, c'est extrêmement rapide.
D'autant que mes premiers ordinateurs furent un Atari 800XL puis un 520STF.

A voir également

Ajouter un commentaire Commentaires
dedalusman Messages postés 4 Date d'inscription vendredi 5 mars 2010 Statut Membre Dernière intervention 1 juin 2016
7 mars 2010 à 11:57
Merci pour ce commentaire car je n'avait pas pensé à ce type d'optimisation des compilateurs modernes.

J'utilise Dev C++. Il est possible de voir que le temps d'exécution est en rapport avec le nombre de boucle et le type d'opération. Donc la boucle est codée. (Voir la capture écran)

La partie entière du temps indiqué est en seconde.

for(nb=0;nb<1000000000;nb++)
{
e1=e2/e3;
}

se fait en 46,953 secondes

J'obtiens (voir la capture écran) la même valeur de temps que pour:

for(nb=0;nb<1000000000;nb++)
{
f1=f2/f3;
}

Peut-être qu'un OS multitâche comme Win XP ne permet pas des mesures précises au millième de seconde étant donnée qu'il y a un basculement perpétuel entre les tâches ?
BruNews Messages postés 21041 Date d'inscription jeudi 23 janvier 2003 Statut Modérateur Dernière intervention 21 août 2019
6 mars 2010 à 18:58
for(nb=0;nb<1000000000;nb++)
{
e1=e2/e3;
}

Tu es certain que le compilo a généré le code de la boucle ???
Un compilo normal moderne vire la boucle en voyant qu'une seule opération donne le résultat. Il faut sortir le listing ASM pour etre certain.

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.