Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
2889 connectés 

  FORUM HardWare.fr
  Programmation
  C++

  [C++]Question optimisation.

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

[C++]Question optimisation.

n°1321651
grostony
Posté le 09-03-2006 à 11:17:49  profilanswer
 

Bonjour,
 
Je souhaiterai savoir, juste pour information, ce qui est le plus rapide entre deux codes :
 
Ayant défini quelque part :

Code :
  1. struct str
  2. {
  3.     [...]
  4.     int nbr;
  5.     [...]
  6. }
  7. int nb;
  8. str structure;


Quel est le plus rapide entre :

Code :
  1. nb = structure.nbr;
  2. if (nb == 0)
  3. {
  4.    ...
  5. }
  6. else if (nb == 1)
  7. {
  8.    ...
  9. }
  10. else if (nb == 2)
  11. {
  12.    ...
  13. }
  14. else
  15. {
  16.    ...
  17. }


et :

Code :
  1. if (structure.nbr == 0)
  2. {
  3.    ...
  4. }
  5. else if (structure.nbr == 1)
  6. {
  7.    ...
  8. }
  9. else if (structure.nbr == 2)
  10. {
  11.    ...
  12. }
  13. else
  14. {
  15.    ...
  16. }


Merci.


Message édité par grostony le 09-03-2006 à 11:23:13
mood
Publicité
Posté le 09-03-2006 à 11:17:49  profilanswer
 

n°1321653
masklinn
í dag viðrar vel til loftárása
Posté le 09-03-2006 à 11:21:46  profilanswer
 

1. C'est probablement la même chose, les déréférencements sont résolus à la compilation
2. Switch-Case, c'est pas fait pour les chiens
3.  

Citation :

Premature optimization is the root of all evil


---------------
Stick a parrot in a Call of Duty lobby, and you're gonna get a racist parrot. — Cody
n°1321658
grostony
Posté le 09-03-2006 à 11:26:49  profilanswer
 

1. Merci, c'est ce qu'il me semblait.
 
2. En fait c'est dans un code qui a déja été fait (et par quelqu'un d'autre :d) et j'ai mis le else pour faire plus simple mais dans le code il y a un "else if (val > 6)" et je ne pense pas que ça soit transformable en Switch-Case.
edit : et encore le gars avait pas mis les else, avec une autre valeur dans le if ça faisait du genre pas du tout optimisé :

Code :
  1. if ((val == 1) && (nb == 0))
  2. {
  3.    ...
  4. }
  5. if ((val == 1) && (nb == 1))
  6. {
  7.    ...
  8. }
  9. if ((val == 1) && (nb == 2))
  10. {
  11.    ...
  12. }
  13. [...]
  14. if ((val == 1) && (nb > 6))
  15. {
  16.    ...
  17. }


 
au lieu de
 

Code :
  1. if (val == 1)
  2. {
  3. if (nb == 0)
  4. {
  5.     ...
  6. }
  7. else if (nb == 1)
  8. {
  9.     ...
  10. }
  11. else if (nb == 2)
  12. {
  13.     ...
  14. }
  15. [...]
  16. else if (nb > 6)
  17. {
  18.    ...
  19. }
  20. }


3. Comme je l'ai dit c'était juste pour info :d.


Message édité par grostony le 09-03-2006 à 11:36:01
n°1321668
Taz
bisounours-codeur
Posté le 09-03-2006 à 11:39:07  profilanswer
 

2 c'est pareil, le compilateur n'est pas aussi idiot que toi :)

n°1321669
chrisbk
-
Posté le 09-03-2006 à 11:39:10  profilanswer
 

bah switch case et dans le default tu fais ton > 6 mais bon, spa tres important

n°1321676
Taz
bisounours-codeur
Posté le 09-03-2006 à 11:41:41  profilanswer
 

et pi tu veux 'optimiser', mais est-ce que tu sais au moins comment dire à ton compilateur de produire du code optimisé ?

