[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [PROGRAMMERING] 16 class'es



On Fri, 14 Apr 2006 23:49:57 +0200
David wrote:

> Jeg har 16 classer Design1, Design2 ....Design16 med samme base
> class. Hvordan laver jeg objekter ud fra en variabel 0<=d<16,
> sålede at når d=0 så oprettes objekt ud fra Design1 og når d=1
> ud fra Design2 osv.
> 
> Jeg kunne bruge switch
> 
> switch(d)
>     case 0:
>         o= new Design1();
>         break;
>     case 1;
>         o=new Design2();
>         break;
> ..
> ...
> 
> 
>  Men det vil tage programmet alt for lang tid, at gå alle
>  mulighederne 
> sekventielt igennem når d=15; Hvordan springer jeg i kode mest
> effektivt? 


switch/case implementeres som en jump-tabel, som fx.
"indexed relative jump" i x86-asm

det er derfor at syntaxen er case <constant>:

Så normalt løbes der ikke noget igennem sekventielt.

Se fx. asm-output fra min gamle gcc-3.3.2; 


        jmp     *.L13(,%edx,4)  ;; <-- jump indexeret af edx.
        .section        .rodata
        .align 4
        .align 4
.L13:
        .long   .L3   ;; read-only data som udgør jump-tabel.
        .long   .L5
        .long   .L7
        .long   .L8
        .long   .L9
        .long   .L10
        .text ;; ".text" betyder maskin-kode som assembler-instruktioner.

.L3:
        movl    $1, %eax  ;; case 1: gcc har bortoptimeret Design1() ;-)
        .p2align 4,,15    ;; desv. denne-her kan jeg ikke lige gennemskue.

    [... cut...]


Kildetxt er oversat gcc -S -O3 switch-expl1.c 
Kilde til sådan en jump tabel (det er ikke C++):

#define new     // reduktion af expl. - vil ikke have for meget kode.

int Design1()
{
    return 1;
}

int Design2()
{
    return 2;
}

int Design3()
{
    return 3;
}

int main(int argc, char **argv, char **env)
{
    int d; /* kun som demo - egtl input fra anden side */
    int o; /* fx.status */

    d = (int) (rand() * 15);  /* simulere input */

    switch (d) {

    case 0:
	o = new Design1();
	break;

    case 1:
	o = new Design2();
	break;

    case 2:
        o = 23;
        break;
    case 3:
        o = 34;
        break;
    case 4:
        o=45;
        break;
    case 5:
        o=55;
        break;

    default:
	o = new Design3();
    }
    return o;
}





-- 
donald_j_axel donax snabela get2net.dk -- http://d-axel.dk/


 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2006-05-01, 02:01 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *