🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: es.comp.lenguajes.c++
Article #101

Re: switch vs if else if

#101
From: Pascal J. Bourguignon
Date: Mon, 04 Aug 2008 07:23
57 lines
3395 bytes
Omar Andres Zapata Mesa <andresete.chaos@gmail.com> writes:

> Hola tengo una pregunta que tiene que ver con el rendimiento que
> ofrecen los condicionales.  la cosa es que hay muchos debates sobre
> el uso de switch en if esle if anidado, quisiera saber cual de los
> dos condicionales es mas optimo y por que?

La verdad es que depende del compilador. Si tiene fases de
optimizac�on sofisticadas, entonces probablemente va a generar lo
mismo.  Sinon, es posible que el switch sea implementado m�s
eficazmente.

Por ejemplo:
----(a.c)---------------------------------
#include <stdio.h>

void f(int x){
    switch(x){
    case 0: printf("cero\n");break;
    case 1: printf("uno\n");break;
    case 2: printf("dos\n");break;
    case 3: printf("tres\n");break;
    case 4: printf("cuatro\n");break;
    case 5: printf("cinco\n");break;
    case 6: printf("seis\n");break;
    case 7: printf("siete\n");break;
    case 8: printf("ocho\n");break;
    case 9: printf("nueve\n");break;
    default:  printf("otro\n");break; }}

void g(int x){

    if(x==0){ printf("cero\n"); }
    else if(x==1){ printf("uno\n"); }
    else if(x==2){ printf("dos\n"); }
    else if(x==3){ printf("tres\n"); }
    else if(x==4){ printf("cuatro\n"); }
    else if(x==5){ printf("cinco\n"); }
    else if(x==6){ printf("seis\n"); }
    else if(x==7){ printf("siete\n"); }
    else if(x==8){ printf("ocho\n"); }
    else if(x==9){ printf("nueve\n"); }
    else{  printf("otro\n"); }}

------------------------------------------
gcc -O3 -c -S -o a.s a.c

se ve en a.s que el switch esta compilado por un �nico salto via una
tabla, en lugar, los ifs estan compilados por saltos
succesivos. Promedio, habra un test y un salto con el switch, y 5
tests y 5 saltos con los ifs.

Pero, un compilador un pelin m�s inteligente podria realizar que g y f
son equivalente y general el mismo codigo por ambas.

-- 
__Pascal Bourguignon__
Technical Details
Message-ID: <7ciquhjlaw.fsf@pbourguignon.anevia.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!feeder1-2.proxad.net!proxad.net!feeder2-2.proxad.net!cleanfeed2-a.proxad.net!nnrp19-1.free.fr!not-for-mail
References: <g74t54$5eg$1@helvete.escomposlinux.org>

Show Full Headers
Complete Article Headers
X-Google-Thread: f53ec,7b7f6f4fddf24aa0
X-Google-Attributes: gidf53ec,domainid0,public,usenet
X-Google-Language: SPANISH,ASCII
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!feeder1-2.proxad.net!proxad.net!feeder2-2.proxad.net!cleanfeed2-a.proxad.net!nnrp19-1.free.fr!not-for-mail
Newsgroups: es.comp.lenguajes.c++
Subject: Re: switch vs if else if
Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAABlBMVEUAAAD///+l2Z/dAAAA
      oElEQVR4nK3OsRHCMAwF0O8YQufUNIQRGIAja9CxSA55AxZgFO4coMgYrEDDQZWPIlNAjwq9
      033pbOBPtbXuB6PKNBn5gZkhGa86Z4x2wE67O+06WxGD/HCOGR0deY3f9Ijwwt7rNGNf6Oac
      l/GuZTF1wFGKiYYHKSFAkjIo1b6sCYS1sVmFhhhahKQssRjRT90ITWUk6vvK3RsPGs+M1RuR
      mV+hO/VvFAAAAABJRU5ErkJggg==
X-Accept-Language:         fr, es, en
X-Disabled: X-No-Archive: no
References: <g74t54$5eg$1@helvete.escomposlinux.org>
From: pjb@informatimago.com (Pascal J. Bourguignon)
Date: Mon, 04 Aug 2008 09:23:19 +0200
Message-ID: <7ciquhjlaw.fsf@pbourguignon.anevia.com>
Organization: Anevia SAS
User-Agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/22.1 (gnu/linux)
Cancel-Lock: sha1:NTA4NDFkNjZmZjdhZTEwNjc2NDMzMTVlMjQwYmYxZWVlMjBmMWE4Yw==
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit
Lines: 57
NNTP-Posting-Date: 04 Aug 2008 09:23:18 MEST
NNTP-Posting-Host: 88.170.236.224
X-Trace: 1217834598 news-2.free.fr 1305 88.170.236.224:55422
X-Complaints-To: abuse@proxad.net