n°1321696
grostony
Posté le 09-03-2006 à 12:01:03  profilanswer
 

C'est vrai que j'optimise surtout le code niveau lisibilité quand je fais ça, c'est vrai que ce qui se passe au niveau de compilateur je n'y connais pas grand chose :d, mais oui je sais comment optimiser mon code, l'appli sur laquelle je bosse est compilée avec cl.exe (compilateur de Visual Studio et il y a les options :

Citation :

/Ot Favor Fast Code.
/Os Favor Small Code.


 
Et pour Linux avec gcc :

Citation :

Options That Control Optimization
 
       These options control various sorts of optimizations.
 
       Without any optimization option, the compiler's goal is to reduce the cost of compilation and to make debugging produce the expected results.  Statements are indepen-
       dent: if you stop the program with a breakpoint between statements, you can then assign a new value to any variable or change the program counter to any other state-
       ment in the function and get exactly the results you would expect from the source code.
 
       Turning on optimization flags makes the compiler attempt to improve the performance and/or code size at the expense of compilation time and possibly the ability to
       debug the program.
 
       Not all optimizations are controlled directly by a flag.  Only optimizations that have a flag are listed.
 
       -O
       -O1 Optimize.  Optimizing compilation takes somewhat more time, and a lot more memory for a large function.
 
           With -O, the compiler tries to reduce code size and execution time, without performing any optimizations that take a great deal of compilation time.
 
           -O turns on the following optimization flags: -fdefer-pop -fmerge-constants -fthread-jumps -floop-optimize -fcrossjumping -fif-conversion -fif-conversion2 -fde-
           layed-branch -fguess-branch-probability -fcprop-registers
 
           -O also turns on -fomit-frame-pointer on machines where doing so does not interfere with debugging.
 
       -O2 Optimize even more.  GCC performs nearly all supported optimizations that do not involve a space-speed tradeoff.  The compiler does not perform loop unrolling or
           function inlining when you specify -O2.  As compared to -O, this option increases both compilation time and the performance of the generated code.
 
           -O2 turns on all optimization flags specified by -O.  It also turns on the following optimization flags: -fforce-mem -foptimize-sibling-calls -fstrength-reduce
           -fcse-follow-jumps  -fcse-skip-blocks -frerun-cse-after-loop  -frerun-loop-opt -fgcse   -fgcse-lm   -fgcse-sm -fdelete-null-pointer-checks -fexpensive-optimiza-
           tions -fregmove -fschedule-insns  -fschedule-insns2 -fsched-interblock -fsched-spec -fcaller-saves -fpeephole2 -freorder-blocks  -freorder-functions
           -fstrict-aliasing -falign-functions  -falign-jumps -falign-loops  -falign-labels
 
           Please note the warning under -fgcse about invoking -O2 on programs that use computed gotos.
 
       -O3 Optimize yet more.  -O3 turns on all optimizations specified by -O2 and also turns on the -finline-functions and -frename-registers options.
 
       -O0 Do not optimize.  This is the default.
 
       -Os Optimize for size.  -Os enables all -O2 optimizations that do not typically increase code size.  It also performs further optimizations designed to reduce code
           size.
 
           -Os disables the following optimization flags: -falign-functions  -falign-jumps  -falign-loops -falign-labels  -freorder-blocks  -fprefetch-loop-arrays
 
           If you use multiple -O options, with or without level numbers, the last such option is the one that is effective.


Message édité par grostony le 09-03-2006 à 12:01:49

Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  C++

  [C++]Question optimisation.

 

Sujets relatifs
question a 2 ballesMettre une question sur mon site
[C] Petite question sur la commande 'return'une question toute simple
[resolu]question de gros nul...désolé $_POST et $_GETquestion de noob
Une petite question sur gdbpetite question pour programmer sous VB
Simple question PHP (Variable)Mysql et optimisation ?
Plus de sujets relatifs à : [C++]Question optimisation.


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR